SlideShare a Scribd company logo
Copyright © 2004, Oracle. All rights reserved.
Displaying Data
from Multiple Tables
Copyright © 2004, Oracle. All rights reserved.
Objectives
After completing this lesson, you should be able to do
the following:
• Write SELECT statements to access data from
more than one table using equijoins and non-
equijoins
• Join a table to itself by using a self-join
• View data that generally does not meet a join
condition by using outer joins
• Generate a Cartesian product of all rows from two
or more tables
Copyright © 2004, Oracle. All rights reserved.
Obtaining Data from Multiple Tables
EMPLOYEES DEPARTMENTS
…
…
Copyright © 2004, Oracle. All rights reserved.
Types of Joins
Joins that are compliant with the SQL:1999 standard
include the following:
• Cross joins
• Natural joins
• USING clause
• Full (or two-sided) outer joins
• Arbitrary join conditions for outer joins
Copyright © 2004, Oracle. All rights reserved.
Joining Tables Using SQL:1999 Syntax
Use a join to query data from more than one table:
SELECT table1.column, table2.column
FROM table1
[NATURAL JOIN table2] |
[JOIN table2 USING (column_name)] |
[JOIN table2
ON (table1.column_name = table2.column_name)]|
[LEFT|RIGHT|FULL OUTER JOIN table2
ON (table1.column_name = table2.column_name)]|
[CROSS JOIN table2];
Copyright © 2004, Oracle. All rights reserved.
Creating Natural Joins
• The NATURAL JOIN clause is based on all columns
in the two tables that have the same name.
• It selects rows from the two tables that have equal
values in all matched columns.
• If the columns having the same names have
different data types, an error is returned.
Copyright © 2004, Oracle. All rights reserved.
SELECT department_id, department_name,
location_id, city
FROM departments
NATURAL JOIN locations ;
Retrieving Records with Natural Joins
Copyright © 2004, Oracle. All rights reserved.
Creating Joins with the USING Clause
• If several columns have the same names but the
data types do not match, the NATURAL JOIN clause
can be modified with the USING clause to specify
the columns that should be used for an equijoin.
• Use the USING clause to match only one column
when more than one column matches.
• Do not use a table name or alias in the referenced
columns.
• The NATURAL JOIN and USING clauses are
mutually exclusive.
Copyright © 2004, Oracle. All rights reserved.
Joining Column Names
EMPLOYEES DEPARTMENTS
Foreign key Primary key
… …
Copyright © 2004, Oracle. All rights reserved.
SELECT employees.employee_id, employees.last_name,
departments.location_id, department_id
FROM employees JOIN departments
USING (department_id) ;
Retrieving Records with the USING Clause
…
Copyright © 2004, Oracle. All rights reserved.
Qualifying Ambiguous
Column Names
• Use table prefixes to qualify column names that
are in multiple tables.
• Use table prefixes to improve performance.
• Use column aliases to distinguish columns that
have identical names but reside in different tables.
• Do not use aliases on columns that are identified
in the USING clause and listed elsewhere in the
SQL statement.
Copyright © 2004, Oracle. All rights reserved.
SELECT e.employee_id, e.last_name,
d.location_id, department_id
FROM employees e JOIN departments d
USING (department_id) ;
Using Table Aliases
• Use table aliases to simplify queries.
• Use table aliases to improve performance.
Copyright © 2004, Oracle. All rights reserved.
Creating Joins with the ON Clause
• The join condition for the natural join is basically
an equijoin of all columns with the same name.
• Use the ON clause to specify arbitrary conditions
or specify columns to join.
• The join condition is separated from other search
conditions.
• The ON clause makes code easy to understand.
Copyright © 2004, Oracle. All rights reserved.
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e JOIN departments d
ON (e.department_id = d.department_id);
Retrieving Records with the ON Clause
…
Copyright © 2004, Oracle. All rights reserved.
Self-Joins Using the ON Clause
MANAGER_ID in the WORKER table is equal to
EMPLOYEE_ID in the MANAGER table.
EMPLOYEES (WORKER) EMPLOYEES (MANAGER)
… …
Copyright © 2004, Oracle. All rights reserved.
Self-Joins Using the ON Clause
SELECT e.last_name emp, m.last_name mgr
FROM employees e JOIN employees m
ON (e.manager_id = m.employee_id);
…
Copyright © 2004, Oracle. All rights reserved.
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e JOIN departments d
ON (e.department_id = d.department_id)
AND e.manager_id = 149 ;
Applying Additional Conditions
to a Join
Copyright © 2004, Oracle. All rights reserved.
SELECT employee_id, city, department_name
FROM employees e
JOIN departments d
ON d.department_id = e.department_id
JOIN locations l
ON d.location_id = l.location_id;
Creating Three-Way Joins with the
ON Clause
…
Copyright © 2004, Oracle. All rights reserved.
Non-Equijoins
EMPLOYEES JOB_GRADES
Salary in the EMPLOYEES
table must be between
lowest salary and highest
salary in the JOB_GRADES
table.
…
Copyright © 2004, Oracle. All rights reserved.
SELECT e.last_name, e.salary, j.grade_level
FROM employees e JOIN job_grades j
ON e.salary
BETWEEN j.lowest_sal AND j.highest_sal;
Retrieving Records
with Non-Equijoins
…
Copyright © 2004, Oracle. All rights reserved.
Outer Joins
EMPLOYEESDEPARTMENTS
There are no employees in
department 190.
…
Copyright © 2004, Oracle. All rights reserved.
INNER Versus OUTER Joins
• In SQL:1999, the join of two tables returning only
matched rows is called an inner join.
• A join between two tables that returns the results
of the inner join as well as the unmatched rows
from the left (or right) tables is called a left (or
right) outer join.
• A join between two tables that returns the results
of an inner join as well as the results of a left and
right join is a full outer join.
Copyright © 2004, Oracle. All rights reserved.
SELECT e.last_name, e.department_id, d.department_name
FROM employees e LEFT OUTER JOIN departments d
ON (e.department_id = d.department_id) ;
LEFT OUTER JOIN
…
Copyright © 2004, Oracle. All rights reserved.
SELECT e.last_name, e.department_id, d.department_name
FROM employees e RIGHT OUTER JOIN departments d
ON (e.department_id = d.department_id) ;
RIGHT OUTER JOIN
…
Copyright © 2004, Oracle. All rights reserved.
SELECT e.last_name, d.department_id, d.department_name
FROM employees e FULL OUTER JOIN departments d
ON (e.department_id = d.department_id) ;
FULL OUTER JOIN
…
Copyright © 2004, Oracle. All rights reserved.
Cartesian Products
• A Cartesian product is formed when:
– A join condition is omitted
– A join condition is invalid
– All rows in the first table are joined to all rows in the
second table
• To avoid a Cartesian product, always include a
valid join condition.
Copyright © 2004, Oracle. All rights reserved.
Generating a Cartesian Product
Cartesian product:
20 x 8 = 160 rows
EMPLOYEES (20 rows) DEPARTMENTS (8 rows)
…
…
Copyright © 2004, Oracle. All rights reserved.
SELECT last_name, department_name
FROM employees
CROSS JOIN departments ;
Creating Cross Joins
• The CROSS JOIN clause produces the cross-
product of two tables.
• This is also called a Cartesian product between
the two tables.
…
Copyright © 2004, Oracle. All rights reserved.
Summary
In this lesson, you should have learned how to use
joins to display data from multiple tables by using:
• Equijoins
• Non-equijoins
• Outer joins
• Self-joins
• Cross joins
• Natural joins
• Full (or two-sided) outer joins
Copyright © 2004, Oracle. All rights reserved.
Practice 5: Overview
This practice covers the following topics:
• Joining tables using an equijoin
• Performing outer and self-joins
• Adding conditions

More Related Content

PPT
Les05 (Displaying Data from Multiple Table)
Achmad Solichin
 
PPT
Les05
Akmal Rony
 
PPT
Les02 (restricting and sorting data)
Achmad Solichin
 
PDF
Oracle SQL Part 3
Gurpreet singh
 
PDF
Oracle SQL Part 2
Gurpreet singh
 
Les05 (Displaying Data from Multiple Table)
Achmad Solichin
 
Les05
Akmal Rony
 
Les02 (restricting and sorting data)
Achmad Solichin
 
Oracle SQL Part 3
Gurpreet singh
 
Oracle SQL Part 2
Gurpreet singh
 

What's hot (20)

PPT
Oracle sql joins
redro
 
PPT
App C
Sudharsan S
 
PDF
Sql joins
Vivek Singh
 
PPT
e computer notes - From multiple tables
ecomputernotes
 
PPT
Les05
Sudharsan S
 
PPTX
Oracle SQL - Grants, filters, groups and more
A Data Guru
 
PPT
ALL ABOUT SQL AND RDBMS
gaurav koriya
 
PPT
Displaying data from multiple tables
Syed Zaid Irshad
 
PPTX
Oracle SQL - Select Part -1 let's write some queries!
A Data Guru
 
PDF
Consultas con agrupaci¾n de datos
Caleb Gutiérrez
 
PPT
SQL Introduction to displaying data from multiple tables
Vibrant Technologies & Computers
 
PPTX
MULTIPLE TABLES
ASHABOOPATHY
 
PDF
Sql Basics | Edureka
Edureka!
 
PPTX
Oracle: Joins
DataminingTools Inc
 
PPTX
Hira
hira elahi
 
DOC
SQL Joins
Paul Harkins
 
PPT
Les09 (using ddl statements to create and manage tables)
Achmad Solichin
 
PPTX
Retrieving Data From A Database
DataminingTools Inc
 
PDF
SQL JOINS
Swapnali Pawar
 
PPTX
Xml part1
NOHA AW
 
Oracle sql joins
redro
 
Sql joins
Vivek Singh
 
e computer notes - From multiple tables
ecomputernotes
 
Oracle SQL - Grants, filters, groups and more
A Data Guru
 
ALL ABOUT SQL AND RDBMS
gaurav koriya
 
Displaying data from multiple tables
Syed Zaid Irshad
 
Oracle SQL - Select Part -1 let's write some queries!
A Data Guru
 
Consultas con agrupaci¾n de datos
Caleb Gutiérrez
 
SQL Introduction to displaying data from multiple tables
Vibrant Technologies & Computers
 
MULTIPLE TABLES
ASHABOOPATHY
 
Sql Basics | Edureka
Edureka!
 
Oracle: Joins
DataminingTools Inc
 
SQL Joins
Paul Harkins
 
Les09 (using ddl statements to create and manage tables)
Achmad Solichin
 
Retrieving Data From A Database
DataminingTools Inc
 
SQL JOINS
Swapnali Pawar
 
Xml part1
NOHA AW
 
Ad

Viewers also liked (19)

DOCX
Tupen 6 1235010002
Abrianto Nugraha
 
DOC
Lapres 4 1235010002
Abrianto Nugraha
 
DOC
Tupen 7 1235010002
Abrianto Nugraha
 
DOC
Lapres 6 1235010002
Abrianto Nugraha
 
DOC
Lapres 7 1235010002
Abrianto Nugraha
 
DOC
Tupen 7 1235010002
Abrianto Nugraha
 
DOC
Tupen 8 1235010002
Abrianto Nugraha
 
DOC
Lapres 2 1235010002
Abrianto Nugraha
 
DOCX
Tupen 8 1235010002
Abrianto Nugraha
 
DOC
Tugas jarkom router 2 1235010002
Abrianto Nugraha
 
DOC
Lapres 5 1235010002
Abrianto Nugraha
 
DOC
Lapres 4 1235010002
Abrianto Nugraha
 
DOC
Lapers 6 1235010002
Abrianto Nugraha
 
DOC
Lapres 2 1235010002
Abrianto Nugraha
 
Tupen 6 1235010002
Abrianto Nugraha
 
Lapres 4 1235010002
Abrianto Nugraha
 
Tupen 7 1235010002
Abrianto Nugraha
 
Lapres 6 1235010002
Abrianto Nugraha
 
Lapres 7 1235010002
Abrianto Nugraha
 
Tupen 7 1235010002
Abrianto Nugraha
 
Tupen 8 1235010002
Abrianto Nugraha
 
Lapres 2 1235010002
Abrianto Nugraha
 
Tupen 8 1235010002
Abrianto Nugraha
 
Tugas jarkom router 2 1235010002
Abrianto Nugraha
 
Lapres 5 1235010002
Abrianto Nugraha
 
Lapres 4 1235010002
Abrianto Nugraha
 
Lapers 6 1235010002
Abrianto Nugraha
 
Lapres 2 1235010002
Abrianto Nugraha
 
Ad

Similar to Les05 (20)

PPT
Les04 Displaying Data from Multiple Tables.ppt
DrZeeshanBhatti
 
PPT
Displaying Data from Multiple Tables - Oracle Data Base
Salman Memon
 
PPT
Database Management Systems SQL And DDL language
HSibghatUllah
 
PPT
Les01 Writing BAsic SQL SELECT Statement.ppt
DrZeeshanBhatti
 
PPT
Writing Basic SQL SELECT Statements
Salman Memon
 
PPT
Les02 Restricting and Sorting Data using SQL.ppt
DrZeeshanBhatti
 
PDF
Les01.pdfdf4eg53wrg4354b106t48rt7t88t78ert78
hanadijad
 
PPT
Restricting and Sorting Data - Oracle Data Base
Salman Memon
 
PPT
SQL, consultas rapidas y sencillas, oracle
marycielocartagena73
 
PPT
Aggregating Data Using Group Functions
Salman Memon
 
PDF
Lesson05 从多表中查询数据
renguzi
 
PPT
Sql oracle
Md.Abu Noman Shuvo
 
PPTX
ADVANCE SQL-"Sub queries"
Ankit Surti
 
PPT
Oracle SQL - Aggregating Data Les 05.ppt
DrZeeshanBhatti
 
PPT
Les04
Akmal Rony
 
PPT
Less07 schema
Imran Ali
 
PPT
Les01
Akmal Rony
 
PPT
plsql Les08
sasa_eldoby
 
Les04 Displaying Data from Multiple Tables.ppt
DrZeeshanBhatti
 
Displaying Data from Multiple Tables - Oracle Data Base
Salman Memon
 
Database Management Systems SQL And DDL language
HSibghatUllah
 
Les01 Writing BAsic SQL SELECT Statement.ppt
DrZeeshanBhatti
 
Writing Basic SQL SELECT Statements
Salman Memon
 
Les02 Restricting and Sorting Data using SQL.ppt
DrZeeshanBhatti
 
Les01.pdfdf4eg53wrg4354b106t48rt7t88t78ert78
hanadijad
 
Restricting and Sorting Data - Oracle Data Base
Salman Memon
 
SQL, consultas rapidas y sencillas, oracle
marycielocartagena73
 
Aggregating Data Using Group Functions
Salman Memon
 
Lesson05 从多表中查询数据
renguzi
 
Sql oracle
Md.Abu Noman Shuvo
 
ADVANCE SQL-"Sub queries"
Ankit Surti
 
Oracle SQL - Aggregating Data Les 05.ppt
DrZeeshanBhatti
 
Les04
Akmal Rony
 
Less07 schema
Imran Ali
 
Les01
Akmal Rony
 
plsql Les08
sasa_eldoby
 

More from Abrianto Nugraha (20)

PPT
Ds sn is-02
Abrianto Nugraha
 
PPT
Ds sn is-01
Abrianto Nugraha
 
PPT
Pertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkap
Abrianto Nugraha
 
PPTX
04 pemodelan spk
Abrianto Nugraha
 
PPTX
02 sistem pengambilan-keputusan_revised
Abrianto Nugraha
 
PPTX
01 pengantar sistem-pendukung_keputusan
Abrianto Nugraha
 
PPT
Pertemuan 7
Abrianto Nugraha
 
PPT
Pertemuan 7 dan_8
Abrianto Nugraha
 
PPT
Pertemuan 5
Abrianto Nugraha
 
PPT
Pertemuan 6
Abrianto Nugraha
 
PPT
Pertemuan 4
Abrianto Nugraha
 
PPT
Pertemuan 3
Abrianto Nugraha
 
PPT
Pertemuan 2
Abrianto Nugraha
 
PPT
Pertemuan 1
Abrianto Nugraha
 
DOCX
Modul 1 mengambil nilai parameter
Abrianto Nugraha
 
DOCX
Modul 3 object oriented programming dalam php
Abrianto Nugraha
 
DOCX
Modul 2 menyimpan ke database
Abrianto Nugraha
 
Ds sn is-02
Abrianto Nugraha
 
Ds sn is-01
Abrianto Nugraha
 
Pertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkap
Abrianto Nugraha
 
04 pemodelan spk
Abrianto Nugraha
 
02 sistem pengambilan-keputusan_revised
Abrianto Nugraha
 
01 pengantar sistem-pendukung_keputusan
Abrianto Nugraha
 
Pertemuan 7
Abrianto Nugraha
 
Pertemuan 7 dan_8
Abrianto Nugraha
 
Pertemuan 5
Abrianto Nugraha
 
Pertemuan 6
Abrianto Nugraha
 
Pertemuan 4
Abrianto Nugraha
 
Pertemuan 3
Abrianto Nugraha
 
Pertemuan 2
Abrianto Nugraha
 
Pertemuan 1
Abrianto Nugraha
 
Modul 1 mengambil nilai parameter
Abrianto Nugraha
 
Modul 3 object oriented programming dalam php
Abrianto Nugraha
 
Modul 2 menyimpan ke database
Abrianto Nugraha
 

Recently uploaded (20)

PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
CDH. pptx
AneetaSharma15
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
CDH. pptx
AneetaSharma15
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Basics and rules of probability with real-life uses
ravatkaran694
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 

Les05

  • 1. Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables
  • 2. Copyright © 2004, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: • Write SELECT statements to access data from more than one table using equijoins and non- equijoins • Join a table to itself by using a self-join • View data that generally does not meet a join condition by using outer joins • Generate a Cartesian product of all rows from two or more tables
  • 3. Copyright © 2004, Oracle. All rights reserved. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
  • 4. Copyright © 2004, Oracle. All rights reserved. Types of Joins Joins that are compliant with the SQL:1999 standard include the following: • Cross joins • Natural joins • USING clause • Full (or two-sided) outer joins • Arbitrary join conditions for outer joins
  • 5. Copyright © 2004, Oracle. All rights reserved. Joining Tables Using SQL:1999 Syntax Use a join to query data from more than one table: SELECT table1.column, table2.column FROM table1 [NATURAL JOIN table2] | [JOIN table2 USING (column_name)] | [JOIN table2 ON (table1.column_name = table2.column_name)]| [LEFT|RIGHT|FULL OUTER JOIN table2 ON (table1.column_name = table2.column_name)]| [CROSS JOIN table2];
  • 6. Copyright © 2004, Oracle. All rights reserved. Creating Natural Joins • The NATURAL JOIN clause is based on all columns in the two tables that have the same name. • It selects rows from the two tables that have equal values in all matched columns. • If the columns having the same names have different data types, an error is returned.
  • 7. Copyright © 2004, Oracle. All rights reserved. SELECT department_id, department_name, location_id, city FROM departments NATURAL JOIN locations ; Retrieving Records with Natural Joins
  • 8. Copyright © 2004, Oracle. All rights reserved. Creating Joins with the USING Clause • If several columns have the same names but the data types do not match, the NATURAL JOIN clause can be modified with the USING clause to specify the columns that should be used for an equijoin. • Use the USING clause to match only one column when more than one column matches. • Do not use a table name or alias in the referenced columns. • The NATURAL JOIN and USING clauses are mutually exclusive.
  • 9. Copyright © 2004, Oracle. All rights reserved. Joining Column Names EMPLOYEES DEPARTMENTS Foreign key Primary key … …
  • 10. Copyright © 2004, Oracle. All rights reserved. SELECT employees.employee_id, employees.last_name, departments.location_id, department_id FROM employees JOIN departments USING (department_id) ; Retrieving Records with the USING Clause …
  • 11. Copyright © 2004, Oracle. All rights reserved. Qualifying Ambiguous Column Names • Use table prefixes to qualify column names that are in multiple tables. • Use table prefixes to improve performance. • Use column aliases to distinguish columns that have identical names but reside in different tables. • Do not use aliases on columns that are identified in the USING clause and listed elsewhere in the SQL statement.
  • 12. Copyright © 2004, Oracle. All rights reserved. SELECT e.employee_id, e.last_name, d.location_id, department_id FROM employees e JOIN departments d USING (department_id) ; Using Table Aliases • Use table aliases to simplify queries. • Use table aliases to improve performance.
  • 13. Copyright © 2004, Oracle. All rights reserved. Creating Joins with the ON Clause • The join condition for the natural join is basically an equijoin of all columns with the same name. • Use the ON clause to specify arbitrary conditions or specify columns to join. • The join condition is separated from other search conditions. • The ON clause makes code easy to understand.
  • 14. Copyright © 2004, Oracle. All rights reserved. SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id FROM employees e JOIN departments d ON (e.department_id = d.department_id); Retrieving Records with the ON Clause …
  • 15. Copyright © 2004, Oracle. All rights reserved. Self-Joins Using the ON Clause MANAGER_ID in the WORKER table is equal to EMPLOYEE_ID in the MANAGER table. EMPLOYEES (WORKER) EMPLOYEES (MANAGER) … …
  • 16. Copyright © 2004, Oracle. All rights reserved. Self-Joins Using the ON Clause SELECT e.last_name emp, m.last_name mgr FROM employees e JOIN employees m ON (e.manager_id = m.employee_id); …
  • 17. Copyright © 2004, Oracle. All rights reserved. SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id FROM employees e JOIN departments d ON (e.department_id = d.department_id) AND e.manager_id = 149 ; Applying Additional Conditions to a Join
  • 18. Copyright © 2004, Oracle. All rights reserved. SELECT employee_id, city, department_name FROM employees e JOIN departments d ON d.department_id = e.department_id JOIN locations l ON d.location_id = l.location_id; Creating Three-Way Joins with the ON Clause …
  • 19. Copyright © 2004, Oracle. All rights reserved. Non-Equijoins EMPLOYEES JOB_GRADES Salary in the EMPLOYEES table must be between lowest salary and highest salary in the JOB_GRADES table. …
  • 20. Copyright © 2004, Oracle. All rights reserved. SELECT e.last_name, e.salary, j.grade_level FROM employees e JOIN job_grades j ON e.salary BETWEEN j.lowest_sal AND j.highest_sal; Retrieving Records with Non-Equijoins …
  • 21. Copyright © 2004, Oracle. All rights reserved. Outer Joins EMPLOYEESDEPARTMENTS There are no employees in department 190. …
  • 22. Copyright © 2004, Oracle. All rights reserved. INNER Versus OUTER Joins • In SQL:1999, the join of two tables returning only matched rows is called an inner join. • A join between two tables that returns the results of the inner join as well as the unmatched rows from the left (or right) tables is called a left (or right) outer join. • A join between two tables that returns the results of an inner join as well as the results of a left and right join is a full outer join.
  • 23. Copyright © 2004, Oracle. All rights reserved. SELECT e.last_name, e.department_id, d.department_name FROM employees e LEFT OUTER JOIN departments d ON (e.department_id = d.department_id) ; LEFT OUTER JOIN …
  • 24. Copyright © 2004, Oracle. All rights reserved. SELECT e.last_name, e.department_id, d.department_name FROM employees e RIGHT OUTER JOIN departments d ON (e.department_id = d.department_id) ; RIGHT OUTER JOIN …
  • 25. Copyright © 2004, Oracle. All rights reserved. SELECT e.last_name, d.department_id, d.department_name FROM employees e FULL OUTER JOIN departments d ON (e.department_id = d.department_id) ; FULL OUTER JOIN …
  • 26. Copyright © 2004, Oracle. All rights reserved. Cartesian Products • A Cartesian product is formed when: – A join condition is omitted – A join condition is invalid – All rows in the first table are joined to all rows in the second table • To avoid a Cartesian product, always include a valid join condition.
  • 27. Copyright © 2004, Oracle. All rights reserved. Generating a Cartesian Product Cartesian product: 20 x 8 = 160 rows EMPLOYEES (20 rows) DEPARTMENTS (8 rows) … …
  • 28. Copyright © 2004, Oracle. All rights reserved. SELECT last_name, department_name FROM employees CROSS JOIN departments ; Creating Cross Joins • The CROSS JOIN clause produces the cross- product of two tables. • This is also called a Cartesian product between the two tables. …
  • 29. Copyright © 2004, Oracle. All rights reserved. Summary In this lesson, you should have learned how to use joins to display data from multiple tables by using: • Equijoins • Non-equijoins • Outer joins • Self-joins • Cross joins • Natural joins • Full (or two-sided) outer joins
  • 30. Copyright © 2004, Oracle. All rights reserved. Practice 5: Overview This practice covers the following topics: • Joining tables using an equijoin • Performing outer and self-joins • Adding conditions