Join Tables

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Join Tables

CMPSC140
Introduction to Joins
• Selects Specific Columns from Multiple Tables
• JOIN keyword specifies that tables are joined and how to join them
• ON keyword specifies join condition

• Queries Two or More Tables to Produce a Result Set


• Use primary and foreign keys as join conditions
• Use columns common to specified tables to join tables
Cross product of the two tables
• Joining two tables without a on clause or specifically without any
condition will lead to the cross product of the two tables
• The output of two cross joined tables without any condition will be n*
m where n and m are number of records in two tables.
Cross product of the two tables
SELECT
SELECT ** FROM
FROM Employees
Employees e,Departments
e,Departments d;
d;

Employees e.eid e.last_name e.dept_id d.dept_id d.dept_name d.location_id


eid last_name dept_id 100 King 90 10 Administration 1700
100 King 90 100 King 90 20 Marketing 1800
101 Kocher 90
100 King 90 90 Accounting 1700
102 Grant
101 Kocher 90 10 Administration 1700
202 Fay 20
101 Kocher 90 20 Marketing 1800
101 Kocher 90 90 Accounting 1700
Departments 102 Grant 10 Administration 1700
dept_id dept_name location_id
102 Grant 20 Marketing 1800
10 Administration 1700
102 Grant 90 Accounting 1700
20 Marketing 1800
90 Accounting 1700 202 Fay 20 10 Administration 1700
202 Fay 20 20 Marketing 1800
202 Fay 20 90 Accounting 1700
INNER and OUTER Joins
• A join of two or more tables that returns only the matched rows is
called an inner join
• When a join returns the unmatched rows as well as the matched
rows, it is called an outer join
• There are three types of outer join
• LEFT, RIGHT and FULL Outer joins
• These names are associated with the order of the table names in the FROM
Clause of the SELECT statement
INNER Join Syntax
SELECT table1.column1, table2.column2...
FROM table1 [INNER] JOIN table2 ON table1.common_field = table2.common_field;
INNER Join
SELECT
SELECT last_name,
last_name, dept_name
dept_name
FROM
FROM Employees
Employees ee INNER
INNER JOIN
JOIN Departments
Departments dd ON
ON e.dept_id
e.dept_id == d.dept_id
d.dept_id

Employees Departments
eid last_name dept_id dept_id dept_name location_id
100 King 90 10 Administration 1700
101 Kocher 90 20 Marketing 1800
102 Grant 90 Accounting 1700
202 Fay 20

last_name dept_name
King Accounting
Kocher Accounting
Fay Marketing
LEFT OUTER Join
• This example returns lastname, dept_id and dept_name for all employees
including those employees not assigned to a department.
SELECT
SELECT e.last_name,
e.last_name, d.dept_id,
d.dept_id, d.dept_name
d.dept_name
FROM
FROM Employees
Employees ee LEFT
LEFT OUTER
OUTER JOIN
JOIN Departments
Departments dd ON
ON e.dept_id
e.dept_id == d.dept_id
d.dept_id

Employees Departments
eid last_name dept_id dept_id dept_name location_id last_name dept_id dept_name
100 King 90 10 Administration 1700 King 90 Accounting
101 Kocher 90 20 Marketing 1800 Kocher 90 Accounting
102 Grant 90 Accounting 1700
Grant NULL NULL
202 Fay 20
Fay 20 Marketing
RIGHT OUTER Join
• This example returns all department IDs ad department names, both those that
have employees assigned to them and those that do not

SELECT
SELECT e.last_name,
e.last_name, d.dept_id,
d.dept_id, d.dept_name
d.dept_name
FROM
FROM Employees
Employees ee RIGHT
RIGHT OUTER
OUTER JOIN
JOIN Departments
Departments dd ON
ON e.dept_id
e.dept_id == d.dept_id
d.dept_id

Employees Departments
eid last_name dept_id dept_id dept_name location_id last_name dept_id dept_name
100 King 90 10 Administration 1700 NULL 10 Administration
101 Kocher 90 20 Marketing 1800 Fay 20 Marketing
102 Grant 90 Accounting 1700 King 90 Accounting
202 Fay 20
Kocher 90 Accounting
FULL OUTER Join
• The result set of a full outer join includes all rows from a left outer join and all
rows from the right outer join combined together without duplication

SELECT
SELECT e.last_name,
e.last_name, d.dept_id,
d.dept_id, d.dept_name
d.dept_name
FROM
FROM Employees
Employees ee FULL
FULL OUTER
OUTER JOIN
JOIN Departments
Departments dd ON
ON e.dept_id
e.dept_id == d.dept_id
d.dept_id

Employees Departments
eid last_name dept_id last_name dept_id dept_name
dept_id dept_name location_id
100 King 90 10 Administration 1700 King 90 Administration
101 Kocher 90 20 Marketing 1800 Kosher 90 Administration
102 Grant 90 Accounting 1700 Grant NULL NULL
202 Fay 20 Fay 20 Marketing
NULL 10 Administration
SELF-JOIN
• A self-join joins data from the same table. In other words, it joins a table with
itself.
• Records taken from the table are matched to other records from the same table. 
• To join a table to itself, the table is given two names or aliases

Employees
eid last_name dept_id manager_id
100 King 90 NULL
101 Kocher 90 100
102 Grant 101
202 Fay 20 102
SELF-JOIN
SELECT e.eid, e.last_name, m.last_name AS boss_lname
FROM employee e INNER JOIN employee m ON e.manager_id = m.eid ;
Employees e
eid last_name dept_id manager_id Employees m
100 King 90 NULL eid last_name dept_id manager_id
101 Kocher 90 100 100 King 90 NULL
102 Grant 101 101 Kocher 90 100
202 Fay 20 102 102 Grant 101

eid last_name Boss_lname


101 Kocher King
102 Grant Kocher
202 Fay Grant
Find employees who work in same department.
SELECT e1.eid, e1.last_name, e2.last_name
FROM employee e1 INNER JOIN employee e2 ON e1.dept_id =
e2.dept_id WHERE e1.eid > e2.eid ;
Employees e
eid last_name dept_id manager_id Employees m
100 King 90 NULL eid last_name dept_id manager_id
101 Kocher 90 100 100 King 90 NULL
102 Grant 101 101 Kocher 90 100
202 Fay 20 102 102 Grant 101

eid last_name lastnme


101 Kocher King
Joining More Than Two Tables
• If you want to join three tables together, how many joins would it
take?
• How many bridges are needed to join three islands?
• To join three tables, you need to add another join condition to the
WHERE clause using the AND operator
Joining More Than Two Tables
• Suppose we need a report of our employees and the city where
their department is located?
• We need to join three tables: employees, departments, and
locations
Joining More Than Two Tables
Employees Departments Locations
eid last_name dept_id dept_id dept_name LOCATION_ID LOCATION_ID CITY
100 King 90 10 Administration 1700 1700 Toronto
101 Kocher 90 20 Marketing 1800 1800 Peoria
102 Grant 90 Accounting 1700
2000 LA
202 Fay 20

SELECT last_name, city


FROM employee e, departments d, location l
WHRER e.dept_id =d.dept_id AND d.location_id = l.location_id;

last_name city
King Toronto
Kocher Toronto
2000 Peoria

You might also like