7
Displaying Data from Multiple Tables
Using Joins
Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Course Roadmap
Lesson 1: Introduction Lesson 6: Reporting Aggregated Data Using
Group Functions
Unit 1: Retrieving, Restricting, Lesson 7: Displaying Data from Multiple
and Sorting Data Tables Using Joins
Unit 2: Joins, Subqueries, and Lesson 8: Using Subqueries to Solve You are here!
Set Operators Queries
Unit 3: DML and DDL Lesson 9: Using Set Operators
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7-2
Objectives
After completing this lesson, you should be able to do the following:
• Write SELECT statements to access data from more than one table by using equijoins
and nonequijoins
• 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 © 2017, Oracle and/or its affiliates. All rights reserved. 7-3
Lesson Agenda
• Types of JOINS and their syntax
• Natural join
• Join with the USING clause
• Join with the ON clause
• Self-join
• Nonequijoins
• OUTER join:
– LEFT OUTER JOIN
– RIGHT OUTER JOIN
– FULL OUTER JOIN
• Cartesian product
– Cross join
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7-4
Why Join? HR Application
Select the desired columns:
EMPLOYEE
first_name > employee_id
last_name >> job_id
I want the information of all
…
employees and their jobs. But
they are stored in different JOBS
tables. How to I combine them
min_salary > job_id
into a single report?
max_salary >> job_title
… GO
Combines columns
from both the tables HR Application
Emp_ID Job_id Job_title
206 AC_ACCOUNTANT Public Accountant
Jody
103 AC_MGR Accounting
Manager
100 AD_PRES President
…
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7-5
Obtaining Data from Multiple Tables
EMPLOYEES JOBS
…
…
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7-6
Types of Joins
Joins that are compliant with the SQL:1999 standard include the following:
• Natural join with the NATURAL JOIN clause
• Join with the USING clause
• Join with the ON clause
• OUTER joins:
– LEFT OUTER JOIN
– RIGHT OUTER JOIN
– FULL OUTER JOIN
• Cross joins
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7-7
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 © 2017, Oracle and/or its affiliates. All rights reserved. 7-8
Lesson Agenda
• Types of JOINS and their syntax
• Natural join
• Join with the USING clause
• Join with the ON clause
• Self-join
• Nonequijoins
• OUTER join:
– LEFT OUTER JOIN
– RIGHT OUTER JOIN
– FULL OUTER JOIN
• Cartesian product
– Cross join
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7-9
Creating Natural Joins
• The NATURAL JOIN clause is based on all the columns that have the same name in two
tables.
• 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.
SELECT * FROM table1 NATURAL JOIN table2;
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 10
Retrieving Records with Natural Joins
SELECT employee_id, first_name, job_id, job_title
from employees NATURAL JOIN jobs;
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 11
Creating Joins with the USING Clause
When should you use the USING clause?
• If several columns have the same names but the data types do not match, use the
USING clause to specify the columns for the equijoin.
• Use the USING clause to match only one column when more than one column matches.
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 12
Joining Column Names
EMPLOYEES DEPARTMENTS
Foreign key Primary key
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 13
Retrieving Records with the USING Clause
SELECT employee_id, last_name,
location_id, department_id
FROM employees JOIN departments
USING (department_id) ;
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 14
Qualifying Ambiguous Column Names
• Use table prefixes to:
– Qualify column names that are in multiple tables
– Increase the speed of parsing of a statement
• Instead of full table name prefixes, use table aliases.
• Table alias gives a table a shorter name:
– Keeps SQL code smaller, uses less memory
• Use column aliases to distinguish columns that have identical
names, but reside in different tables.
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 15
Using Table Aliases with the USING Clause
• Do not qualify a column that is used in the NATURAL join or a join with a USING clause.
• If the same column is used elsewhere in the SQL statement, do not alias it.
SELECT l.city, d.department_name
FROM locations l JOIN departments d
USING (location_id)
WHERE d.location_id = 1400;
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 16
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 the columns to join.
• Use the ON clause to separate the join condition from other search conditions.
• The ON clause makes code easy to understand.
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 17
Retrieving Records with the ON Clause
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);
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 18
Creating Three-Way Joins
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;
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 19
Applying Additional Conditions to a Join
Use the AND clause or the WHERE clause to apply additional conditions:
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 ;
OR
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)
WHERE e.manager_id = 149 ;
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 20
Lesson Agenda
• Types of JOINS and their syntax
• Natural join
• Join with the USING clause
• Join with the ON clause
• Self-join
• Nonequijoins
• OUTER join:
– LEFT OUTER JOIN
– RIGHT OUTER JOIN
– FULL OUTER JOIN
• Cartesian product
– Cross join
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 21
Joining a Table to Itself
EMPLOYEES (WORKER) EMPLOYEES (MANAGER)
… …
MANAGER_ID in the WORKER table is equal to
EMPLOYEE_ID in the MANAGER table.
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 22
Self-Joins Using the ON Clause
SELECT worker.last_name emp, manager.last_name mgr
FROM employees worker JOIN employees manager
ON (worker.manager_id = manager.employee_id);
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 23
Lesson Agenda
• Types of JOINS and their syntax
• Natural join
• Join with the USING clause
• Join with the ON clause
• Self-join
• Nonequijoins
• OUTER join:
– LEFT OUTER JOIN
– RIGHT OUTER JOIN
– FULL OUTER JOIN
• Cartesian product
– Cross join
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 24
Nonequijoins
EMPLOYEES JOB_GRADES
The JOB_GRADES table defines the
LOWEST_SAL and HIGHEST_SAL range of
values for each GRADE_LEVEL.
…
Therefore, the GRADE_LEVEL column can be used to assign grades to each
employee based on his salary.
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 25
Retrieving Records with Nonequijoins
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;
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 26
Lesson Agenda
• Types of JOINS and their syntax
• Natural join
• Join with the USING clause
• Join with the ON clause
• Self-join
• Nonequijoins
• OUTER join:
– LEFT OUTER JOIN
– RIGHT OUTER JOIN
– FULL OUTER JOIN
• Cartesian product
– Cross join
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 27
Returning Records with No Direct Match Using OUTER Joins
DEPARTMENTS Equijoin with EMPLOYEES
There are no employees in …
department 190.
Employee “Grant” has
not been assigned a
department ID.
Therefore, the above two records do not appear in the
equijoin result.
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 28
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) table 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.
A B AB
(Matched &
Unmatched rows)
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 29
LEFT OUTER JOIN
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) ;
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 30
RIGHT OUTER JOIN
SELECT e.last_name, d.department_id, d.department_name
FROM employees e RIGHT OUTER JOIN departments d
ON (e.department_id = d.department_id) ;
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 31
FULL OUTER JOIN
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) ;
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 32
Lesson Agenda
• Types of JOINS and their syntax
• Natural join
• Join with the USING clause
• Join with the ON clause
• Self-join
• Nonequijoins
• OUTER join:
– LEFT OUTER JOIN
– RIGHT OUTER JOIN
– FULL OUTER JOIN
• Cartesian product
– Cross join
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 33
Cartesian Products
A Cartesian product:
• Is a join of every row of one table to every row of another table
• Generates a large number of rows and the result is rarely useful
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 34
Generating a Cartesian Product
EMPLOYEES (20 rows) DEPARTMENTS (8 rows)
Cartesian product:
20 x 8 = 160 rows
…
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 35
Creating Cross Joins
• A CROSS JOIN is a JOIN operation that produces a Cartesian product of two tables.
• To create a Cartesian product, specify CROSS JOIN in your SELECT statement.
SELECT last_name, department_name
FROM employees
CROSS JOIN departments ;
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 36
Quiz Q
If you join a table to itself, what kind of join are you using?
a. Nonequijoin
b. Left OUTER join
c. Right OUTER join
d. Full OUTER join
e. Self-join
f. Natural join
g. Cartesian products
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 37
Summary
In this lesson, you should have learned how to :
• Write SELECT statements to access data from more than one table using equijoins and
nonequijoins
• 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 tables
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 38
Practice 7: Overview
This practice covers the following topics:
• Joining tables using an equijoin
• Performing outer and self-joins
• Adding conditions
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7 - 39