Oracle
  Home arrow Oracle arrow Stepping through Sub-Queries in Oracle
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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
     
     
    The Best Selling PC Migration Utility.
     
    ADVERTISEMENT

    Virtual Tradeshows by Ziff Davis Enterprise - A Unique Opportunity to Connect with IT Experts, Access Information, and Gain Insight on today's Technology

    Stepping through Sub-Queries in Oracle
    (Page 1 of 5 )

    This is the first article in a series concentrating on working with sub-queries in Oracle. Sub-queries really have tremendous depth. In this series I will show you several scenarios where they can be efficiently used to retrieve information from Oracle databases.

    As this is the first in series, I will be concentrating on the following topics:

    • How to work with a “dual” table.
    • How to analyze and identify the steps needed to deal with a sub-query.
    • How to frame queries for each of the identified steps.
    • How to combine all the framed queries and design a single command to retrieve the final output.

    A primer on the “dual” table in Oracle

    This section mainly explains the “dual” table in Oracle.  I shall use this table in a creative manner wherever required in this article as well as upcoming articles.  If you are already familiar with the "dual” table, feel free to skip to the next section.

    What is a “dual” table?  It is a simple table which is created/installed automatically during the installation of the Oracle database.  To understand it, let us consider the following SELECT statement:

    SELECT 123 FROM dual;

    The above statement simply returns 123.  Let us work with another statement:

    SQL> SELECT 10,20,'Jagadish Chatarji',3400 FROM dual;

    10    20    'JAGADISHCHATARJI' 3400                   

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

    10    20    Jagadish Chatarji  3400                  

    1 rows selected

    This returns any constant values you provide as columns.  So “dual” is just a convenience table.  It is simply a one column and one row table that exists as part of SYS user.  You can use the DESC command to display the structure of a “dual” table as follows:

    DESC dual;

    The above statement returns the following output:

    Name

    Null

    Type

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

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

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

    DUMMY

     

    VARCHAR2(1)

    You can observe that there exists only one column named “dummy” in the "dual" table.  Similarly, you can even display all the rows in the "dual" table as follows:

    SQL> SELECT * FROM dual;

    DUMMY

    -----

    X    

    1 rows selected

    From the above, you can observe that there exists only one row with a dummy value of "x." 

    You don't really need to use the “dual” table at all.  It is only used when you want to add/include any constant values in your queries.  You can even do calculations as follows:

    SQL> SELECT 12 * 13 + 14 FROM dual;

    12*13+14              

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

    170                   

    1 rows selected

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