Guides / SQL interview

SQL interview · guide

SQL interview questions for data analysts, with answers

These questions come up again and again in analyst screens. Practise saying the answer in one or two sentences, then show the query.

Fundamentals

  1. WHERE vs HAVING? WHERE filters rows before grouping; HAVING filters groups after aggregation.
  2. INNER vs LEFT JOIN? INNER keeps matches only; LEFT keeps every left row and fills NULL where nothing matches.
  3. COUNT(*) vs COUNT(column)? COUNT(*) counts rows; COUNT(column) skips NULLs.
  4. What does GROUP BY do? Collapses rows into one per group so you can aggregate.
  5. NULL behaviour? NULL is unknown: NULL = NULL is not true. Use IS NULL, and COALESCE to replace it.

Intermediate

  1. Top N per group? ROW_NUMBER() OVER (PARTITION BY group ORDER BY value DESC), then filter rn <= N.
  2. Running total? SUM(x) OVER (ORDER BY date).
  3. Month over month change? value - LAG(value) OVER (ORDER BY month).
  4. Find duplicates? GROUP BY the key and HAVING COUNT(*) > 1.
  5. CTE vs subquery? A CTE names a step for readability and reuse; results are equivalent for simple cases.

Analytics-flavoured

  1. Retention or cohort query? Assign each user a first-activity month, join back to activity, and count users per month offset.
  2. Conditional totals? SUM(CASE WHEN status = 'paid' THEN amount ELSE 0 END), also called pivoting with conditional sums.

How to answer well

Practise it

Share on LinkedIn

Practise this for free

Run real code, get instant checks and keep your progress. Free to join with Google.

Start free → Choose a career path

More guides