Oracle
  Home arrow Oracle arrow Page 4 - Multi-Table Queries with Oracle 10G XE
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

Multi-Table Queries with Oracle 10G XE
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2006-11-29


    Table of Contents:
  • Multi-Table Queries with Oracle 10G XE
  • Example 1. Single Table queries
  • Example 2. Two Tables: Join Employees and Departments tables
  • Example 3. Three tables: Employees, Departments, and Locations
  • Example 4. Four tables: Employees, Departments, Locations and Countries

  • 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


    Multi-Table Queries with Oracle 10G XE - Example 3. Three tables: Employees, Departments, and Locations
    ( Page 4 of 5 )

    In the case of three or more tables, Oracle joins two tables based on the join condition. The result of this will be joined with the third tables based on a different join condition as shown in the next listing. In the first join, it used the common column DEPARTMENT_ID; in joining the result with the Location tables it makes use of the common column LOCATION_ID as shown in the diagram.

    select	 "EMPLOYEES"."EMPLOYEE_ID" as "EMPLOYEE_ID",
    	 "EMPLOYEES"."FIRST_NAME" as "FIRST_NAME",
    	 "EMPLOYEES"."LAST_NAME" as "LAST_NAME",
    	 "EMPLOYEES"."SALARY" as "SALARY",
    	 "DEPARTMENTS"."DEPARTMENT_NAME" as "DEPARTMENT_NAME",
    	 "LOCATIONS"."POSTAL_CODE" as "POSTAL_CODE",
    	 "LOCATIONS"."CITY" as "CITY" 
     from	 "LOCATIONS" "LOCATIONS",
    	 "DEPARTMENTS" "DEPARTMENTS",
    	 "EMPLOYEES" "EMPLOYEES" 
     where "EMPLOYEES"."DEPARTMENT_ID"="DEPARTMENTS"."DEPARTMENT_ID" 
    and "DEPARTMENTS"."LOCATION_ID"="LOCATIONS"."LOCATION_ID" and
    "EMPLOYEES"."SALARY" > 9000

    The amount of text to be typed for queries can be greatly reduced by using table and column aliases. Aliases make the code easier to read. The result of either of these yields the same result shown in the next picture. In the present case all the tables are from the same schema. When they come from different schemas, the tables have to be referred to together with the schema name, as schemaName.tableName. For referring to the columns in this case, you prefix the table and schema names, schemaName.tableName.columnName, provided the table name has been defined with its schema name. However, when the table alias is used, it is wrong to use the column name with the table name.

    In the following query the letters e, d, and l (aliases) stand respectively for the tables "EMPLOYEES," "DEPARTMENTS" and "LOCATIONS."

     

     select e."EMPLOYEE_ID" as "EMPLOYEE_ID",
    	 e."FIRST_NAME" as "FIRST_NAME",
    	 e."LAST_NAME" as "LAST_NAME",
    	 e."SALARY" as "SALARY",
    	 d."DEPARTMENT_NAME" as "DEPARTMENT_NAME",
    	 l."POSTAL_CODE" as "POSTAL_CODE",
    	 l."CITY" as "CITY" 
     from	 "LOCATIONS" l,
    	 "DEPARTMENTS" d,
    	 "EMPLOYEES" e 
     where  e."DEPARTMENT_ID"=d."DEPARTMENT_ID"
     and	d."LOCATION_ID"=l."LOCATION_ID"
      and 	 e."SALARY" > 9000
    



     
     
    >>> More Oracle Articles          >>> More By Jayaram Krishnaswamy
     

       

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