Sub topic 12
FULL OUTER JOIN on databases without it
Some databases do not implement FULL OUTER JOIN. This chapter shows how to get the same result from joins they do support. It also covers the mistakes that make the emulation return wrong rows.
This chapter assumes you have read the earlier chapters on LEFT and RIGHT joins, NULL padding, and anti-joins. It also assumes you know UNION and UNION ALL. It uses the customers and orders sample tables from the start of this tutorial:
- Cleo (customer 3) has no orders.
- Order 104 has a
NULLcustomer.
Which databases lack FULL OUTER JOIN
FULL OUTER JOIN is part of standard SQL. PostgreSQL, SQL Server, Oracle and recent SQLite versions (3.39.0 and later) support it. MySQL and MariaDB do not. They report a syntax error when they reach the FULL keyword.
Check your database's documentation before you emulate. A native FULL OUTER JOIN is shorter, easier to read, and usually easier for the optimizer to handle.
What a full join is made of
A full outer join returns three groups of rows:
- Matched rows. Rows where the
ONcondition is true, as in an inner join. - Left-only rows. Rows from the left table with no match, padded with
NULLon the right. - Right-only rows. Rows from the right table with no match, padded with
NULLon the left.
A LEFT JOIN already returns groups 1 and 2. Group 3 is the missing piece. It is exactly the set of right-table rows that have no left-table partner, which is the anti-join pattern from earlier in the tutorial, mirrored.
The standard emulation
Run a LEFT JOIN, then append the right-only rows with UNION ALL:
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
UNION ALL
SELECT c.name, o.order_id
FROM customers c
RIGHT JOIN orders o ON o.customer_id = c.customer_id
WHERE c.customer_id IS NULL;
-- Ada 101
-- Ada 102
-- Ben 103
-- Cleo NULL
-- NULL 104The first query produces the matched rows and Cleo. The second query is a RIGHT JOIN that keeps every order. The WHERE c.customer_id IS NULL then keeps only the orders that found no customer, which here is order 104.
The result has the same five rows as the native FULL OUTER JOIN. As with the native join, the row order is not guaranteed.
Why the test column matters
The WHERE test must use a column that can never be NULL in a real customer row. The primary key customer_id is the safe choice. If you tested a nullable column such as a middle_name, a matched customer with a missing value would be dropped by mistake. This is the same rule as in the anti-join chapter.
Writing it with only LEFT JOINs
Some teams avoid RIGHT JOIN and always put the preserved table first. You can write the second query as a LEFT JOIN from orders:
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
UNION ALL
SELECT c.name, o.order_id
FROM orders o
LEFT JOIN customers c ON o.customer_id = c.customer_id
WHERE c.customer_id IS NULL;Both versions return the same rows. The select list keeps the same column order (c.name, then o.order_id) in both halves, so the two halves line up.
Mistakes that change the result
Forgetting the filter
Without the WHERE in the second query, the RIGHT JOIN returns every order again, including the ones that already matched in the first query:
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
UNION ALL
SELECT c.name, o.order_id
FROM customers c
RIGHT JOIN orders o ON o.customer_id = c.customer_id;
-- Ada 101
-- Ada 102
-- Ben 103
-- Cleo NULL
-- Ada 101 <- duplicate
-- Ada 102 <- duplicate
-- Ben 103 <- duplicate
-- NULL 104Every matched row appears twice. The filter in the second query removes exactly the rows the first query already returned.
Using UNION instead of UNION ALL
Plain UNION removes duplicate rows. It looks like a fix for the mistake above, and with the sample data it even returns the right answer, because every row differs from the others. It is still the wrong tool, for two reasons:
- A real full join can legitimately contain identical rows. If you select only
c.nameando.total, and Ada has two separate orders of 20.00, the native join returns twoAda 20.00rows.UNIONcollapses them into one. UNIONhas to compare every row to find duplicates, which costs extra sorting or hashing work.
With UNION ALL plus the anti-join filter, no row appears twice by construction, so no deduplication is needed.
| Variant | Matched rows | Legitimate duplicates | Cost |
|---|---|---|---|
| `UNION ALL` with `IS NULL` filter | Once | Kept | Lowest |
| `UNION ALL` without filter | Twice | Kept, plus extras | Wasted work |
| `UNION` without filter | Once, if rows are distinct | Lost | Extra deduplication |
Repeating the full ON condition
The emulation has two copies of the join logic, so any extra condition must appear in both halves. Suppose the original full join was meant to match only large orders:
-- Intended: FULL OUTER JOIN orders o
-- ON o.customer_id = c.customer_id AND o.total > 30
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.customer_id AND o.total > 30
UNION ALL
SELECT c.name, o.order_id
FROM customers c
RIGHT JOIN orders o
ON o.customer_id = c.customer_id AND o.total > 30
WHERE c.customer_id IS NULL;
-- Ada 101
-- Ben 103
-- Cleo NULL
-- NULL 102
-- NULL 104Here the total > 30 test sits in ON, so it decides only what counts as a match. Order 102 (total 20.00) no longer matches Ada, so it appears as a right-only row. If you put that condition in WHERE, you would remove rows instead of changing which rows match, and the result would no longer be a full join. This is the ON versus WHERE rule from earlier, applied twice.
If you edit the condition in one half and forget the other, the two halves disagree and rows are lost or duplicated. Keep the two copies identical.
Using the emulated join in a larger query
The emulation is a compound query, so it cannot be combined with other clauses directly. To aggregate over it, filter it, or join it to another table, wrap it in a derived table:
SELECT COUNT(*) AS total_rows,
COUNT(f.name) AS rows_with_customer,
COUNT(f.order_id) AS rows_with_order
FROM (
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
UNION ALL
SELECT c.name, o.order_id
FROM orders o
LEFT JOIN customers c ON o.customer_id = c.customer_id
WHERE c.customer_id IS NULL
) AS f;
-- 5 4 4COUNT(*) counts all five rows. COUNT(f.name) skips the one row where the name is NULL (order 104). COUNT(f.order_id) skips the one row where the order is NULL (Cleo). MySQL requires the alias (AS f) on a derived table.
An ORDER BY placed after the last SELECT applies to the whole union, not to that last query. You can refer to the output columns by name or by position:
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
UNION ALL
SELECT c.name, o.order_id
FROM orders o
LEFT JOIN customers c ON o.customer_id = c.customer_id
WHERE c.customer_id IS NULL
ORDER BY order_id;The ordering of NULL values differs between databases. In MySQL, NULL sorts first in ascending order, so Cleo's row would come first.
Alternative: NOT EXISTS for the second half
Instead of a join with an IS NULL filter, the right-only rows can come from NOT EXISTS. This form needs no RIGHT JOIN, and its intent is easy to read:
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
UNION ALL
SELECT NULL, o.order_id
FROM orders o
WHERE NOT EXISTS (
SELECT 1 FROM customers c WHERE c.customer_id = o.customer_id
);Because no customer columns are available in the second query, you write NULL for them yourself. In some databases a bare NULL has no type of its own, so the union may need a cast, such as CAST(NULL AS VARCHAR(50)). This variant also handles order 104 correctly. Its customer_id is NULL, so the comparison never succeeds and NOT EXISTS is true. It is the safe choice here. NOT IN is not, because a NULL in the list makes the whole test unknown.
Summary
- A full outer join is matched rows, plus left-only rows, plus right-only rows.
- Emulate it with a LEFT JOIN,
UNION ALL, and a second query that returns only the right-only rows. - Filter the second query on a non-nullable column of the left table, normally its primary key.
- Prefer
UNION ALLoverUNION.UNIONremoves rows that a real full join would keep. - Repeat any extra
ONcondition identically in both queries. - Wrap the emulation in a derived table to aggregate or filter it.
Exercise
A colleague emulates a full join on MySQL with this query, and the result shows every matched order twice. What is missing, and what would you add? SELECT c.name, o.orderid FROM customers c LEFT JOIN orders o ON o.customerid = c.customerid UNION ALL SELECT c.name, o.orderid FROM customers c RIGHT JOIN orders o ON o.customerid = c.customerid;
Show answer
The second query returns all orders, including those already returned by the LEFT JOIN. It needs a filter that keeps only the right-only rows: add WHERE c.customer_id IS NULL, using the non-nullable primary key of customers. Then only order 104 is added after the LEFT JOIN rows.
Exercise
Why is replacing UNION ALL with UNION a poor way to repair the query above, even when the output looks correct on the sample data?
Show answer
UNION removes all duplicate rows, including legitimate ones. If the select list contains only columns such as c.name and o.total, two separate orders with the same customer and total are real, distinct rows in a full join, but UNION collapses them into one. It also adds the cost of deduplicating the whole result. The anti-join filter with UNION ALL avoids both problems.