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 NULLfinds left rows with no match (customers without orders).WHERE o.amount > 100after a left join quietly drops non-matches (NULL fails the comparison). Put right-table filters in theONclause 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
Discussion
Comments
Share feedback or questions about this page. No account required.
Loading comments…