Professional Documents
Culture Documents
Join Tables
Join Tables
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
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
last_name city
King Toronto
Kocher Toronto
2000 Peoria