Before continuing: This entry is part of the JOIN guide. If you are new to the topic, start with JOIN.
FULL OUTER JOIN example
WITH expected(id, name) AS (
VALUES (1, 'Amy'), (6, 'Peter'), (9, 'Nora')
), activity(customer_id, last_date) AS (
VALUES (1, '2025-05-10'),
(7, '2025-05-23'),
(9, '2025-06-01')
)
SELECT e.id AS expected_id,
e.name,
a.customer_id,
a.last_date
FROM expected AS e
FULL OUTER JOIN activity AS a
ON a.customer_id = e.id
ORDER BY COALESCE(e.id, a.customer_id);| expected_id | name | customer_id | last_date |
|---|---|---|---|
| 1 | Amy | 1 | 2025-05-10 |
| 6 | Peter | NULL | NULL |
NULL | NULL | 7 | 2025-05-23 |
| 9 | Nora | 9 | 2025-06-01 |
Amy and Nora match. Peter is only on the expected list; id 7 only appears in the activity. The result shows all three situations in a single query.
How FULL OUTER JOIN is used
FULL JOIN and FULL OUTER JOIN are equivalent forms in engines that support this operator.
Common mix-ups with FULL OUTER JOIN
Filtering afterwards and losing one kind of gap
A condition like WHERE a.last_date >= '2025-06-01' removes the rows whose right side is NULL. If you need to keep them, write the rule carefully, for example by including OR a.last_date IS NULL, or filter before joining.
Key differences between engines
PostgreSQL, SQL Server and SQLite (from 3.39.0) support FULL OUTER JOIN. The MySQL 8.4 documentation describes LEFT and RIGHTouter joins, but not a FULLoperator; there, an equivalent result is usually built by combining queries, and it needs careful testing to avoid duplicating matches.
Reconciling two sources without hiding their differences
This join is useful for comparing an expected list with the list you actually received. Show both original keys while diagnosing: a missing left key means a row that was only received; a missing right key means an expected row with no counterpart. A key combined with COALESCE looks tidier, but showing only that key hides which side each row came from.
Before reconciling, check that the key is unique in both sources. Two rows on the left and three on the right with the same key produce six pairs. The join does not pick the most recent row or reconcile quantities for you. If you need one row per person, first decide which version counts as current, or summarise each source to the right level.
The self-contained case shows all three possible situations: a shared key, a key that was only expected and a key that was only received. Use required keys to detect a missing side. If the match condition relies on nullable columns, two unknown values do not match with =. Decide whether they should count as equal before changing the operator: matching every null to every other null can multiply rows unexpectedly.
Self-contained case to check in SQLite
The expected list contains 1 and 2; the received list contains 1 and 3. Each row shows explicitly which side it came from.
WITH expected(id) AS (VALUES (1), (2)),
received(id) AS (VALUES (1), (3))
SELECT e.id AS expected_id, r.id AS received_id,
CASE WHEN e.id IS NULL THEN 'received only'
WHEN r.id IS NULL THEN 'expected only'
ELSE 'in both' END AS situation
FROM expected e FULL OUTER JOIN received r ON r.id = e.id
ORDER BY COALESCE(e.id, r.id);| expected_id | received_id | situation |
|---|---|---|
| 1 | 1 | in both |
| 2 | NULL | expected only |
NULL | 3 | received only |
Key 1 matches, 2 is missing on the right and 3 is missing on the left. This test needs SQLite 3.39 or later.
Check your interpretation: If received contains two rows with id 1, why do two matches appear?
Show answer
Because each row of one table is paired with every row of the other that meets ON. The 1 in expected matches both rows with id 1 in received and forms two pairs.
Technical questions: PostgreSQL: Table Expressions and Joins (opens in a new tab).