Oracle
  Home arrow Oracle arrow Page 4 - 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 
Moblin 
JMSL Numerical Library 
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: 4 stars4 stars4 stars4 stars4 stars / 37
    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
     
     
    ADVERTISEMENT


    Inserting Sub-Queries in SELECT Statements in Oracle - Derived tables (or inline views) with Sub-Queries in Oracle


    (Page 4 of 6 )

    Can we write sub-queries in (or as part of) the FROM clause? The answer is YES and you can use it very efficiently in some scenarios. 

    Let us consider the following statement:

    SELECT empno,ename,sal,sal*12 AS AnnSal
    FROM emp
    WHERE AnnSal > 30000

    If I execute the above statement, it will result in an error saying that “AnnSal” is an “Invalid Identifier.”  The reason is that “AnnSal” is not a column existing in the table “emp.”  It is simply an alias (or some reasonable name). We are not allowed to work with a column alias in any of the conditions present in WHERE clause.

    Let us modify the above statement to make it work. Try the following now:

    SELECT empno,ename,sal,AnnSal
    FROM (
          SELECT empno,ename,sal,sal*12 AS AnnSal
          FROM emp
          )
    WHERE AnnSal > 30000

    The above statement is totally different from the previous one. Within the above statement, the outer query doesn’t rely on any specific physical table. The output (or result set) of the inner query is considered as a table for the outer query! The inner query is very similar to that of a view which doesn’t have any physical view name, and it gets created and destroyed on the fly.

    So, according to the inner query, it retrieves four columns (empno, ename, sal, AnnSal). The outer query can work with all four columns as if they are directly from a physical table. 

    As you are trying to define/derive your own table of information from an existing physical table, you call it as a derived table (or inline view). Even the derived tables can be nested to any number of levels with further sub-derived tables as part of FROM clauses.

    More Oracle Articles
    More By Jagadish Chatarji


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

       

    ORACLE ARTICLES

    - 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...
    - Sub-templates and More with Oracle HTML DB





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