Oracle
  Home arrow Oracle arrow Page 2 - 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 
Moblin 
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 - The simplest sub-query in Oracle


    (Page 2 of 5 )

    Before explaining sub-queries, let us consider the following scenario:

    SQL> SELECT empno,ename,sal,deptno FROM emp

    WHERE sal = 5000;

    EMPNO     ENAME      SAL         DEPTNO                

    --------- ---------- ----------- ------------

    7839      KING       5000        10                    

    1 rows selected

    Let us modify the above statement as follows:

    SQL> SELECT empno,ename,sal,deptno FROM emp

    WHERE sal = (SELECT 5000 FROM dual);

    I already explained the “dual” table in the previous section.  In the above statement, I have two SELECT statements as part of a single command.  The following is the order in which the above statement gets executed:

    • The innermost query gets executed first. 
    • In this case, the query “select 5000 from dual” gets executed first.
    • Once the innermost query gets executed, it returns a value to the immediate outer query.  In this case, it is 5000.
    • The entire innermost query gets replaced with the new value returned by it.  In this case, the outer query virtually becomes “select empno, ename, sal, deptno from emp where sal = 5000.”
    • And finally, the outer query gets executed, which retrieves KING’s details.

    In the above case, I used a SELECT query as part of another SELECT query; thus it is called a “sub-query.”  You can even modify the above statement to include an expression as follows:

    SQL> SELECT empno,ename,sal,deptno FROM emp

    WHERE sal = (SELECT 2000+3000 FROM dual);

    The above statement first evaluates “2000+3000” (which results in 5000) as part of executing the inner query.  Based on the returned value (which is 5000), the outer query gets executed.

    The next section will show you a few simple and practically used sub-queries.

    More Oracle Articles
    More By Jagadish Chatarji


       · 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