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  
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

Multi-Table Queries with Oracle 10G XE
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 8
    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:
      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


    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


       · This is a continuation of the usage of the query builder tool in Oracle 10g XE....
     

       

    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 2 hosted by Hostway