Sql2

479 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
479
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sql2

  1. 1. Reddit SQL Lecture 2<br />Shane Smith<br />
  2. 2. Recap<br />
  3. 3. Parts of a database<br />Record<br />Tables<br />Attribute/Field<br /><ul><li> Records become “rows”
  4. 4. Attributes/fields become “columns”
  5. 5. Rules determine the relationship between the tables and tie the data together to form a database</li></li></ul><li>Creating tables with CREATE<br />Adding data with INSERT<br />Viewing data with SELECT<br />Removing data with DELETE<br />Modifying data with UPDATE<br />Destroying tables with DROP<br />Basic SQL Commands<br />
  6. 6. The WHERE clause<br />Helps parse results for specific criteria<br />Multiple WHERE clauses can be strung together<br />Will evaluate results based on order in which WHERE clauses occur<br />Can help reduce cost if used properly, or increase if used improperly<br />
  7. 7. WHERE clause syntax<br />SELECT column_name(s)<br />FROM table_name<br />WHERE column_nameoperator‘value’<br />EX:<br />SELECT name, phone_number, address<br />FROM contacts<br />WHERE zip code=‘32792’<br />
  8. 8. Using AND and OR<br />As queries become more complex, AND/OR can be used to parse results even further<br />Used in conjunction with WHERE statements<br />AND retrieves data that meet all criteria<br />OR retrieves data that meet any criteria<br />Both can come at significant cost<br />
  9. 9. Using AND/OR<br />SELECT column_name(s)<br />FROM table_name<br />WHERE column_nameoperator ‘value’<br />AND column_nameoperator ‘value’<br />EX:<br />SELECT name, phone_number, address<br />FROM contacts<br />WHERE zip code=32792<br />AND children=25<br />
  10. 10. Sorting<br />You can use ORDER BY to sort your results:<br />SELECT column_name(s)<br />FROM table_name<br />ORDER BY column_name(s) ASC/DESC<br />EX:<br />SELECT name, phone_number, address<br />FROM contacts<br />WHERE zip code=32792<br />ORDER BY name ASC<br />*Default order, if not specified is ASCENDING<br />
  11. 11. Using IN<br />You can use IN to specify multiple criteria for a WHERE statement<br />Helps reduce query clutter<br />Reduces query cost in some cases<br />
  12. 12. Using IN<br />SELECT column_name(s)<br />FROM table_name<br />WHERE column_nameIN (‘value1’, ‘value2’)<br />EX:<br />SELECT name, phone_number, address<br />FROM contacts<br />WHERE zip codeIN (‘32792’, ‘33707’)<br />
  13. 13. Using BETWEEN<br />The BETWEEN operator is used in a WHERE clause to select a range of data between two values<br />SELECT column_name(s)<br />FROM table_name<br />WHERE column_name<br />BETWEEN ‘value1’ AND ‘value2’<br />
  14. 14. Using TOP<br />The TOP clause is used to specify the number of records to return.<br />The TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance.<br />TOP PERCENT can also be used<br />
  15. 15. Using TOP<br />SELECT TOP number|percentcolumn_name(s)<br />FROM table_name<br />EX:<br />SELECT TOP 50 name, phone_number, address<br />FROM contacts<br />SELECT TOP 50 PERCENT name, phone_number, address<br />FROM contacts<br />
  16. 16. TOP equivalents<br />Not all DBMS’s use TOP<br />MySQL uses LIMIT<br />Oracle uses a WHERE statement with the value ROWNUM<br />SELECT column_name(s)<br />FROM table_name<br />WHERE ROWNUM <= number<br />

×