How to Answer SQL Interview Questions Like a Data Analyst (Not Like a Textbook)
This article is available in English. Browse translated guides in the language menu.
Here is an uncomfortable truth about data analyst SQL interviews: most rejected candidates could write the query. They knew JOIN, GROUP BY and window functions. They still didn’t get the offer.
Why? Because the interviewer wasn’t really testing SQL. They were testing whether you can be trusted with a number that a manager will use to make a decision.
This guide shows you how to answer like an analyst: calm, structured, and careful. It covers a repeatable framework, five classic questions with the hidden traps in each, the phrases that signal seniority, and a one-week plan.
What Interviewers Actually Score
Most analyst interview rubrics look roughly like this:
| Signal | What it looks like | Weight |
|---|---|---|
| Problem understanding | You ask what the metric means before writing code | High |
| Correctness | Handles duplicates, NULLs, ties, empty groups | High |
| Communication | You narrate your thinking and state assumptions | High |
| Validation | You sanity-check the output | Medium |
| Syntax fluency | Query runs with minor or no errors | Medium |
| Speed / cleverness | Fancy tricks | Low |
Notice that clever one-liners sit at the bottom. A clear, slightly longer query you can explain beats an elegant one you can’t.
The 6-Step Answer Framework
Use this on every question. Say the steps out loud.
1. Restate the question in business terms
“So we want the share of customers who ordered again within 30 days of their first order. Is that right?”
This takes 10 seconds and prevents a 10-minute wrong turn.
2. Clarify the definitions
Every metric hides a decision. Ask about it:
- “Active user”: logged in? Purchased? Within what window?
- “Revenue”: before or after refunds? Including tax?
- “Month”: calendar month or rolling 30 days? Which time zone?
- “Top 3”: what happens on ties?
3. Look at the grain of each table
Ask: “What does one row represent?” One row per order? Per order item? Per user per day? Most wrong answers come from joining tables of different grain.
4. Plan before typing
Say your plan in one breath: “I’ll first find each customer’s first order date, then join back to orders, filter to the 30-day window, and divide by the total customers.”
5. Write in small, readable steps
Use CTEs with meaningful names (first_orders, repeat_buyers). Readability is evidence of clear thinking.
6. Validate out loud
Before declaring “done”, run through a quick check:
- Does the result have a plausible size? (A percentage over 100% is a red flag.)
- Do the parts add up to the whole?
- What happens with a user who has no orders? Or an order with a NULL date?
Candidates who validate unprompted are remembered. It’s the cheapest way to look senior.
Five Classic Questions and the Trap in Each
Question 1: “Find the second-highest salary.”
The trap: ties and “no such value”.
SELECT MAX(salary) AS second_highest_salary
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
Or, more generally (the Nth highest):
WITH ranked AS (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
)
SELECT MAX(salary) AS second_highest_salary
FROM ranked
WHERE rnk = 2;
What to say: “I’m using DENSE_RANK so two people tied for first don’t skip rank 2. Wrapping in MAX means I get NULL instead of an empty result if there is no second value. I’d confirm which behaviour the business wants.”
Question 2: “Which customers have never placed an order?”
The trap: NOT IN with NULLs, and counting the wrong thing after a join.
SELECT c.customer_id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;
What to say: “I prefer LEFT JOIN ... IS NULL or NOT EXISTS. NOT IN returns zero rows if the subquery contains even one NULL, which is a silent failure.”
Question 3: “What’s the month-over-month revenue growth?”
The trap: the first month has no previous month, and division by zero.
WITH monthly AS (
SELECT DATE_TRUNC('month', order_date) AS month,
SUM(amount) AS revenue
FROM orders
WHERE status = 'completed'
GROUP BY 1
)
SELECT month,
revenue,
LAG(revenue) OVER (ORDER BY month) AS prev_revenue,
ROUND(
100.0 * (revenue - LAG(revenue) OVER (ORDER BY month))
/ NULLIF(LAG(revenue) OVER (ORDER BY month), 0), 2
) AS growth_pct
FROM monthly
ORDER BY month;
What to say: “I filter to completed orders because cancelled ones aren’t revenue. NULLIF guards against divide-by-zero, and the first month is intentionally NULL, not 0%. Also, if a month has no orders at all, it’d be missing from the output, so I’d ask whether we need a calendar table to fill gaps.”
Question 4: “What percentage of users came back within 7 days of signing up?”
The trap: the denominator. Users who signed up less than 7 days ago haven’t had a chance to return yet.
WITH eligible AS (
SELECT user_id, signup_date
FROM users
WHERE signup_date <= CURRENT_DATE - INTERVAL '7 days'
),
returned AS (
SELECT DISTINCT e.user_id
FROM eligible e
JOIN sessions s
ON s.user_id = e.user_id
AND s.session_date > e.signup_date
AND s.session_date <= e.signup_date + INTERVAL '7 days'
)
SELECT ROUND(100.0 * COUNT(r.user_id) / COUNT(e.user_id), 1) AS d7_retention_pct
FROM eligible e
LEFT JOIN returned r USING (user_id);
What to say: “I only count users old enough to be measured. Including newer users would drag retention down artificially.”
This single point (a fair denominator) separates strong analysts from average ones.
Question 5: “Top 3 products per category by sales.”
The trap: LIMIT 3 gives you 3 rows total, not 3 per group. Ties also matter.
WITH product_sales AS (
SELECT category, product_name, SUM(amount) AS sales
FROM order_items
GROUP BY category, product_name
),
ranked AS (
SELECT *,
DENSE_RANK() OVER (PARTITION BY category ORDER BY sales DESC) AS rnk
FROM product_sales
)
SELECT category, product_name, sales
FROM ranked
WHERE rnk <= 3
ORDER BY category, rnk;
What to say: “I’m partitioning by category so the ranking restarts in each. I’ve used DENSE_RANK so tied products both appear. If the business wants exactly three rows, I’d switch to ROW_NUMBER with a tiebreaker.”
Behavioral-Style SQL Questions You Should Prepare
Analyst loops often include questions that sound non-technical but test judgment:
-
“A dashboard number looks wrong. What do you do?” Reproduce it, check the filters and date range, compare with a raw count, look for duplicate rows from joins, then check whether the definition changed. Mention talking to the metric’s owner.
-
“Two reports show different revenue. Why?” Likely differences: refunds, time zones, order date vs. payment date, test accounts, currency. Show that you’d reconcile by segmenting until the gap is isolated.
-
“Your query returns in 2 minutes. Is that a problem?” Your answer: “Depends on who uses it. For a one-off analysis, fine. For a daily dashboard, I’d look at filtering early and selecting only needed columns.” Keep it practical, not theoretical.
-
“How would you explain this result to a non-technical stakeholder?” Lead with the answer, then the one caveat that matters, then the recommended action.
Phrases That Signal You’re Careful
Memorise a few, and use them naturally:
- “Can I confirm what one row in this table represents?”
- “How should ties be handled?”
- “Should cancelled or test records be excluded?”
- “I’m making an assumption here; tell me if it’s wrong.”
- “Let me sanity-check this against a total.”
- “That could double-count if a user has multiple orders, so I’ll count distinct users.”
Common Mistakes That Cost Offers
| Mistake | Why it hurts | Fix |
|---|---|---|
| Silently typing for 5 minutes | Interviewer can’t assess thinking | Narrate every step |
| Never asking a question | Looks careless on ambiguity | Ask at least 2 clarifying questions |
COUNT(*) after a one-to-many join | Inflated numbers | Count distinct keys; check grain |
| Ignoring NULLs | Wrong totals, broken filters | Say how NULLs behave |
| Giving up when stuck | Signals low resilience | Solve a simpler version first, then extend |
| Not testing with a tiny example | Bugs go unnoticed | Walk through 3-4 sample rows |
| Over-engineering | Hard to follow | Prefer clear CTEs |
If you’re stuck, say so and think aloud: “Let me solve this for a single customer first, then generalise.” Interviewers reward a visible recovery strategy far more than a silent struggle.
A 7-Day Prep Plan
| Day | Focus | Practice |
|---|---|---|
| 1 | SELECT, WHERE, ORDER BY, NULL behaviour | 5 easy filtering questions |
| 2 | GROUP BY, HAVING, aggregates | Revenue per category, repeat customers |
| 3 | Joins (INNER, LEFT), anti-joins | ”Customers with no orders” style problems |
| 4 | Window functions: ROW_NUMBER, RANK, LAG | Top-N per group, month-over-month |
| 5 | Dates, cohorts, retention | 7-day and 30-day retention |
| 6 | Mock interview, out loud, timed | Use the 6-step framework on 3 questions |
| 7 | Review mistakes, write your phrase list | Redo the questions you fumbled |
The key is Day 6. Practising out loud is the single most underrated preparation step; saying your reasoning feels awkward at first and natural by the third attempt.
Your Pre-Submit Checklist
Before you say “I’m done”:
- Did I restate the metric in plain words?
- Did I confirm definitions (time window, exclusions, ties)?
- Do I know the grain of every table I used?
- Did I handle NULLs and empty groups?
- Is my denominator fair?
- Does the output size and magnitude look plausible?
- Can I explain the result in one sentence to a manager?
Final Thought
Anyone can memorise 100 queries. What gets people hired is the habit behind them: understand the question, define the terms, protect the number, explain the result. Practise that habit and the syntax becomes the easy part.
Ready to practise? Work through the hands-on labs and interview problems on SQLMaster, and apply the 6-step framework to each one, out loud.
Recommended Practice & Interview Guides
Ready to Level Up Your SQL?
Don't just read about databases. Write queries, build schemas, and practice real-world interview questions interactively.