Interview Questions, Part 3
The theory questions.
Not every interview question asks you to write code. Many ask you to explain an idea in one or two sentences. What's the difference between where and having? Between union and union all? These are quick wins, if you've practised the answers. Let's go through the most common ones.

- Explain it in a sentence
- Quick wins
- The most common ones
WHERE vs HAVING
Question one. What's the difference between where and having? Answer. Where filters rows, before grouping. Having filters groups, after grouping, so it can use aggregates like count and sum.
WHERE
- Filters rows
- Before GROUP BY
- No aggregates
HAVING
- Filters groups
- After GROUP BY
- Uses COUNT, SUM ...
UNION vs UNION ALL
Question two. Union versus union all. Let's prove it. We stack the customers' countries on top of themselves, both ways. Run it. Union removes duplicates, leaving six countries. Union all keeps everything, one thousand rows. Union all is faster, because it doesn't have to check for duplicates.
SELECT COUNT(*) AS with_union
FROM (SELECT country FROM customers
UNION
SELECT country FROM customers);
SELECT COUNT(*) AS with_union_all
FROM (SELECT country FROM customers
UNION ALL
SELECT country FROM customers);โโโโโโโโโโโโโโ โ with_union โ โโโโโโโโโโโโโโค โ 6 โ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ with_union_all โ โโโโโโโโโโโโโโโโโโค โ 1000 โ โโโโโโโโโโโโโโโโโโ
INNER vs LEFT JOIN
Question three. Inner join versus left join. Answer. An inner join keeps only rows that match in both tables. A left join keeps every row from the left table, with nulls where there's no match.
INNER JOIN
- Matches only
- Unmatched rows dropped
- Both sides needed
LEFT JOIN
- All left rows
- NULLs when no match
- Find what's missing
Summary
Where versus having, union versus union all, inner versus left. Short, clear answers win interviews. More theory next.
Key takeaways- WHERE: rows, HAVING: groups
- UNION dedupes, ALL keeps
- INNER vs LEFT