# Topic 31: FULL OUTER JOIN
Combining outer sets from both tables, bidirectional NULL padding, reconciliation queries, and full outer anti-joins.
1. Definition
A FULL OUTER JOIN returns all rows when there is a match in either the left or right table, returning NULLs on whichever side lacks a matching row.
2. Explain Like I'm 5
Think of two lists of students: one from Class A and one from Class B. A FULL OUTER JOIN lists every single student from both classes. If someone is in both classes, they share a row; otherwise, their missing class is blank.
3. Animation & Visual Simulation
Interactive Engine TracePhysical Join Engine (Build & Probe Phase)
SELECT u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id;Active InputStep 1: Build Phase (Smaller Table)
Key: id=1 ➔ Alice
Key: id=2 ➔ Bob
The optimizer selects the smaller table (users) and hashes primary keys into memory buckets.
4. Real-World Example
Reconciling bank accounts and payment gateway transactions to find missing settlements on either side.
| bank_ref | bank_amount | gateway_ref | gateway_amount |
|---|---|---|---|
| TX_101 | $100.00 | TX_101 | $100.00 (Matched) |
| TX_102 | $50.00 | NULL | NULL (Missing in Gateway) |
| NULL | NULL | TX_103 | $75.00 (Missing in Bank) |
5. SQL Syntax
ANSI SQL / PostgreSQL DialectSELECT COALESCE(b.id, g.id) AS transaction_id, b.amount AS bank_amount, g.amount AS gateway_amount FROM bank_transactions b FULL OUTER JOIN gateway_transactions g ON b.id = g.id;
SELECT * FROM bank_transactions b FULL OUTER JOIN gateway_transactions g ON b.id = g.id WHERE b.id IS NULL OR g.id IS NULL;
6. Important Rules & Behaviors
| Rule | Behavioral Explanation |
|---|---|
| All Rows Preserved | No rows are discarded from either table; missing matches are NULL-padded. |
| COALESCE Key Merging | Use COALESCE(left.id, right.id) to project a unified identifier column. |
| Execution Mechanism | Typically executed via Merge Full Join or Hash Full Join in database engines. |
| Bidirectional NULL padding | FULL OUTER JOIN preserves unmatched rows from BOTH relations, filling missing attributes with NULL on both sides. |
| Hash Full Outer Join memory | Hash Full Outer Join must keep tracking bitmasks for all hash table entries to identify unmatched right rows at stream end. |
7. Deep Dive: Full Outer Anti-Join for Data Synchronization Audits
The Full Outer Anti-Join (WHERE a.id IS NULL OR b.id IS NULL) is the definitive SQL pattern for finding sync mismatches between replica databases, payment systems, and data warehouses.
SELECT
COALESCE(a.id, b.id) AS record_id,
CASE
WHEN a.id IS NULL THEN 'Missing in Primary'
WHEN b.id IS NULL THEN 'Missing in Replica'
END AS discrepancy_reason
FROM primary_table a
FULL OUTER JOIN replica_table b ON a.id = b.id
WHERE a.id IS NULL OR b.id IS NULL;8. Advantages & Disadvantages
Advantages
- •Complete visibility across two datasets without dropping records.
- •Essential tool for data reconciliation and sync auditing.
Disadvantages & Trade-offs
- •High memory consumption as both sides must be tracked in memory.
9. Common Interview Questions
Q1.When should you use FULL OUTER JOIN in production systems?
FULL OUTER JOIN is predominantly used in financial reconciliation, schema migrations, and data auditing where you need to compare two datasets and identify: 1. Rows present in both systems, 2. Rows missing in the destination, and 3. Unexpected orphaned rows present only in the destination.
Q2.How does an engine execute a Hash Full Outer Join under the hood?
The engine hashes table A into an in-memory hash table where every entry has a "visited" bit initialized to 0. It streams table B and probes the hash table: matching rows are emitted and the hash entry’s bit is set to 1; unmatched table B rows are emitted with NULLs for table A. After table B finishes streaming, the engine scans the hash table and emits all entries whose visited bit is still 0 with NULLs for table B.
Q3.How do you write a Full Outer Anti-Join to find rows that exist in EITHER table but NOT in both?
A Full Outer Anti-Join combines FULL OUTER JOIN with a WHERE filter checking for NULL on either primary key: SELECT COALESCE(a.id, b.id) AS id, a.val, b.val FROM table_a a FULL OUTER JOIN table_b b ON a.id = b.id WHERE a.id IS NULL OR b.id IS NULL; This returns the symmetric difference between the two datasets.
Q4.Why can FULL OUTER JOIN NOT be optimized into a simple Nested Loop Join with an inner index scan?
A standard index nested loop scans outer table A and probes an index on B. But in a FULL OUTER JOIN, the engine must also detect which rows in B were NEVER visited by any row in A! An index probe cannot easily identify unvisited index entries without maintaining a full bitmap of the inner table or scanning table B independently.
Q5.What is the performance difference between FULL OUTER JOIN vs UNION of LEFT and RIGHT joins?
A single FULL OUTER JOIN processes both relations in a single pipeline pass using an in-memory hash join. Simulating it with (SELECT ... LEFT JOIN ...) UNION (SELECT ... RIGHT JOIN ...) executes two separate full table scans of both tables, performs two separate joins, and runs an expensive sorting/deduplication pass across the union, running 3x to 5x slower.
10. Interview-Ready Answer
“A FULL OUTER JOIN preserves all records from both tables, filling unmatched columns with NULLs. Its primary real-world application is data reconciliation and sync audit pipelines between disparate systems.”
11. Quick Test
Table A has 3 rows; Table B has 2 rows. None of the keys match. How many rows does a FULL OUTER JOIN produce?