← All lessons
Combining and summarizing · Lesson 5 of 10

LEFT JOIN and what's missing

An inner join drops any row that has no match. Gus and Hana have never ordered, so they vanish from customers joined to orders. A LEFT JOIN keeps every row of the first (left) table. Where the other table has no match, its columns come back as NULL:

SELECT c.name, o.id AS order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE c.id >= 5
ORDER BY c.name;
Returns
name | order_id
-----+---------
Eve  | 107
Finn | 109
Gus  | NULL
Hana | NULL
(4 rows)

That gives you a way to find what's missing: left join, then keep the rows where the right side is NULL. Customers who have never ordered:

SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;
Returns
name
----
Gus
Hana
(2 rows)

With GROUP BY it counts zeros properly. COUNT(o.id) counts only the matched orders and skips the NULLs, so Gus gets 0. COUNT(*) would count his one row of NULLs and say 1:

SELECT c.name, COUNT(o.id) AS orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY orders DESC, c.name;
Returns
name | orders
-----+-------
Ana  | 3
Ben  | 2
Cleo | 2
Dev  | 1
Eve  | 1
Finn | 1
Gus  | 0
Hana | 0
(8 rows)

Now the trap. Say you want every customer with a count of their shipped orders. Put o.status = 'shipped' in WHERE and the customers with no shipped orders disappear, because their status is NULL and WHERE drops them. The left join has quietly become an inner one. Put the condition in the ON, which decides what counts as a match, and every customer stays:

SELECT c.name, COUNT(o.id) AS shipped
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'shipped'
GROUP BY c.id, c.name
ORDER BY c.id;
Returns
name | shipped
-----+--------
Ana  | 3
Ben  | 2
Cleo | 1
Dev  | 1
Eve  | 0
Finn | 1
Gus  | 0
Hana | 0
(8 rows)

RIGHT JOIN is a left join written the other way round, so people rarely use it. FULL OUTER JOIN keeps the unmatched rows from both sides. SQLite has both since version 3.39, as do most databases.

Practice

The names of the customers who have never placed an order.

The tables
  • customers (id, name, city, joined)
  • products (id, name, category, price)
  • orders (id, customer_id, ordered_on, status)
  • order_items (order_id, product_id, qty)
  • signups (id, email, name, signed_up_at)
  • payments (id, order_id, amount, currency, paid_at)
  • events (id, received_at, payload (JSON text))
  • staff (id, name, role, manager_id, salary, hired)
  • logins (customer_id, day)
More info

Quick reference

LEFT JOIN t ON …Every left row; the right side is NULL where nothing matches.
… WHERE t.id IS NULLAfter a LEFT JOIN: the left rows with no match.
COUNT(t.id) vs COUNT(*)With a LEFT JOIN, COUNT(t.id) gives 0 for no match; COUNT(*) gives 1.
ON … AND x = yA condition on the right table that keeps unmatched left rows.
FULL OUTER JOINUnmatched rows from both sides.

The other AI and data engineering courses