Sub topic 8
FULL OUTER JOIN
This chapter assumes that you know SELECT, WHERE, table aliases, primary keys, foreign keys, and NULL. A FULL OUTER JOIN combines two tables while preserving every row from both tables. It returns matching rows as combined rows. It also returns unmatched rows, filling the missing table's columns with NULL.
When to use a FULL OUTER JOIN
Use a full outer join when you need to compare two sets and see what exists on either side. Common examples include:
- Comparing current and previous records.
- Finding departments with no employees and employees with no valid department.
- Reconciling two data sources.
- Auditing whether related data is missing in either table.
Suppose employees.department_id refers to departments.department_id. A department may have no employees, and an employee may have a missing or invalid department reference. A full outer join shows both situations.
SELECT e.employee_id,
e.last_name,
e.department_id AS employee_department_id,
d.department_name,
d.department_id AS department_table_id
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id
ORDER BY COALESCE(e.department_id, d.department_id);COALESCE returns the first non-NULL value in its argument list. Here, it displays the department ID from whichever table supplied the row.
| Employee row | Department row | Result |
|---|---|---|
| Matching | Matching | One combined row |
| Present | Missing | Employee columns plus `NULL` department columns |
| Missing | Present | `NULL` employee columns plus department columns |
How matching works
Oracle evaluates the join condition in the ON clause. For matching department IDs, it produces one result row containing columns from both tables.
For an employee without a matching department, Oracle keeps the employee row and sets the department columns to NULL. For a department without employees, Oracle keeps the department row and sets the employee columns to NULL.
Unlike an inner join, a full outer join does not discard unmatched rows. Unlike a left or right outer join, it does not choose only one table's unmatched rows.
Filtering a full outer join
A condition in WHERE runs after the join result has been formed. Because unmatched rows contain NULL, a filter can remove them.
For example, this query removes departments without employees because e.employee_id is NULL for those rows:
SELECT e.last_name,
d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id
WHERE e.employee_id IS NOT NULL;It also removes employee-only rows if the employee ID is always non-NULL. The result is therefore no longer a complete reconciliation.
To retain unmatched rows while restricting matched employees, put the employee condition in the ON clause:
SELECT e.last_name,
d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id
AND e.salary > 10000;This condition changes which employee rows match departments. Rows that do not match remain in the result, with NULL values on the other side. Always test the placement of filters when preserving unmatched data matters.
Limits of the older `(+)` syntax
Oracle's legacy outer join operator cannot express a full outer join directly in one join condition. The operator marks the table that may lack a matching row, so it can represent a left or right outer join:
SELECT e.last_name,
d.department_name
FROM employees e, departments d
WHERE e.department_id(+) = d.department_id;For a full outer join, use ANSI syntax. It states that both sides are preserved and avoids the restrictions and confusing condition placement associated with (+).
Exercise
Which join should you use to retain every row from both employees and departments, including rows with no match?
Show answer
Use FULL OUTER JOIN with an ON condition. It preserves unmatched rows from both tables.