Sub topic 3
Keeping Unmatched Rows with LEFT and RIGHT OUTER JOIN
This chapter assumes you know SELECT, WHERE, ORDER BY, table aliases, primary keys, foreign keys, and NULL. It also assumes you can read an inner join. An outer join is useful when the rows without a match matter.
What an outer join does
An inner join returns only rows that match in both tables. An outer join also returns unmatched rows from one or both tables. Oracle fills the missing side with NULL.
The table whose rows must all remain is the preserved table:
LEFT OUTER JOINpreserves every row from the table on the left.RIGHT OUTER JOINpreserves every row from the table on the right.
Consider these relationships:
| Table | Meaning |
|---|---|
| `departments` | A department can exist without employees |
| `employees` | An employee belongs to a department |
To list every department, including empty departments, preserve departments:
SELECT d.department_id,
d.department_name,
e.employee_id,
e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id
ORDER BY d.department_id, e.employee_id;If a department has no employees, the department columns still appear. The employee columns contain NULL.
A join condition in ON describes how rows match. It does not remove a preserved row merely because no match exists.
Choosing LEFT or RIGHT
LEFT and RIGHT describe the position of the preserved table, not the direction of the relationship.
These two queries return the same rows:
SELECT d.department_name, e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id;SELECT d.department_name, e.last_name
FROM employees e
RIGHT OUTER JOIN departments d
ON e.department_id = d.department_id;The first form is usually easier to read because the preserved table appears first. You can rewrite a right join as a left join by swapping the table positions.
Use a left join when the business question sounds like “show every row from this first table.” For example:
- Show every department and its employees.
- Show every customer, including customers with no orders.
- Show every product, including products never sold.
A common error: filtering away the unmatched rows
Suppose you want every department but only employees earning more than 10000. Put the employee condition in ON:
SELECT d.department_name, e.last_name, e.salary
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id
AND e.salary > 10000;This keeps departments with no high-earning employees. Their employee columns are NULL.
If you put the same condition in WHERE, Oracle filters the result after the outer join:
SELECT d.department_name, e.last_name, e.salary
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id
WHERE e.salary > 10000;For an unmatched department, e.salary is NULL. The expression NULL > 10000 is not true, so the row is removed. The query therefore behaves like an inner join for this condition.
Check the result deliberately
Before writing an outer join, identify:
- Which table must have every row represented?
- Which table is optional?
- Which columns should become
NULLwhen no match exists? - Do additional conditions belong in
ONor inWHERE?
Exercise
You need every department, but only employees whose last name starts with "S". Where should the employee name condition go if empty departments must remain?
Show answer
Put the condition in the ON clause: LEFT OUTER JOIN employees e ON e.departmentid = d.departmentid AND e.last_name LIKE 'S%'. A WHERE condition would remove departments with no matching employee.