Sub topic 8
FULL OUTER JOIN
This chapter assumes you can write inner and LEFT/RIGHT outer joins with ANSI JOIN ... ON, that you know what NULL means in a comparison, and that you have used table aliases. The examples use the employees and departments tables from the Oracle HR sample schema.
What a full outer join returns
A LEFT OUTER JOIN keeps every row from the left table. A RIGHT OUTER JOIN keeps every row from the right table. A FULL OUTER JOIN keeps every row from both tables. The result has three groups of rows:
- Matched rows: pairs where the
ONcondition is true. - Left-only rows: rows from the left table with no match. The right table's columns are NULL.
- Right-only rows: rows from the right table with no match. The left table's columns are NULL.
SELECT e.last_name, d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id;In the HR schema this query returns:
- Every employee who works in a department, paired with the department name.
- Any employee whose
department_idis NULL, withdepartment_nameNULL. The HR sample data contains one such employee. - Any department with no employees, with
last_nameNULL. The HR sample data has several of these.
OUTER is optional. FULL JOIN means the same as FULL OUTER JOIN.
Finding the rows that do not match
A common use of a full outer join is reconciliation: comparing two tables to see what exists on one side only. Because the unmatched side is NULL-extended, you can test a column that is never NULL in its own table, usually the primary key.
SELECT e.employee_id, e.last_name, d.department_id, d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id
WHERE e.employee_id IS NULL
OR d.department_id IS NULL;The WHERE clause keeps only the left-only and right-only rows:
e.employee_id IS NULLis true only for a department with no employees.employee_idis the primary key, so it is never NULL in a real row.d.department_id IS NULLis true only for an employee with no matching department.
This is the correct use of WHERE after an outer join: it filters the finished result, so you want it to act on the NULL-extended rows. Pick a primary key column for the test. If you test a nullable column such as e.department_id, you cannot tell a NULL-extended row from a real row whose value happens to be NULL.
Why (+) cannot express it
The legacy (+) operator marks the side of a join that may be missing rows. A full outer join needs both sides to be optional, and Oracle does not allow that:
SELECT e.last_name, d.department_name
FROM employees e, departments d
WHERE e.department_id(+) = d.department_id(+);Oracle rejects this with ORA-01468: a predicate may reference only one outer-joined table. The (+) operator can make one table optional in a given join condition, so a single (+) join gives a left or right outer join and never a full one. The chapter on (+) restrictions covers the other limits. For now, remember that if you need a full outer join, you need ANSI syntax.
Old code sometimes works around this with a UNION of two outer joins:
SELECT e.last_name, d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id(+)
UNION
SELECT e.last_name, d.department_name
FROM employees e, departments d
WHERE e.department_id(+) = d.department_id;The first query keeps all employees. The second keeps all departments. UNION combines them and removes the duplicate matched rows. This works, but it has costs:
- It reads each table twice.
UNIONremoves duplicates by comparing whole rows. If your real data has two identical output rows, for example two employees with the same last name in the same department,UNIONcollapses them and the result differs from a true full outer join.UNION ALLwould keep duplicates but would then repeat the matched rows.- It is longer and easier to get wrong than one
FULL OUTER JOIN.
Prefer the ANSI form.
Conditions in ON and WHERE
The ON-versus-WHERE rule from the earlier chapter applies on both sides of a full outer join. A condition in ON decides which rows match. A condition in WHERE removes rows from the finished result.
Put a salary filter in ON:
SELECT e.last_name, e.salary, d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id
AND e.salary > 10000;This does not discard anyone. Every employee still appears. An employee earning 10000 or less no longer matches any department, so that employee shows up as a left-only row with a NULL department name. Every department also still appears. A department's lower earners now appear on their own rows, with NULL in the department columns, and the department itself shows up as a right-only row if it has no high earners.
Put the same filter in WHERE:
SELECT e.last_name, e.salary, d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id
WHERE e.salary > 10000;Now the join runs first, with all matches. Then WHERE removes every row where e.salary is NULL or 10000 or less. That removes all right-only rows, because their salary is NULL. The result is effectively a left outer join restricted to high earners, and the "full" part is lost.
| Filter location | Effect on unmatched rows from the left table | Effect on unmatched rows from the right table |
|---|---|---|
| `ON` | Kept, with NULLs on the right | Kept, with NULLs on the left |
| `WHERE` on a left column | Filtered by the condition | Removed if the condition fails on NULL |
| `WHERE` on a right column | Removed if the condition fails on NULL | Filtered by the condition |
Filtering one side before the join
If you want the filter to apply to the table itself, not to the match, use an inline view. The inline view reduces the table first, and then the full join keeps every row of the reduced table.
SELECT e.last_name, e.salary, d.department_name
FROM (SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE salary > 10000) e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id;Here only high earners take part in the join. Departments with no high earner appear as right-only rows. Low earners do not appear anywhere, which differs from the ON version above where they still appeared as left-only rows.
Combining columns from both sides
In the result of a full outer join, the join column is NULL on whichever side lacked a match. If you want one column that shows the key whichever side had it, use COALESCE:
SELECT COALESCE(e.department_id, d.department_id) AS department_id,
e.last_name,
d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id
ORDER BY department_id;With USING, Oracle does this for you. The shared column is returned once, and its value comes from whichever side has it:
SELECT department_id, e.last_name, d.department_name
FROM employees e
FULL OUTER JOIN departments d USING (department_id)
ORDER BY department_id;Do not qualify department_id with an alias when it is the USING column. Oracle raises ORA-25154: column part of USING clause cannot have qualifier.
Counting and aggregating
A full outer join with aggregates needs care, because the NULL-extended rows are real rows in the result. COUNT(*) counts them. COUNT(column) skips NULLs.
SELECT d.department_name,
COUNT(e.employee_id) AS employee_count
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id
GROUP BY d.department_name
ORDER BY d.department_name;- A department with no employees gets a count of 0, because
COUNT(e.employee_id)ignores the NULL. - Employees with no department are grouped under a NULL
department_name, and they get their own group. COUNT(*)here would return 1 for an empty department, because the row exists with NULL employee columns. Count a non-null column from the table whose rows you want to count.
Edge cases
- NULL keys never match. If
e.department_idis NULL, the conditione.department_id = d.department_idis not true for any department, so the employee appears as a left-only row. Two NULLs do not match each other either. - Duplicate keys multiply rows. If the right table had two rows for the same key, each matching left row would appear twice. A full outer join does not remove this multiplication. It only adds the unmatched rows.
- Chained full joins need care. In
A FULL JOIN B ON ... FULL JOIN C ON ..., the secondONcondition is tested against the result of the first join. If a row came only fromC, columns fromAandBare NULL. If your second condition refers toA's column, a row fromBalone with a NULL in that column cannot matchC. UseCOALESCEover the earlier tables' key columns when you chain. - Ordering is not guaranteed. Without
ORDER BY, the order of matched, left-only and right-only rows is not defined. Do not rely on unmatched rows appearing last. - Performance. Oracle can run a full outer join as a hash join that also tracks unmatched rows from both inputs. Expect a full scan of both inputs, since every row is needed. Indexes help little for a plain full join.
Choosing between join types
| You want | Use |
|---|---|
| Only rows that match | `INNER JOIN` |
| All rows from one table, matches from the other | `LEFT` or `RIGHT OUTER JOIN` |
| Every row from both tables | `FULL OUTER JOIN` |
| To list only the rows that do not match | `FULL OUTER JOIN` with `WHERE ... IS NULL` on a key from each side, or an anti-join with `NOT EXISTS` |
Exercise
A query uses employees e FULL OUTER JOIN departments d ON e.department_id = d.department_id WHERE d.location_id = 1700. A colleague expects the output to include employees who have no department. Will it? Explain, and show how to apply the location filter to departments while still keeping every employee and every department.
Show answer
No. Employees with no department are left-only rows, so d.location_id is NULL on them. The condition d.location_id = 1700 is not true for NULL, so WHERE removes them. It also removes every department outside location 1700 and every department-less row, so the result is no longer a full outer join. To filter departments but keep all rows, put the condition in ON: ON e.department_id = d.department_id AND d.location_id = 1700. Departments outside 1700 then fail to match, so they appear as right-only rows and their employees appear as left-only rows. If you want departments outside 1700 to disappear completely, filter an inline view of departments first, and note that their employees then appear as left-only rows with NULL department columns.
Exercise
Write a query that lists only the employees with no department and the departments with no employees, in one result, using a full outer join.
Show answer
SELECT e.employeeid, e.lastname, d.departmentid, d.departmentname FROM employees e FULL OUTER JOIN departments d ON e.departmentid = d.departmentid WHERE e.employeeid IS NULL OR d.departmentid IS NULL; The tests use primary key columns, so a NULL can only come from a missing match.