Oracle
  Home arrow Oracle arrow Page 6 - Inserting Sub-Queries in SELECT Statements 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

Inserting Sub-Queries in SELECT Statements in Oracle
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 46
    2006-11-14


    Table of Contents:
  • Inserting Sub-Queries in SELECT Statements in Oracle
  • Sub-Queries returning single and multiple values in Oracle
  • Sub-Queries as part of the BETWEEN operator in Oracle
  • Derived tables (or inline views) with Sub-Queries in Oracle
  • Sub-Queries with CASE structure in Oracle SELECT statements
  • Sub-Queries as (or part of) columns in Oracle SELECT statements

  • 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


    Inserting Sub-Queries in SELECT Statements in Oracle - Sub-Queries as (or part of) columns in Oracle SELECT statements
    ( Page 6 of 6 )

    Before getting into sub-queries as part of columns, let us look at the following small query:

    SELECT
        MAX(sal) AS highest,
        MIN(sal) AS least,
        COUN(*) AS employees,
        SUM(sal) AS total
    FROM emp

    Everyone can easily understand that the above query returns only one row containing four values of aggregation. Let us rewrite the above query using sub-queries to get the same output.

    SELECT
      (SELECT MAX(sal) FROM emp) AS highest,
      (SELECT MIN(sal) FROM emp) AS least,
      (SELECT COUNT(*) FROM emp) AS employees,
      (SELECT SUM(sal) FROM emp) AS total
    FROM dual

    You can observe that I replaced all aggregate functions with sub-queries!  Another important issue to concentrate on in the above query is the “dual” table.  As the sub-queries in the above statement are working individually by themselves, I need not work with any table and thus I used the “dual” table.  If you want to learn more about the “dual” table, please go through my first article in this same series.

    Now, let us look at an interesting query which deals with sub-queries at both the column level and the CASE level.  The following is the query:

    SELECT
      empno,
      ename,
      sal AS salary,
      ROUND((sal -(SELECT AVG(sal) FROM emp)),2) AS avgcompare,
      CASE 
        WHEN sal >= (SELECT AVG(sal) FROM emp) THEN 'HIGH'
        ELSE 'LOW'
      END AS paying
    FROM emp

    The following is the sample output of the above query:

    EMPNO   ENAME      SALARY   AVGCOMPARE   PAYING

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

    7839    KING       5000     2926.79      HIGH  

    7698    BLAKE      2850     776.79       HIGH  

    .

    .

    7654    MARTIN     1250     -823.21      LOW   

    7499    ALLEN      1600     -473.21      LOW   

    .

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



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