6. The WHERE clause Helps parse results for specific criteria Multiple WHERE clauses can be strung together Will evaluate results based on order in which WHERE clauses occur Can help reduce cost if used properly, or increase if used improperly
7. WHERE clause syntax SELECT column_name(s) FROM table_name WHERE column_nameoperator‘value’ EX: SELECT name, phone_number, address FROM contacts WHERE zip code=‘32792’
8. Using AND and OR As queries become more complex, AND/OR can be used to parse results even further Used in conjunction with WHERE statements AND retrieves data that meet all criteria OR retrieves data that meet any criteria Both can come at significant cost
9. Using AND/OR SELECT column_name(s) FROM table_name WHERE column_nameoperator ‘value’ AND column_nameoperator ‘value’ EX: SELECT name, phone_number, address FROM contacts WHERE zip code=32792 AND children=25
10. Sorting You can use ORDER BY to sort your results: SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC/DESC EX: SELECT name, phone_number, address FROM contacts WHERE zip code=32792 ORDER BY name ASC *Default order, if not specified is ASCENDING
11. Using IN You can use IN to specify multiple criteria for a WHERE statement Helps reduce query clutter Reduces query cost in some cases
12. Using IN SELECT column_name(s) FROM table_name WHERE column_nameIN (‘value1’, ‘value2’) EX: SELECT name, phone_number, address FROM contacts WHERE zip codeIN (‘32792’, ‘33707’)
13. Using BETWEEN The BETWEEN operator is used in a WHERE clause to select a range of data between two values SELECT column_name(s) FROM table_name WHERE column_name BETWEEN ‘value1’ AND ‘value2’
14. Using TOP The TOP clause is used to specify the number of records to return. The TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance. TOP PERCENT can also be used
15. Using TOP SELECT TOP number|percentcolumn_name(s) FROM table_name EX: SELECT TOP 50 name, phone_number, address FROM contacts SELECT TOP 50 PERCENT name, phone_number, address FROM contacts
16. TOP equivalents Not all DBMS’s use TOP MySQL uses LIMIT Oracle uses a WHERE statement with the value ROWNUM SELECT column_name(s) FROM table_name WHERE ROWNUM <= number