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.

Ghostpilot team · · 4 min read

A dark blue card with the title "SQL interview questions and answers" next to the Ghostpilot ghost

In short

Practise this free: a mock interview for Behavioural round (STAR), scored, with a stronger answer. 3 a month, no card.

Start a mock interview

SQL 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:

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

  1. 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.
  2. WHERE versus HAVING? WHERE filters rows before grouping; HAVING filters groups after.
  3. 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.
  4. How do you handle NULL? Use IS NULL, not = NULL. COUNT(column) skips NULLs; COUNT(*) doesn’t. COALESCE gives a default value.
  5. DISTINCT versus GROUP BY? Both can remove duplicates; GROUP BY lets you aggregate as well.

Intermediate questions

  1. Find customers who never placed an order. A LEFT JOIN with a NULL check, or NOT EXISTS.
  2. Find duplicate emails. GROUP BY email HAVING COUNT(*) > 1.
  3. Second highest salary. A subquery with MAX below the MAX, or a window function such as DENSE_RANK.
  4. Monthly revenue. Truncate the date to the month, then GROUP BY it.
  5. 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

  1. 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.
  2. Running total. SUM(amount) OVER (ORDER BY date).
  3. Day-over-day change. LAG(value) OVER (ORDER BY date).
  4. 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.
  5. 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:

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

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 free

Questions

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

How to pass a coding interview (a step-by-step plan)Technical and coding · 4 min read
Take-home coding assignment tipsTechnical and coding · 4 min read
Technical interviews for non-technical rolesTechnical and coding · 4 min read
Online coding assessment tips (HackerRank, CodeSignal)Technical and coding · 4 min read