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  
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? 
Google.com  
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 - 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
     

       

    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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek