Sub topic 12
FULL OUTER JOIN on Databases Without It
Prerequisites and the goal
This chapter assumes that you know SELECT, WHERE, INNER JOIN, LEFT JOIN, and NULL. You should also know that a primary key cannot be NULL.
A FULL OUTER JOIN returns:
- Every matching pair.
- Every unmatched row from the left table.
- Every unmatched row from the right table.
Some databases, including MySQL, do not support FULL OUTER JOIN directly. You can reproduce it by combining two queries with UNION ALL.
Build the two halves
Suppose customers contains Ada, Ben, and Cleo. Ada and Ben have orders. Cleo has no order. Order 104 has a NULL customer ID, so it has no matching customer.
The first query uses a LEFT JOIN. It returns every customer and any matching order.
The second query uses a RIGHT JOIN, then keeps only rows where the customer side is missing. This adds orders that were not included in the first query.
SELECT c.name, o.order_id, o.total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
UNION ALL
SELECT c.name, o.order_id, o.total
FROM customers AS c
RIGHT JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE c.customer_id IS NULL;The result contains the following logical rows:
| name | order_id | total |
|---|---|---|
| Ada | 101 | 50.00 |
| Ada | 102 | 20.00 |
| Ben | 103 | 35.00 |
| Cleo | NULL | NULL |
| NULL | 104 | 15.00 |
The first SELECT already returns all matches, Cleo, and her NULL order columns. The second SELECT returns only order 104. Its WHERE clause removes matched orders because those rows have a customer ID.
Why UNION ALL matters
Use UNION ALL, not just UNION, for the usual emulation pattern.
UNION ALL appends the second result without removing rows. The WHERE c.customer_id IS NULL condition already prevents the second query from returning matches, so no duplicate matched rows should exist.
UNION performs duplicate elimination. That extra work is unnecessary and can hide a query-design problem. It can also remove two genuinely separate rows if every selected value happens to be identical.
A table with a non-null primary key on the preserved side makes the anti-match condition safe:
| Condition | Meaning |
|---|---|
| `c.customer_id IS NULL` | The right-side order has no customer row |
| `o.order_id IS NULL` | A customer has no matching order in a left-join result |
Choose a column that cannot be NULL in a real row. A nullable data column is not a reliable unmatched-row marker.
Alternative using only LEFT JOIN
You can avoid RIGHT JOIN by swapping the table order in the second query:
SELECT c.name, o.order_id, o.total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
UNION ALL
SELECT c.name, o.order_id, o.total
FROM orders AS o
LEFT JOIN customers AS c
ON c.customer_id = o.customer_id
WHERE c.customer_id IS NULL;This version has the same meaning. The second query starts with orders, so its LEFT JOIN preserves every order. The filter keeps only orders without a customer.
Exercise
Why does the second query need WHERE c.customer_id IS NULL?
Show answer
Without that filter, it would return every order, including orders already returned by the first query. The filter keeps only orders with no matching customer.