Oracle
  Home arrow Oracle arrow Page 5 - Stepping through Sub-Queries in Oracle
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

Stepping through Sub-Queries in Oracle
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 16
    2006-11-07

    Table of Contents:
  • Stepping through Sub-Queries in Oracle
  • The simplest sub-query in Oracle
  • A sub-query with aggregate functions (or group functions) in Oracle
  • Designing sub-queries to deal with more than one table (or different tables)
  • An example of a nested sub-query (or multi-level sub-query)

  • 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

    Stepping through Sub-Queries in Oracle - An example of a nested sub-query (or multi-level sub-query)


    (Page 5 of 5 )

    Let us consider that I would like to retrieve the department name of the highest paid employee. Let us write the question in a meaningful manner and identify the steps as follows:

    From the above figure, you have three steps to go through with the query.  The following is the order you must follow (based on the above figure):

    • Find the highest salary (1).
    • Based on the value you get, retrieve the department number belonging to that salary (2).
    • Based on the value you get, find the department name belonging to that department number (3).

    The following is the statement which finds the highest salary:

    SELECT MAX(sal) FROM emp

    To retrieve a department number based on a given salary (say 2000), the query would be as follows:

    SELECT deptno FROM emp

    WHERE sal = 2000

    To retrieve a department name based on a given department number (say 20), the query would be as follows:

    SELECT dname FROM dept

    WHERE deptno = 20

    Combining all of the above queries according to the order given above, the new query would be as follows:

    SQL> SELECT dname FROM dept

    WHERE deptno = (SELECT deptno FROM emp

    WHERE sal = (SELECT MAX(sal) FROM EMP));

    You can observe the following figure to understand how the execution takes place.  You can also observe the underlined columns on how they relate logically:

    Now, let us walk through its execution:

    • The innermost query gets executed first. 
    • In this case, the query “select max(sal) from emp” gets executed first.  It retrieves the highest salary. In this case it would be 5000.
    • The entire innermost query gets replaced with the new value returned by it.  In this case, the immediate outer query virtually becomes “select deptno from emp where sal = 5000.”  Once this query gets executed, it returns a department number, which is 10 in this case.
    • And finally, the outermost query virtually becomes “select dname from dept where deptno = 10,” which retrieves KING’s department details.

    Any bugs, doubts, suggestions, feedback etc. are highly appreciated at http://jagchat.spaces.live.com


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hello guys. I started a new series on working with sub-queries in Oracle. Enjoy and...
     

       

    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 6 hosted by Hostway