Oracle
  Home arrow Oracle arrow Page 2 - 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 to the Rescue


    (Page 2 of 8 )

    Hopefully, the scenarios in the previous section give you some insight into the utility of the WHERE clause, including the ability to:

    • Filter out unwanted data from a query’s result set.

    • Isolate one or more rows of a table for modification.

    • Conditionally join two or more data sets together.

    To see how these things are accomplished, let’s add a WHERE clause to the previous SELECT statement, which strives to locate all parts supplied by Acme Industries. Here’s the query with the new WHERE clause:

    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 here is comprised of two parts, known as conditions, which are evaluated separately. Conditions always evaluate to either TRUE or FALSE; if there are multiple conditions in a WHERE clause, they all must evaluate to TRUE for a given row to be included in the result set. Actually, that’s a bit of an oversimplification. As you will see later, using the OR and NOT operators allows the WHERE clause to evaluate to TRUE even if individual conditions evaluate to FALSE.

    For this example, a row created by combining data from the part and supplier tables will only be included in the final result set if both tables share a common value for the supplier_id column, and if the value of the name column in the supplier table matches 'Acme Industries'. Any other permutation of data from the two tables would evaluate to FALSE and be discarded.

    Note: For this chapter only, we’ll use the older style of join syntax in which you specify join conditions in the WHERE clause. We do this to explore the full functionality of the WHERE clause.

    With the addition of the WHERE clause to the previous example, therefore, Oracle will take on the work of discarding undesired rows from the result set, and only 50 rows would be returned by the query, rather than 1,000,000. Now that you have retrieved the 50 rows of interest from the database, you can begin the process of modifying the data. Keep in mind, however, that with the WHERE clause at your disposal you will no longer need to delete and re-insert your modified data; instead, you can use the UPDATE statement to modify specific rows based on the part_nbr column, which is the unique identifier for the table:

    UPDATE part
    SET status = 'DISCONTINUED'
    WHERE part_nbr = 'AI5-4557';

    While this is certainly an improvement, you can do even better. If your intent is to modify the status for all 50 parts supplied by Acme Industries, there is no need to execute a separate query at all. Simply execute a single UPDATE statement that finds and modifies all 50 records:

    UPDATE part
    SET status = 'DISCONTINUED'
    WHERE supplier_id =
      (SELECT supplier_id
        FROM supplier
        WHERE name = 'Acme Industries');

    The WHERE clause in this statement consists of a single condition that equates the supplier_id column to the value returned by the subquery against the supplier table. Subqueries are covered extensively in Chapter 5, so don’t worry if this looks a bit intimidating. The net result is that the condition will be rewritten to use the value returned by the subquery, as in:

    UPDATE part |
    SET status = 'DISCONTINUED'
    WHERE supplier_id = 1;

    When executed, the condition evaluates to TRUE for exactly 50 of the 10,000 rows in the part table, and the status of those 50 rows changes to DISCONTINUED.  

    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

    Click Here




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