Oracle
  Home arrow Oracle arrow Page 5 - Inserting Sub-Queries in SELECT Statem...
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 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 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

Inserting Sub-Queries in SELECT Statements in Oracle
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 36
    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:
      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
     
    IBM developerWorks
     
    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

    Inserting Sub-Queries in SELECT Statements in Oracle - Sub-Queries with CASE structure in Oracle SELECT statements


    (Page 5 of 6 )

    Now let us go through an interesting topic on CASE structure.  Let us see how a CASE structure works.  Consider the following statement:

    SELECT
      empno,
      ename,
      CASE job
        WHEN 'SALESMAN' THEN 'SALES'
        WHEN 'MANAGER' THEN 'MGMT'
        ELSE job
      END AS jobfunction
      sal
    FROM emp

    When the above query is executed, it returns four columns (empno, ename, jobfunction, sal). The only eye-catching issue from the above is the following structure:

      CASE job
        WHEN 'SALESMAN' THEN 'SALES'
        WHEN 'MANAGER' THEN 'MGMT'
        ELSE job
      END AS jobfunction

    The above dictates something very similar to the following:

    • If the value of “job” is “salesman” return “sales.”
    • If the above condition fails and if the value of “job” is “manager” return “mgmt.”
    • If both of the above conditions fail then return the same value of “job.”
    • All the values must be returned in a new column with the heading “jobfunction.”

    You need to observe that I specified the column (job) along with CASE. The conditions of WHEN work with the values available within that column. We can even work with different relational (and SQL) operators within the WHEN condition as shown in the following example:

    SELECT
      empno,
      ename,
      CASE 
        WHEN comm IS NULL OR comm=0 THEN '-NA-'
        ELSE TO_CHAR(comm)
      END AS comm,
      sal
    FROM emp

    In the above case, the conditions are directly used within the WHEN statement and you need not specify any column with the CASE.

    Finally, you can even work with sub-queries within the CASE structure as follows:

    SELECT
      empno,
      ename,
      CASE 
        WHEN sal >= (SELECT avg(sal) FROM emp) THEN 'HIGH'
        ELSE 'LOW'
      END AS pay,
      sal
    FROM emp

    The above returns a new column named “pay,” which contains either “HIGH” or “LOW” based on their salary compared to the average salary of all employees.

    More Oracle Articles
    More By Jagadish Chatarji


       · Hello guys! This is my second contribution on working with Sub-Queries in Oracle. ...
     

       

    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

    SlickEdit




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway