MySQL interview question · Most asked
Explain INNER, LEFT and RIGHT joins. What is the classic LEFT JOIN mistake?
Summary
INNER JOIN returns only matching rows; LEFT JOIN returns every row from the left table with NULLs where there is no match; RIGHT JOIN is the mirror image. The classic mistake is filtering the right table in WHERE, which removes the NULL rows and silently turns the LEFT JOIN into an INNER JOIN.
Longer Answer
-- Wrong: customers without 2026 orders disappear
SELECT c.id, COUNT(o.id) FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.created_at >= '2026-01-01'
GROUP BY c.id;
-- Right: put the condition in ON
SELECT c.id, COUNT(o.id) FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id AND o.created_at >= '2026-01-01'
GROUP BY c.id;
Also remember that COUNT(o.id) counts matches while COUNT(*) counts the NULL row too, and that joining two one-to-many relations multiplies rows (fan-out), inflating sums.
Key Points
- INNER vs LEFT vs RIGHT.
- WHERE on the right table negates the LEFT JOIN.
- COUNT(col) vs COUNT(*).
- Fan-out when joining two child tables.
More most-asked MySQL questions
- What is a clustered index in InnoDB, and why does primary key choice matter so much?
- Why is OFFSET pagination slow on large tables, and what is keyset pagination?
- How do you read EXPLAIN output in MySQL?
- Why might MySQL not use an index you created?
- What are the transaction isolation levels, and which does InnoDB use by default?
- How does a composite index work, and what is the leftmost-prefix rule?
- What causes deadlocks in MySQL, and how do you handle them?
- What are gap locks and next-key locks, and why do they surprise developers?