Skip to content
Back to 50 Concepts
Topic 31 of 50query-executionIntermediate

# 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.

Relational Invariant:Deterministic relational algebra contract evaluated by the Cost-Based Optimizer.

2. Explain Like I'm 5

Intuitive mental model for beginners

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 Trace
Interactive Engine Simulation

Physical Join Engine (Build & Probe Phase)

Step 1 of 3
SQL>SELECT u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id;Active Input

Step 1: Build Phase (Smaller Table)

Engine Action: Construct in-memory Hash Table on users.id
Hash Bucket 1

Key: id=1 ➔ Alice

Hash Bucket 2

Key: id=2 ➔ Bob

How the Database Executes This:

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 Ledger vs Gateway Ledger
bank_refbank_amountgateway_refgateway_amount
TX_101$100.00TX_101$100.00 (Matched)
TX_102$50.00NULLNULL (Missing in Gateway)
NULLNULLTX_103$75.00 (Missing in Bank)
Relational walkthrough: A FULL OUTER JOIN exposes discrepancies on both sides simultaneously.

5. SQL Syntax

ANSI SQL / PostgreSQL Dialect
Step 1: Bidirectional reconciliation
SELECT
  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;
Step 2: Full Outer Anti-Join (Discrepancy only)
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;
Filters strictly for records that do NOT match on both sides.

6. Important Rules & Behaviors

RuleBehavioral Explanation
All Rows PreservedNo rows are discarded from either table; missing matches are NULL-padded.
COALESCE Key MergingUse COALESCE(left.id, right.id) to project a unified identifier column.
Execution MechanismTypically executed via Merge Full Join or Hash Full Join in database engines.
Bidirectional NULL paddingFULL OUTER JOIN preserves unmatched rows from BOTH relations, filling missing attributes with NULL on both sides.
Hash Full Outer Join memoryHash 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;
Caution: FULL OUTER JOIN operations cannot be optimized into simple index lookups as easily as INNER or LEFT joins.

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

Scenario Question:

Table A has 3 rows; Table B has 2 rows. None of the keys match. How many rows does a FULL OUTER JOIN produce?

Select your answer: