Skip to main content

LEFT JOIN and NULLs

A left join keeps every row from the left table. When the right side has no match, its columns are NULL.

Watch

Video coming soon

Learn

SELECT c.customer_id, c.email, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON c.customer_id = o.customer_id;

NULL traps:

  • WHERE o.order_id IS NULL finds left rows with no match (customers without orders).
  • WHERE o.amount > 100 after a left join quietly drops non-matches (NULL fails the comparison). Put right-table filters in the ON clause when you need to keep unmatched left rows.
SELECT c.customer_id, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON c.customer_id = o.customer_id
AND o.status = 'paid';

Check

Check understanding

What happens to unmatched right-table columns in a LEFT JOIN?
How do you list customers with no orders after a LEFT JOIN to orders?

Discussion

Comments

Share feedback or questions about this page. No account required.

Loading comments…