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

Part 05 / Topic 3 of 9

LEFT JOIN: keeping the unmatched

LEFT JOIN keeps every row of the left table; where no right-side match exists, the right side's columns come back as NULL:

SELECT c.first_name, o.order_id, o.status
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
ORDER BY c.customer_id, o.order_id;
 first_name | order_id |  status   
------------+----------+-----------
 Asha       |        1 | delivered
 Asha       |        3 | delivered
 Asha       |        7 | pending
 Ravi       |        2 | delivered
 Ravi       |        6 | delivered
 Meera      |        4 | shipped
 Meera      |        8 | delivered
 John       |        5 | cancelled
 Sara       |        9 | delivered
 Vikram     |          | 
(10 rows)

Count the rows: 6 customers, 9 orders — and 10 result rows. The 9 matches, plus one row for Vikram padded with NULLs. Those NULLs are the signature of "kept, but unmatched" — and filtering on them is the classic way to ask "who has none?":

SELECT c.first_name, c.email
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;
 first_name |       email        
------------+--------------------
 Vikram     | [email protected]
(1 row)

RIGHT JOIN is the mirror image (keep every right-side row). You will rarely see it in real code — swapping the table order turns any RIGHT into a LEFT, and reading "keep the left one" consistently is easier on everyone.

🧪 Quick quiz — answer from memory, then check.

  1. shopdb has 6 customers and 9 orders; one customer has no orders. How many rows does customers LEFT JOIN orders return?
  2. In a LEFT JOIN result, what marks a left row that found no partner?
Show the answers
  1. 10 — the 9 matched rows plus 1 unmatched customer padded with NULLs.
  2. NULLs in every right-side column (test the right table's key with IS NULL).

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

Search the guide