Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

Part 05 / Topic 4 of 9

FULL OUTER JOIN and CROSS JOIN

The two you reach for rarely, but nothing else does their job.

FULL OUTER JOIN keeps unmatched rows from both sides — the tool for comparing two sets. Which customer ids appear in customers but never in events, and vice versa?

SELECT c.customer_id AS in_customers, e.customer_id AS in_events,
       count(e.event_id) AS events
FROM customers AS c
FULL OUTER JOIN events AS e ON e.customer_id = c.customer_id
GROUP BY c.customer_id, e.customer_id
ORDER BY c.customer_id;
 in_customers | in_events | events 
--------------+-----------+--------
            1 |         1 |      3
            2 |         2 |      2
            3 |           |      0
            4 |           |      0
            5 |           |      0
            6 |         6 |      2
(6 rows)

Rows with a NULL on either side are the differences (here: customers 3, 4 and 5 have no events; every event has a customer, so the right side has no orphans). Reconciliations — payments vs invoices, exports vs imports — are FULL OUTER JOIN territory.

CROSS JOIN pairs every row with every row — no condition at all. 3 suppliers × 6 categories = 18 combinations:

SELECT s.name AS supplier, cat.name AS category
FROM suppliers AS s
CROSS JOIN categories AS cat
ORDER BY s.name, cat.name
LIMIT 7;
   supplier   |  category   
--------------+-------------
 HomeGoods Co | Books
 HomeGoods Co | Electronics
 HomeGoods Co | Home
 HomeGoods Co | Kitchen
 HomeGoods Co | Laptops
 HomeGoods Co | Phones
 PageTurners  | Books
(7 rows)

Useful on purpose (every product × every month, for a report grid); catastrophic by accident — an inner join with a wrong or missing ON degenerates into exactly this, at a million × a million. If a join result looks impossibly large, check the ON first.

🧪 Quick quiz — answer from memory, then check.

  1. Which join finds rows present in either table but missing from the other?
  2. CROSS JOIN between a 3-row and a 4-row table returns how many rows?
Show the answers
  1. FULL OUTER JOIN — unmatched rows from both sides survive, flagged by NULLs.
  2. 12 — every combination (the Cartesian product).

Done here? The practice set for Part 5 — 8 exercises with solutions — is on the part page.

Search the guide