Team LiB   Previous Section   Next Section

2.5 WHERE to Go from Here

This chapter has introduced the role of the WHERE clause in different types of SQL statements as well as the various components used to build a WHERE clause. Because the WHERE clause plays such an important role in many SQL statements, however, the topic is far from exhausted. Additional coverage of WHERE clause topics may be found in:

  • Chapter 3, in which various flavors of join conditions are studied in detail

  • Chapter 5, which probes the different types of subqueries along with the appropriate operators for evaluating their results

  • Chapter 6, in which various methods of handling date/time data are explored

  • Chapter 14, which explores certain aspects of the WHERE clause from the standpoint of performance and efficiency

Additionally, here are a few tips to help you make the most of your WHERE clauses:

  1. Check your join conditions carefully. Make sure that each data set in the FROM clause is properly joined. Keep in mind that some joins require multiple conditions. See Chapter 3 for more information.

  2. Avoid unnecessary joins. Just because two data sets in your FROM clause contain the same column does not necessitate a join condition be added to your WHERE clause. In some designs, redundant data has been propagated to multiple tables through a process called denormalization. Take the time to understand the database design, and ask your DBA or database designer for a current data model.

  3. Use parentheses. Oracle maintains both operator precedence and condition precedence, meaning there are clearly defined rules for the order in which things will be evaluated, but the safest route for you and for those who will later maintain your code is to dictate evaluation order using parentheses. For operators, specifying (5 * p.inventory_qty) + 2 rather than 5 * p.inventory_qty + 2 makes the order in which the operations should be performed clear. For conditions, use parentheses any time the OR operator is employed.

  4. Use consistent indentation. For example, if the previous line contains a left parenthesis without a matching right parenthesis, indent the current line to show that it is a continuation of the previous line.

  5. When using OR, put the condition requiring the least effort to evaluate first. If the first condition evaluates to TRUE, Oracle won't bother evaluating the remaining OR'd conditions, possibly saving significant execution time. This strategy is useful with correlated subqueries, which are generally executed once per candidate row.

  6. Handle NULLs properly. After writing your WHERE clause, inspect each condition with respect to its ability to properly handle NULL values. Take the time to understand the table definitions in your database so that you know which columns allow NULLs.

  7. Pick up introductory books on logic and set theory at your local library. While understanding these two topics won't necessarily get you invited to more cocktail parties, it will certainly make you a better SQL programmer.

    Team LiB   Previous Section   Next Section