SunQuest
 
       Oracle
  Home arrow Oracle arrow Page 3 - Mastering the WHERE Clause
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ORACLE

Mastering the WHERE Clause
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 13
    2004-10-19

    Table of Contents:
  • Mastering the WHERE Clause
  • WHERE to the Rescue
  • WHERE Clause Evaluation
  • Conditions and Expressions
  • Membership Conditions and Range Conditions
  • Matching Conditions
  • Regular Expressions and Handling NULL
  • Placement of Join Conditions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Mastering the WHERE Clause - WHERE Clause Evaluation


    (Page 3 of 8 )

    Now that you have seen the WHERE clause in action, let’s take a look at how it is evaluated. As previously mentioned, the WHERE clause consists of one or more conditions that evaluate independently to TRUE or FALSE. If your WHERE clause consists of multiple conditions, the conditions are separated by the logical operators AND and OR. Depending on the outcome of the individual conditions and the placement of these logical operators, Oracle will assign a final value of TRUE or FALSE to each candidate row, thereby determining whether a row will be included in the final result set.

    Here’s another look at the Acme Industries query:

    SELECT p.part_nbr, p.name, p.supplier_id, p.status, p.inventory_qty,
      s.supplier_id, s.name
    FROM part p, supplier s
    WHERE s.supplier_id = p.supplier_id
      AND s.name = 'Acme Industries';

    The WHERE clause consists of two conditions separated by AND. Thus, a row will only be included if both conditions evaluate to TRUE. Table 2-1 shows the possible scenarios when conditions are replaced by their possible outcomes.

    Table 2-1. Multiple-condition evaluation using AND

    Intermediate result Final result
    WHERE TRUE AND TRUE TRUE
    WHERE FALSE AND FALSE FALSE
    WHERE FALSE AND TRUE FALSE
    WHERE TRUE AND FALSE FALSE

     

     

     

     

    Using basic logic rules, you can see that the only combination of outcomes that results in a final value of TRUE being assigned to a candidate row is where both conditions evaluate to TRUE. Table 2-2 demonstrates the possible outcomes if the conditions had been separated by OR rather than AND.

    Intermediate result Final result
    WHERE TRUE OR TRUE TRUE
    WHERE FALSE OR FALSE FALSE
    WHERE FALSE OR TRUE TRUE
    WHERE TRUE OR FALSE TRUE

    Table 2-2. Multiple-condition evaluation using OR

    Next, let’s spice the query up a bit by including parts supplied by either Acme Industries or Tilton Enterprises:

    SELECT p.part_nbr, p.name, p.supplier_id, p.status, p.inventory_qty,
      s.supplier_id, s.name
    FROM part p, supplier s
    WHERE s.supplier_id = p.supplier_id
      AND (s.name = 'Acme Industries'
        OR s.name = 'Tilton Enterprises');

    There are now three separate conditions separated by AND and OR with parentheses surrounding two of the conditions. Table 2-3 illustrates the possible outcomes.

    Intermediate result Final result
    WHERE TRUE AND (TRUE OR FALSE) TRUE
    WHERE TRUE AND (FALSE OR TRUE) TRUE
    WHERE TRUE AND (FALSE OR FALSE) FALSE
    WHERE FALSE AND (TRUE OR FALSE) FALSE
    WHERE FALSE AND (FALSE OR TRUE) FALSE
    WHERE FALSE AND (FALSE OR FALSE) FALSE

    Table 2-3. Multiple-condition evaluation using AND and OR

    Since a particular part cannot be supplied by both Acme Industries and Tilton Enterprises, the intermediate results TRUE AND (TRUE AND TRUE) and FALSE AND (TRUE AND TRUE) were not included in Table 2-3.

    To liven things up even more, here’s an example using the NOT operator. The following query returns data for parts supplied by anyone other than Acme Industries or Tilton Enterprises:

    SELECT p.part_nbr, p.name, p.supplier_id, p.status, p.inventory_qty,
      s.supplier_id, s.name
    FROM part p, supplier s
    WHERE s.supplier_id = p.supplier_id
      AND NOT (s.name = 'Acme Industries'
        OR s.name = 'Tilton Enterprises');

    Table 2-4 demonstrates how the addition of the NOT operator changes the outcome.

    Intermediate result Final result
    WHERE TRUE AND NOT (TRUE OR FALSE) FALSE
    WHERE TRUE AND NOT (FALSE OR TRUE) FALSE
    WHERE TRUE AND NOT (FALSE OR FALSE) TRUE
    WHERE FALSE AND NOT (TRUE OR FALSE) FALSE
    WHERE FALSE AND NOT (FALSE OR TRUE) FALSE
    WHERE FALSE AND NOT (FALSE OR FALSE) FALSE

    Table 2-4. Multiple-condition evaluation using AND, OR, and NOT

    The use of the NOT operator in the previous example is a bit forced; later examples will demonstrate more natural ways of expressing the same logic.  

    Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

    Visit the O'Reilly Network http://www.oreillynet.com for more online content.

    More Oracle Articles
    More By O'Reilly Media


     

       

    ORACLE ARTICLES

    - Tuning PL/SQL Code
    - Debugging PL/SQL Code
    - Testing PL/SQL Code
    - Working With PL/SQL Code
    - Conditional Compilation for Oracle Database ...
    - Compile-Time Warnings for Oracle DB 10g
    - Compiling PL/SQL Code for an Oracle Database
    - Troubleshooting PL/SQL Code
    - Managing PL/SQL Code
    - Data Manipulation and More for HTML DB Appli...
    - Oracle Database Fundamentals
    - Adding Processes to HTML DB Applications
    - Adding Computations, Processes, and Validati...
    - Sub-templates and More with Oracle HTML DB
    - Focusing on Templates in Oracle HTML DB





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway