Oracle
  Home arrow Oracle arrow Page 2 - 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? 
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 returning single and multiple values in Oracle
    ( Page 2 of 6 )

    Let us consider the following statement:

    SELECT
    *
    FROM emp
    WHERE sal = 800

    When you execute the above statement, the condition in the WHERE clause works with only a single value.  Let us rewrite the above statement with a plain sub-query as follows:

    SELECT
    *
    FROM emp
    WHERE sal = (SELECT MIN(sal) FROM emp)

    From the above statement you can understand that the sub-query returns one and only one value (or single value) and the query works fine.

    If we would like to work with more than one value in a condition (of equality), you may need to work something like the following:

    SELECT
    *
    FROM emp
    WHERE empno IN (7902,7788)

    Let us frame the above with a sub-query as follows:

    SELECT

    *

    FROM emp

    WHERE empno IN

    (SELECT empno FROM emp

           WHERE sal = (SELECT MAX(sal) FROM EMP

                        WHERE sal < (SELECT MAX(sal) FROM emp)))

    The above query gives us all the employees earning the second highest salary!  In the above case, the second level sub-query returns more than one value (or two employee numbers).  Therefore, I provided the IN operator as part of the condition.  If you replace the IN with “=” in the above query, it will return with an error stating “single row sub-query returns more than one row.”  When you receive such an error, try to replace “=” with “IN” wherever appropriate.



     
     
    >>> 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 1 Hosted by Hostway
    Stay green...Green IT