Oracle
  Home arrow Oracle arrow Page 4 - 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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 23
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Stepping through Sub-Queries in Oracle - Designing sub-queries to deal with more than one table (or different tables)
    ( Page 4 of 5 )

    Let us consider that I would like to retrieve KING's department name.  All department names are available in the table “dept,” which is quite different from “emp” table.  Let us write the question in a meaningful manner and identify the steps as follows:

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

    • Find KING's department number (1).
    • Based on the value you get, retrieve the department details like dname, loc. etc belonging to that department number (2)

    The following is the statement which retrieves KING's department number:

    SELECT deptno FROM emp WHERE ename = ‘KING’

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

    SELECT dname FROM dept

    WHERE deptno = 30

    Just replace the value 30 with the query that gives you KING's department number.  The complete statement would be as follows:

    SQL> SELECT dname FROM dept

    WHERE deptno = (SELECT deptno FROM emp WHERE ename=’KING’);

    Now, let us walk through its execution:

    • The innermost query gets executed first. 
    • In this case, the query “select deptno from emp where ename=’king’” gets executed first.  It retrieves KING's department number.
    • Once the innermost query gets executed, it returns a value to the immediate outer query.  In this case, it is 10.
    • The entire innermost query gets replaced with the new value returned by it.  In this case, the outer-query virtually becomes “select dname from dept where deptno = 10.”
    • And finally, the outer query gets executed, which retrieves KING’s department details



     
     
    >>> More Oracle Articles          >>> More By Jagadish Chatarji
     

       

    ORACLE ARTICLES

    - Oracle's Turn to Play in the Sun
    - Implementing and Using Oracle`s Restore Poin...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT