Home · Blog · Technical and coding
SQL interview questions and answers
SQL questions show up in interviews for data analysts, engineers, scientists and many developer roles. Most of them test the same dozen ideas. This guide covers them from basic to advanced, with sample queries and how to talk through your answer.

In short
- Most SQL interviews test joins, aggregation, filtering, NULLs, window functions and indexes.
- Read the tables first and say what one row means before writing any query.
- Build the query in steps and check it against a few rows of sample data.
- Know the classics - second highest salary, duplicates, running totals, top N per group.
Practise this free: a mock interview for Behavioural round (STAR), scored, with a stronger answer. 3 a month, no card.
Start a mock interviewSQL interviews are more predictable than most technical rounds. The same ideas come up again and again: joining tables, grouping, filtering, handling NULLs and ranking with window functions. Learn those well, practise explaining them, and most questions become variations on things you’ve seen.
How SQL interviews work
You’ll usually meet one of three formats:
- Verbal questions: “What’s the difference between an inner and a left join?”
- Live queries: a shared editor with a schema, where you write queries while explaining.
- Take-home tasks: a dataset and a set of questions to answer in a few hours; see our take-home assignment tips.
In live rounds, always start by reading the tables and saying what one row means: “Each row in orders is one order, with a customer_id and a created_at date.” Many wrong answers come from misunderstanding the grain of a table.
Basic questions
- What’s the difference between INNER JOIN, LEFT JOIN and FULL OUTER JOIN? Inner keeps matches only; left keeps every row from the left table, with NULLs where there’s no match; full outer keeps everything from both.
- WHERE versus HAVING? WHERE filters rows before grouping; HAVING filters groups after.
- What does GROUP BY do? It collapses rows with the same values into one row per group, so you can aggregate with COUNT, SUM, AVG.
- How do you handle NULL? Use IS NULL, not = NULL. COUNT(column) skips NULLs; COUNT(*) doesn’t. COALESCE gives a default value.
- DISTINCT versus GROUP BY? Both can remove duplicates; GROUP BY lets you aggregate as well.
Intermediate questions
- Find customers who never placed an order. A LEFT JOIN with a NULL check, or NOT EXISTS.
- Find duplicate emails. GROUP BY email HAVING COUNT(*) > 1.
- Second highest salary. A subquery with MAX below the MAX, or a window function such as DENSE_RANK.
- Monthly revenue. Truncate the date to the month, then GROUP BY it.
- UNION versus UNION ALL? UNION removes duplicates, which costs time; UNION ALL keeps them.
Question: “Find customers who have never placed an order.”
You: “Customers has one row per customer, orders has one row per order with a customer_id. I’ll keep every customer with a left join and keep only those with no matching order:”
SELECT c.id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;
“NOT EXISTS would work too and can be clearer to read. If orders is large, an index on orders.customer_id keeps this fast.”
Advanced questions
- Top 3 products per category. ROW_NUMBER() OVER (PARTITION BY category ORDER BY sales DESC), then keep rows where the number is 3 or less.
- Running total. SUM(amount) OVER (ORDER BY date).
- Day-over-day change. LAG(value) OVER (ORDER BY date).
- ROW_NUMBER versus RANK versus DENSE_RANK? They differ in how they treat ties: ROW_NUMBER never repeats, RANK repeats and skips, DENSE_RANK repeats without skipping.
- Why is this query slow? Check for missing indexes on join and filter columns, functions on indexed columns, SELECT * on wide tables, and look at the query plan.
The PostgreSQL documentation has a clear tutorial on window functions if they’re new to you.
Question: “Show the top 3 products by revenue in each category.”
SELECT category, product, revenue
FROM (
SELECT category, product, SUM(amount) AS revenue,
ROW_NUMBER() OVER (PARTITION BY category ORDER BY SUM(amount) DESC) AS rn
FROM sales
GROUP BY category, product
) ranked
WHERE rn <= 3;
“I aggregate first, then rank within each category. If ties should share a place, I’d use DENSE_RANK instead.”
Questions about database design
Engineering roles often add a few questions about how data should be stored, not only queried:
- What is normalisation, and when would you denormalise? Normalising removes duplicated data so updates happen in one place; denormalising copies data on purpose to make frequent reads faster, at the cost of keeping copies in sync.
- Primary key versus foreign key? A primary key identifies each row; a foreign key points to a row in another table and keeps the relationship valid.
- What does an index cost? Faster reads on the indexed columns, but slower writes and extra storage. Index columns you filter and join on, not everything.
- What is a transaction? A group of changes that succeed or fail together, so the data is never left half-updated.
- How would you model orders and products? Customers, orders, products, and an order_items table between orders and products, because one order has many products and one product appears in many orders.
Answer these with an example from a project if you have one: “In our app, we denormalised the customer name onto invoices so old invoices kept the name at the time of purchase.”
How to answer well
- Clarify the grain and the edge cases: duplicates, NULLs, time zones, ties.
- Build in steps. Write the inner query, check it, then wrap it.
- Check with sample rows. Walk through two or three rows by hand.
- Mention performance briefly: indexes, and avoiding unnecessary work.
- Format readably. One clause per line; it’s easier for both of you.
Prepare for the role
SQL is usually one part of a wider interview. Our lists of data analyst and data engineer interview questions cover what else comes up. If coding is also part of the process, see how to pass a coding interview; if you’re coming from a non-technical role, our guide to technical interviews for non-technical roles helps.
Practise explaining your queries, free. Ghostpilot’s mock interviews ask the technical questions for your role and score how clearly you explain your answer.
3 mock interviews a month are free, with no card needed.
Start freeQuestions
What SQL questions are asked in interviews?
Usually joins, GROUP BY with HAVING, subqueries, window functions such as ROW_NUMBER and running totals, handling NULLs, finding duplicates and basic indexing. Analyst roles add more aggregation; engineering roles add performance and design.
What's the difference between WHERE and HAVING?
WHERE filters rows before grouping; HAVING filters groups after aggregation. For example, WHERE amount > 0 removes rows, while HAVING COUNT(*) > 5 keeps only groups with more than five rows.
Which SQL dialect should I use in an interview?
Standard SQL that works in most databases is safest. If the role uses a specific database, such as PostgreSQL or BigQuery, mention any dialect-specific functions you use.
How do I prepare for a SQL interview quickly?
Practise the classic patterns on a small sample database: joins, GROUP BY, window functions and duplicates. Write each query by hand, then run it and check the result.
Get live help in your next interview
Ghostpilot hears each question and shows an answer from your CV, in a small window above your call. Free to start: 2 live interviews and 3 mock interviews a month, no card needed.
Keep reading



