Atterna
RUEN
Get access

SQL interview questions for data analysts — 8 practice problems with answers

On this page · 10

Good interviewers test what a query really counts, not just syntax. Answer each question right here — every option explains itself.

1. COUNT(DISTINCT) after a filter

The classic warm-up. The trap is doing the steps in the wrong order: the filter runs first, then the counting.

Practice · check 1 of 8

order_idbuyerstatus
101Annapaid
102Leoncancelled
103Annapaid
104Ninapaid
SELECT COUNT(DISTINCT buyer)
FROM orders
WHERE status = 'paid';

What does this query return?

Preparing the practice…

What the interviewer listens for: you say what one row is (an order, not a buyer) before you count.

2. COUNT(column) is not a count of people

A query that runs and returns a plausible number is the most dangerous kind of wrong.

Practice · check 2 of 8

SELECT COUNT(buyer_id)
FROM orders
WHERE status = 'paid';

Marketing asked for the number of customers. What does this query actually count?

Preparing the practice…

Say it out loud: "COUNT(*) counts rows, COUNT(column) counts non-NULL values; to count customers I need COUNT(DISTINCT customer_id)."

3. NULL in an average

Empty values don't raise errors. They change what an aggregate covers.

Practice · check 3 of 8

productrating
Wireless headphones4.6
Set of wine glasses4.5
Scented candleNULL
Travel mug4.8
SELECT AVG(rating) FROM catalog;

What does it return?

Preparing the practice…

Follow-up you may get: how would you count products without a rating? COUNT(*) - COUNT(rating), or COUNT(*) FILTER (WHERE rating IS NULL).

4. WHERE vs HAVING

WHERE filters rows before grouping; HAVING filters groups after it. Interviewers like it because the wrong answers look almost right.

Practice · check 4 of 8

You need the product categories with more than 100 paid orders. Which query?

Preparing the practice…

5. Grouping by the wrong grain

A timestamp is not a date. Grouping by it returns one row per moment, not per day.

Practice · check 5 of 8

SELECT created_at, COUNT(*)
FROM orders
GROUP BY created_at;

You wanted orders per day. What do you get?

Preparing the practice…

The habit: before GROUP BY, name the grain you want in the result — one row per day, per customer, per category.

6. A JOIN that inflates a total

Interviewers like this one because it is a common way for real dashboards to go wrong.

Practice · check 6 of 8

orders.idamountitems in the order
1€503
2€301
SELECT SUM(o.amount)
FROM orders o
JOIN items i ON i.order_id = o.id;

What does this query return?

Preparing the practice…

The fix is to aggregate before you join, or to join only what you need. The full two-step practice with Meridian's real totals is in the JOIN duplicates guide.

7. A WHERE clause that breaks a LEFT JOIN

LEFT JOIN keeps unmatched rows with NULLs — until a condition on the right table removes them again.

Practice · check 7 of 8

SELECT u.id, COUNT(o.id) AS paid_orders
FROM users u
LEFT JOIN orders o ON o.buyer_id = u.id
WHERE o.status = 'paid'
GROUP BY u.id;

Do users with no paid orders appear in the result with 0?

Preparing the practice…

8. The question behind the query

Some interviewers end with a result that is correct and a conclusion that is not. This one comes straight from Atterna's first case.

Practice · check 8 of 8

Tomás · marketing

Orders with AUTUMN20 were 2.5× bigger than orders without it! Can I put “AUTUMN20 makes baskets bigger” into the December plan?

-- the query is correct
AUTUMN20 orders   €91.81 average
without the code  €36.60 average

The query is correct and the numbers are real. What do you check first?

Preparing the practice…

Why it matters: writing the query is increasingly the easy part — an AI assistant can draft it. Deciding what the number means, and what it can't tell you, is the part your team needs from you.

How to prepare

  • Read every query in logical execution order: FROM and JOIN, WHERE, GROUP BY, HAVING, SELECT, ORDER BY.
  • Before any aggregate, say what one row is and what you want one row of the result to be.
  • For every total, ask: could a row be repeated? Could a value be missing?
  • Practise on data with cancelled orders, refunds and baskets from several sellers, not on toy tables.

Start takes you from reading a table to JOINs and window functions in ten short days; days 1–2 need no account, and a free account opens all ten. If you already write SQL, case 1 is a 17-minute work request with exactly the kind of convincing, wrong number from question 8.

FAQ

What SQL questions are asked in a data analyst interview?

Mostly counting and joining: COUNT vs COUNT(DISTINCT), GROUP BY with HAVING, NULL handling, INNER vs LEFT JOIN, duplicate totals after a join, and often one window function (a running total or a rank). Strong candidates also explain what the result means.

How do I answer a SQL interview question I'm unsure about?

Say what one row of each table is, what one row of the result should be, and which rows could repeat or be missing. Interviewers value that reasoning more than a memorised query.

Is SQL still worth learning when AI writes queries?

Yes. AI writes plausible SQL quickly, including queries that double-count after a join or drop rows in a LEFT JOIN. You need to read SQL well enough to catch that before the number reaches a decision.

Where can I practise SQL interview questions on realistic data?

This page and the JOIN practice are free. Atterna Start and Cases use a practice marketplace with cancelled orders, refunds and multi-seller baskets; all ten Start days (with a free account) and case 1 are free.

0 of 8 checks answered

Try the remaining checks above.

Every answer opens an explanation, including a wrong one.

Free account

Practise this, free

A free account opens hands-on lessons on a realistic marketplace.

  • All 10 days of the SQL courseFrom your first table to JOINs and window functions
  • Meridian episode 1A full analyst week: the record that wasn’t
  • Your first portfolio projectFinished work as a ready GitHub project, with your SQL and charts
  • Progress on any deviceYour cheat sheet and review cards go with you
Create a free account Free · 30 seconds · no cardAlready have one? Sign in

Examples use Meridian, Atterna’s fictional practice marketplace.