For analysts · Segmented by years of experience

SQL interview questions
for data analysts

The SQL barely changes between a junior and a senior analyst loop. What changes is how much of the problem you're handed. Junior rounds give you a spec and check the rows. Senior rounds hand you an ambiguous business question and watch you define the metric — the query is the easy half.

Start the Interview Prep track — free Find your level ↓

What an analyst loop tests — and what it doesn't

Six areas carry almost every analyst interview: joins (including the anti-join), aggregation with GROUP BY and HAVING, window functions, subqueries and CTEs, conditional logic with CASE, and date handling. Those are the six our own Interview Prep track targets, and they are not a coincidence — they are the areas where a business question maps onto a non-obvious SQL shape.

Now the part most prep material gets wrong. Indexes, execution plans, partitioning strategy, and schema design are data engineer questions. They show up in analyst loops rarely and usually as a "do you know this exists" aside, not as a graded exercise. Time spent memorising index types is time not spent on the top-N-per-group pattern you will almost certainly be asked to write.

The one exception worth knowing. If the role is titled "analytics engineer", or the job description mentions dbt, the loop drifts toward engineering: incremental models, testing, and yes, some performance. Read the posting, not the job title — "Data Analyst" at one company is "Analytics Engineer" at another.

By Experience

Same SQL, different question

This is the axis most prep lists ignore. Preparing for a senior round the way you'd prepare for a junior one is the most common way strong candidates underperform.

0–2 years

Can you make it run and return the right rows?

The spec is precise on purpose. "Return the name and total for every customer who spent more than the average." Success is a correct result set; the interviewer is checking mechanics.

  • Joins that don't accidentally fan out and inflate a SUM
  • GROUP BY with the right columns, HAVING rather than WHERE
  • Basic subqueries — scalar and IN
  • Reading the schema before writing anything

Prepare by: volume on Easy and Medium until syntax stops costing you thought.

2–5 years

Can you define the metric before you write it?

The question arrives deliberately underspecified. "Find our most engaged customers." There is no definition of engaged, and supplying one out loud — then defending it — is most of the score.

  • Window functions, especially top-N per group
  • Multi-step CTEs you can narrate step by step
  • Saying "I'm defining engaged as 3+ orders in 30 days — does that match how you use it?"
  • Cohorts, retention, month-over-month change

Prepare by: Hard challenges, and practising narration — say the plan aloud before typing.

5+ years

Will you push back on the question?

You're expected to interrogate the ask. What will this number miss? What happens to it if the table has duplicates, or NULLs in the join key? What would you validate before anyone makes a decision on it?

  • Naming the failure modes of your own query, unprompted
  • Duplicates and NULL semantics as a reflex, not a reminder
  • Recursive CTEs for hierarchies when they come up
  • Trade-offs: "this is correct but scans everything — here's the cheaper version"

Prepare by: Hard set, then re-solving with the question "how would this be wrong in production?"

The trap in senior rounds. Producing a fast, correct query in silence reads as junior. The interviewer cannot grade the thinking they cannot hear, and at 5+ years the thinking is the thing being graded. Narrate the definition, the assumption, and the caveat — even when the SQL is easy for you.

The Short List

Eight patterns to write cold

Not eight questions — eight shapes. Interview questions are these wearing different business clothes.

1. Find the duplicates most asked

GROUP BY key HAVING COUNT(*) > 1 — then the follow-up: which copy would you keep, and why? Full walkthrough →

2. Top-N per group most asked

Best-selling product in each category. Needs ROW_NUMBER() OVER (PARTITION BY … ORDER BY …) — a plain GROUP BY with MAX can't return the other columns of the winning row, and saying so scores points.

3. The anti-join

Customers who never ordered. LEFT JOIN … WHERE right.id IS NULL, or NOT EXISTS. Know why NOT IN with a NULL in the subquery returns nothing at all — this is a favourite trap.

4. Running total / cumulative

SUM(x) OVER (ORDER BY d ROWS UNBOUNDED PRECEDING). The frame clause is the part people forget, and the part that changes the answer.

5. Period-over-period change

LAG() for the previous value, then the percentage. Handle the first row's NULL explicitly instead of letting it become a division error. Worked example →

6. Conditional aggregation

SUM(CASE WHEN … THEN 1 ELSE 0 END) to pivot rows into columns in one pass. Once you see it, half of "build me this report" questions collapse into it.

7. Cohort / retention

First-purchase month per customer, then activity relative to it. Two CTEs and a self-join. This is the archetypal 2–5 year question.

8. Median without a percentile function

Not every warehouse gives you PERCENTILE_CONT. ROW_NUMBER from both ends and take the middle. Asked specifically to see whether you can build a primitive you're missing.

Practice

The ladder, graded

Ordered by the levels above. Every one runs in the browser against real sample data and is checked against the expected result — no self-marking.

Open the editor — free

Or start the SQL Interview Prep track in the Coach — ~25 hours across 34 steps, and it skips what your skill radar shows you already have.

Company Specifics

Interviewing somewhere in particular?

The eight patterns are universal. The framing isn't — a marketplace asks about supply and demand, a payments company asks about reconciliation.

Full interview prep hub →

Frequently Asked

Recall speed is the gap,
not knowledge.

Most analysts who fail a SQL round already knew the answer — they just couldn't produce it while someone watched. That's a practice problem, and it's fixable in a couple of weeks.

Start Free — No Signup ⚡

250+ challenges · Adaptive Coach · Skill radar · Pro from $29/month or $199 one-time