Sub topic 7
Joining Three or More Tables
Prerequisites and the working model
This chapter assumes that you can write SELECT and WHERE clauses, understand primary and foreign keys, use table aliases, and understand NULL.
A multi-table join combines rows from more than two tables. Each JOIN ... ON clause adds one table to the result produced so far. The ON condition must describe how the new table relates to the existing result.
For example:
employees.department_idrefers to a department.departments.location_idrefers to a location.- The three tables form a chain: employees → departments → locations.
Building an inner join step by step
An inner join returns only rows that have a match at every join step. The following query first matches employees to departments, then matches those departments to locations.
SELECT e.employee_id,
e.last_name,
d.department_name,
l.city
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
JOIN locations l
ON d.location_id = l.location_id
ORDER BY e.employee_id;The aliases make the relationships clear:
eidentifies columns fromemployees.didentifies columns fromdepartments.lidentifies columns fromlocations.
The second ON clause can refer to d, because departments is already part of the intermediate result. It does not need to join directly to employees.
A useful way to read the query is:
- Start with rows from
employees. - Keep employees whose
department_idmatchesdepartments.department_id. - For each matched department, find its matching location.
- Keep only rows with a location match.
If an employee has no department, or the department has no matching location, that employee disappears from this inner-join result.
Choosing join types at each step
Each join can have its own type. Use an outer join when a missing match must not remove the row from the table you want to preserve.
| Requirement | Join pattern |
|---|---|
| Return only complete employee, department, and location matches | `JOIN` at both steps |
| Keep every department, even an empty department | Start with `departments`, then `LEFT JOIN employees` |
| Keep every employee, even one without a department | Start with `employees`, then `LEFT JOIN departments` |
To list every department and its employees, including departments with no employees, put departments on the preserved side:
SELECT d.department_name,
l.city,
e.last_name
FROM departments d
LEFT JOIN locations l
ON d.location_id = l.location_id
LEFT JOIN employees e
ON e.department_id = d.department_id
ORDER BY d.department_name, e.last_name;Both joins are left joins, so a department remains in the result when either its location or its employees are missing. Oracle supplies NULL for the missing table's columns.
A condition that belongs to a particular relationship should normally be placed in that join's ON clause. Moving such a condition to WHERE can remove the NULL-extended rows and change an outer join into inner-join behavior.
Avoiding row multiplication
A join does not guarantee one output row per row in the first table. If one department has 20 employees, joining that department to employees produces 20 rows. Adding another one-to-many table can multiply rows again.
For example, joining departments to employees and projects may produce one row for every employee-project combination. This is correct when that combination is what you need, but it is wrong if you expect one row per department.
Before adding a table, ask:
- Is the relationship one-to-one, one-to-many, or many-to-many?
- What should one output row represent?
- Could a
NULLforeign key prevent a match? - Do I need aggregation before joining?
Exercise
Exercise
Write a query that returns every department name, its location city, and employee last name. Keep departments that have no employees.
Show answer
SELECT d.departmentname, l.city, e.lastname FROM departments d LEFT JOIN locations l ON d.locationid = l.locationid LEFT JOIN employees e ON e.departmentid = d.departmentid;