A JOIN combines rows from two tables by matching a condition, usually a foreign key. The join type decides what happens to rows that have no match on the other side.
A JOIN combines rows from two tables by matching a condition, usually a foreign key. The join type decides what happens to rows that have no match on the other side.
NULL.-- customers(id, name) orders(id, customer_id, total)
-- INNER: only customers who have at least one order
SELECT c.name, o.total
FROM customers c
INNER JOIN orders o ON o.customer_id = c.id;
-- LEFT: every customer, even those with zero orders (total = NULL)
SELECT c.name, o.total
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;
-- Classic use: find customers with NO orders
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL; -- the LEFT JOIN's NULL side reveals the gap
WHERE (e.g. WHERE o.total > 100) silently turns a LEFT JOIN back into an INNER JOIN, because NULL > 100 is false. Put such conditions in the ON clause if you want to keep unmatched rows.Most real queries span multiple tables, and the wrong join type quietly drops or duplicates rows — a bug that returns plausible results. Knowing that LEFT JOIN + IS NULL finds "missing" rows, and that a WHERE on the null side cancels the LEFT, prevents a whole class of reporting errors.
A library of IT interview questions with detailed answers — from Junior to Senior.
Donate