Video in progress
How do the results of INNER JOIN and LEFT JOIN differ?
INNER JOIN returns only the rows that found a match in both tables. LEFT JOIN returns every row from the left table, and if there is no match on the right, those columns are NULL.
📖 Answer
Example: there are 3 users, and only two of them have orders.
-- only users that have orders
SELECT u.email, o.total
FROM users u
JOIN orders o ON o.user_id = u.id;
-- all users, total = NULL for those without orders
SELECT u.email, o.total
FROM users u
LEFT JOIN orders o ON o.user_id = u.id;
In practice LEFT JOIN is often used to "find the ones that have nothing":
SELECT u.email
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE o.id IS NULL; -- users who never placed an order
One more thing: if a user has 5 orders, that user shows up 5 times in the JOIN result. Beginners are often surprised when a SUM comes out wrong because of this: a JOIN multiplies rows.
Sign up to mark studied questions and save your progress.