SQL · easy
Asked in Data Analyst interviews, in the SQL round.
INNER = matches only; LEFT = all left + matched right; FULL = all rows both sides.
Say what each one keeps.
INNER JOIN: only rows with a match on both sides.LEFT JOIN: every row from the left table; right-side columns are NULL where there is no match.FULL OUTER JOIN: every row from both tables; NULLs on whichever side is missing.Then show you know when each one is the right choice:
-- customers who have never ordered: LEFT JOIN, then keep the NULL side
SELECT c.customer_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;
The follow-up interviewers like: put a condition on the right table in WHERE and a LEFT JOIN turns back into an INNER JOIN, because the NULL rows fail the filter. Conditions that should preserve unmatched rows belong in the ON clause. Also mention that a join on a non-unique key fans rows out, so COUNT(*) after a join counts matches, not customers.
WHERE versus ON distinction and the fan-out behaviour, more than the definitions.WHERE and wondering where the unmatched rows went.