AnalystPracticeGuides
DashboardSign up

Data analyst SQL interview questions, with answers

Analyst SQL interviews return to the same handful of patterns. The examples below use two simple tables: customers (id, name, country) and orders (id, customer_id, product, amount, order_date).

Watch it solved. 10 SQL interview questions, easy to hard, in our video “SQL Interview Questions and Answers for Data Analysts (Easy to Hard)”. Pressing play loads the video from YouTube (privacy). Open on YouTube

1. What's the difference between WHERE and HAVING?

WHERE filters rows before grouping. HAVING filters groups after it, so it can use totals.

SELECT product, SUM(amount) AS total
FROM orders
WHERE amount > 100
GROUP BY product
HAVING SUM(amount) > 1000;

2. Find customers who have never ordered

This is asked constantly, because it shows you understand what a LEFT JOIN keeps.

SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;

Common mistake: writing = NULL, which is never true. Use IS NULL.

3. What's the difference between INNER JOIN and LEFT JOIN?

An inner join keeps only rows with a match on both sides. A left join keeps every row from the first table and fills the missing side with NULL. A good follow-up to mention: a join can multiply rows, so check your totals afterwards.

4. Find the second highest amount

SELECT DISTINCT amount
FROM orders
ORDER BY amount DESC
LIMIT 1 OFFSET 1;

Say why you added DISTINCT: if two orders tie for first place, the second row would otherwise repeat the top value. SQL Server uses a different syntax, OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY.

5. Find customers with more than one order

SELECT c.name, COUNT(*) AS orders
FROM orders o
JOIN customers c ON o.customer_id = c.id
GROUP BY c.name
HAVING COUNT(*) > 1;

In a real table, group by the id as well, because two customers can share a name.

6. COUNT(*), COUNT(column) or COUNT(DISTINCT column)?

COUNT(*) counts rows. COUNT(column) skips NULLs. COUNT(DISTINCT column) counts different values. This also explains a common bug: after a LEFT JOIN, COUNT(*) gives customers with no orders a count of 1, while COUNT(o.id) correctly gives 0.

7. Put values into your own categories

SELECT CASE WHEN amount >= 1000 THEN 'Big' ELSE 'Small' END AS size,
       COUNT(*)
FROM orders
GROUP BY size;

Some databases don't allow an alias in GROUP BY, so you may need to repeat the whole CASE expression.

8. Calculate a running total

SELECT order_date, amount,
       SUM(amount) OVER (ORDER BY order_date) AS running_total
FROM orders;

Window functions calculate across rows without collapsing them. Expect RANK() and PARTITION BY in the same conversation.

How to answer well

Say what you assume about the data, write the query, then say how you'd check it: compare with a total you know, or look at a few rows by hand. Practising in a real editor also helps a lot, since running a query that fails and fixing it teaches more than reading a correct one.

Try it yourself. Our SQL lessons run these patterns in a real database in your browser, and the SQL interview test puts them together.
Start practising SQL

Revising for an interview? We're putting about 60 Excel and SQL interview questions into one printable PDF. See sample pages

← All free guides