06.01 sql select distinct


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

06.01 sql select distinct

  1. 1. SQL SELECT DISTINCTStatement In a table, some of the columns may containduplicate values. This is not a problem,however, sometimes you will want to list onlythe different (distinct) values in a table. The DISTINCT keyword can be used to returnonly distinct (different) values. SQL SELECT DISTINCT SyntaxSELECT DISTINCT column_name(s)FROM table_name
  2. 2. SELECT DISTINCT Person.PNameFROM Person;Id Name Address Hobby1123 Anita Damauli stamps1123 Anita Damauli coins5556 BinodKathmanduhiking9876 BarshaKathmandustampsPNameAnitaBarshaBinod
  3. 3. SQL AND & OR Operators The AND & OR operators are used to filterrecords based on more than one condition The AND operator displays a record if both thefirst condition and the second condition aretrue. The OR operator displays a record if either thefirst condition or the second condition is true.
  4. 4. SQL OR OperatorsSELECT * FROM PersonWHERE Person.Hobby=hiking ORPerson.ID>3000;ID PName Address Hobby5556 Binod Kathmandu hiking9876 Barsha Kathmandu stamps
  5. 5. SQL AND OperatorsSELECT *FROM PersonWHERE Person.ID>3000 AND Person.ID<5999;ID PName Address Hobby5556 Binod Kathmandu hiking
  6. 6. SQL ORDER BY The ORDER BY keyword is used to sort theresult-set. The ORDER BY keyword is used to sort theresult-set by a specified column. The ORDER BY keyword sorts the records inascending order by default. If you want to sort the records in a descendingorder, you can use the DESC keyword.
  7. 7. SQL ORDER BY Syntax SELECT column_name(s)FROM table_nameORDER BY column_name(s) ASC|DESCSELECT Person.HobbyFROM PersonORDER BY Person.Hobby;Hobbycoinshikingstampsstamps
  8. 8. SQL INSERT INTO The INSERT INTO statement is used to insert a newrow in a table.SQL INSERT INTO Syntax It is possible to write the INSERT INTO statement intwo forms. The first form doesnt specify the column nameswhere the data will be inserted, only their values:INSERT INTO table_name VALUES (value1, value2,value3,...) The second form specifies both the column namesand the values to be inserted:INSERT INTO table_name (column1, column2, column3,...)VALUES (value1, value2, value3,...)
  9. 9. SQL INSERT INTO INSERT INTO PersonsVALUES (4,Nilsen, Johan, Bakken 2,Stavanger) INSERT INTO Persons (P_Id, LastName,FirstName)VALUES (5, Tjessem, Jakob)
  10. 10. SQL UPDATEThe UPDATE statement is used to updateexisting records in a table.SQL UPDATE Syntax:UPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_valueExample:UPDATE PersonsSET Address=‘Nayabazar, City=‘KathmanduWHERE LastName=‘Ghimire ANDFirstName=‘Bishal
  11. 11. SQL UPDATE Warning Be careful when updating records. If we hadomitted the WHERE clause in the exampleabove, like this:UPDATE PersonsSET Address=‘Nayabazar, City=‘Kathmandu‘What will be the result ?
  12. 12. SQL DELETE The DELETE statement is used to delete rowsin a table.SQL DELETE SyntaxDELETE FROM table_nameWHERE some_column=some_valueDELETE FROM PersonsWHERE Address=‘Nayabazar, City=‘Kathmandu‘
  13. 13. SQL TOP The TOP clause is used to specify the numberof records to return. The TOP clause can be very useful on largetables with thousands of records. Returning alarge number of records can impact onperformance. Note: Not all database systems support theTOP clause. SQL Server Syntax SELECT TOP number|percent column_name(s)FROM table_name
  14. 14. SQL TOP MySQL Syntax SELECT column_name(s)FROM table_nameLIMIT number Oracle Syntax SELECT column_name(s)FROM table_nameWHERE ROWNUM <= number SELECT * FROM Persons LIMIT 5
  15. 15. SQL LIKE The LIKE operator is used to search for aspecified pattern in a column. SQL LIKE Syntax SELECT column_name(s)FROM table_nameWHERE column_name LIKE pattern
  16. 16. SQL LIKE To select the persons living in a city that startswith "s" from the table. SELECT * FROM PersonsWHERE City LIKE ‘k%
  17. 17. SQL WildcardsWildcard Description% A substitute for zero or more characters_ A substitute for exactly one character[charlist] Any single character in charlist[^charlist]or[!charlist]Any single character not in charlist• SQL wildcards can substitute for one or morecharacters when searching for data in adatabase.• SQL wildcards must be used with the SQLLIKE operator.
  18. 18. SQL IN The IN operator allows you to specify multiplevalues in a WHERE clause. SQL IN Syntax SELECT column_name(s)FROM table_nameWHERE column_name IN (value1,value2,...)
  19. 19.  Example #1Select *From AddressWhere FirstName IN (Mary, Sam) Example #2SELECT *FROM AddressWHERE FirstName = MaryOR FirstName = Sam
  20. 20. SQL BETWEEN The BETWEEN operator selects a range ofdata between two values. The values can benumbers, text, or dates. SQL BETWEEN Syntax SELECT column_name(s)FROM table_nameWHERE column_nameBETWEEN value1 AND value2
  21. 21.  Example: SELECT * FROM suppliers WHERE supplier_idBETWEEN 5000 AND 5010; SELECT * FROM suppliers WHERE supplier_id>= 5000 AND supplier_id <= 5010;
  22. 22.  example: SELECT * FROM orders WHERE order_datebetween to_date (2013/01/01, yyyy/mm/dd)AND to_date (2013/12/31, yyyy/mm/dd); SELECT * FROM orders WHERE order_date >=to_date(2013/01/01, yyyy/mm/dd) ANDorder_date <=to_date(2013/12/31,yyyy/mm/dd);