INNER JOIN vs LEFT JOIN?
30-second answer: Inner join keeps only matching rows. Left join keeps every row from the left table and fills missing right-side values with NULL. Left join plus IS NULL finds rows with no match.
INNER JOIN returns only rows with a match in both tables. LEFT JOIN returns every row from the left table and fills the right-side columns with NULL when there is no match.
A common pattern is LEFT JOIN … WHERE right.id IS NULL to find rows with no related record.
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL; -- customers who never orderedFollow-up questions interviewers ask next
- What is a self join and when do you use it?
- How do FULL OUTER and CROSS joins differ?