SQL Interview Questions for Data Scientists (2026)
💼 SQL Cluster — 13 of 15 · Applied & Career

SQL Interview Questions for Data Scientists (2026)

Most SQL interview guides are 60-question dumps you skim once and forget. This one is 18 — the specific questions that recur across FAANG and startup data science interviews year after year, each with what the interviewer is actually evaluating, a working query, and the exact trap candidates fall into. Every trap here maps directly to a mistake already covered earlier in this cluster, because interview questions are, almost without exception, just those same traps in disguise.

by Khalid Hussain Published Aug 01, 2026 🕐 16 min read

Quick Answer: What Actually Gets Asked

▶ Direct Answer — SQL Interview Questions

Data science SQL interviews recur around a small set of patterns: finding a specific rank of value (like the second-highest salary), detecting duplicates, JOIN behavior, window functions for top-N-per-group, subqueries vs CTEs, and — increasingly as of 2026 — talking through query optimization out loud, not just producing a correct answer. Interviewers are testing reasoning under ambiguity far more than syntax memorization, which is exactly why explaining your thinking matters as much as the query itself.

18
questions in this guide — the ones that actually recur
Not a 60-question dump
3
difficulty tiers: beginner, intermediate, advanced
Matches how real interview loops are structured
2026
trend: performance-aware answers weighted more than syntax alone
Live coding rounds are now standard

What Interviewers Are Actually Testing

Nobody hiring a data scientist actually cares whether a candidate has a JOIN syntax memorized — a reference is one search away on the job. What interviewers are evaluating is whether a candidate can take an ambiguous business question, translate it into a structured query, and reason clearly about tradeoffs — including performance, which recent interview trends weight more heavily than they used to, alongside live, timed coding rather than take-home problems.

That reframing changes how to prepare. Memorizing 60 answers verbatim is a weak strategy, because interviewers routinely follow up with "why did you choose that join" or "how would this behave on a 10-million-row table" — questions a memorized answer doesn't survive. Understanding the pattern behind each question, and being able to explain the reasoning out loud, is what actually transfers to a live interview.

Beginner: Fundamentals Check

These confirm a candidate has the core toolkit solid before anything more complex gets asked.

1What's the difference between WHERE and HAVING?
Tests: logical query processing order
WHERE filters rows before grouping; HAVING filters groups after aggregation and can reference aggregate functions that WHERE can't. Covered in full in the GROUP BY and HAVING article.
2Find the second-highest salary without using LIMIT or OFFSET.
Tests: subquery reasoning, portability across databases
sqlSECOND_HIGHEST.SQL
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
This finds the highest salary that's still less than the overall maximum — exactly the second-highest value, and it works identically on any database, unlike LIMIT/OFFSET syntax which varies (covered in the ORDER BY and LIMIT article).
3What's the difference between INNER JOIN and LEFT JOIN?
Tests: whether unmatched rows should disappear or survive
INNER JOIN keeps only rows with a match on both sides; LEFT JOIN keeps every row from the left table regardless of a match. The classic follow-up — "how would you find customers with zero orders" — needs LEFT JOIN plus a NULL check, not INNER JOIN, exactly as covered in the JOINs article.
4How do you find duplicate rows in a table?
Tests: GROUP BY + HAVING as a detection pattern
sqlFIND_DUPLICATES.SQL
SELECT email, COUNT(*) AS occurrences
FROM customers
GROUP BY email
HAVING COUNT(*) > 1;
Grouping by the column that should be unique, then filtering with HAVING COUNT(*) > 1, surfaces every value that appears more than once.
5Why doesn't WHERE email = NULL return the rows you'd expect?
Tests: whether the candidate understands NULL's three-valued logic
NULL represents an unknown value, so any comparison to it using = evaluates to unknown, not true — meaning it never matches, silently. IS NULL is required instead, covered fully in the WHERE clause article.

Intermediate: Joins & Window Functions

This tier tests whether a candidate can handle multi-table logic and the ranking/comparison patterns that show up in almost every real analytics task.

6Find the top 3 highest-paid employees in each department.
Tests: window functions combined with PARTITION BY
sqlTOP_3_PER_DEPT.SQL
WITH ranked AS (
  SELECT name, department, salary,
    ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn
  FROM employees
)
SELECT name, department, salary
FROM ranked
WHERE rn <= 3;
One of the single most frequently asked questions across every company in the competitive research behind this guide. Full breakdown in the window functions article.
7What's the difference between RANK, DENSE_RANK, and ROW_NUMBER?
Tests: tie-handling understanding, not just syntax recall
ROW_NUMBER never ties. RANK ties rows equally but skips the next number by the tie count. DENSE_RANK ties equally with no gap. Interviewers ask this specifically to check whether a candidate understands the difference rather than having memorized three similar-looking function names.
8Find employees who earn more than their own manager.
Tests: self-joins, a common blind spot
sqlSELF_JOIN.SQL
SELECT e.name AS employee, m.name AS manager
FROM employees e
JOIN employees m ON e.manager_id = m.employee_id
WHERE e.salary > m.salary;
A table can join to itself — here, aliased twice as "e" (employee) and "m" (manager) — to compare each employee against their own manager's row in the same table.
9What's the difference between UNION and UNION ALL?
Tests: awareness of a quiet performance cost
UNION removes duplicate rows across the combined result sets, which requires an extra sort or hash step; UNION ALL keeps every row, duplicates included, and is faster. Default to UNION ALL unless duplicates genuinely need to be removed.
10When would you use a CTE instead of a subquery?
Tests: code organization judgment, not just correctness
When the same intermediate result is needed more than once, or when a query has enough sequential steps that naming each one aids readability. Full comparison in the subqueries vs CTEs article.

Advanced: Optimization & Design

This is the tier that's grown the most in emphasis recently — interviewers increasingly care as much about how a query would perform at scale as whether it's correct.

11Walk through how you'd optimize a slow query.
Tests: process, not a single memorized fix
Run EXPLAIN ANALYZE first to see the actual execution plan rather than guessing, check for missing indexes on filtered/joined columns, verify WHERE clauses are sargable, and confirm the fix by re-running the plan. Full walkthrough in the query optimization article.
12Calculate a 7-day running total of daily revenue.
Tests: window function frame clauses
sqlRUNNING_TOTAL.SQL
SELECT order_date, daily_revenue,
  SUM(daily_revenue) OVER (
    ORDER BY order_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
  ) AS rolling_7day
FROM daily_revenue_summary;
Covered with the full frame-clause explanation in the time series article.
13What is the N+1 query problem?
Tests: awareness beyond a single query, into application-level design
Running one query for a list of N items, then a separate query per item — N+1 total round trips instead of one batched query with IN or a JOIN. A strong signal candidate mentions this unprompted when discussing a slow application, not just a slow single query.
14How would you find days with zero user activity in an events table?
Tests: recognizing that GROUP BY silently omits missing dates
A plain GROUP BY on the date only returns dates that appear in the data — zero-activity days are missing, not zero. The fix is generating a complete date series and LEFT JOINing the real data onto it, covered fully in the time series article.
15Explain normalization vs denormalization, and when you'd denormalize.
Tests: database design judgment beyond query writing
Normalization reduces redundancy by splitting data into related tables; denormalization intentionally reintroduces redundancy — often by pre-aggregating into a summary table — to speed up read-heavy analytics workloads at the cost of update complexity. Data warehouses commonly favor some denormalization for exactly this reason.
16SQL vs NoSQL — when would you pick one over the other?
Tests: whether the candidate can reason about tradeoffs, not just recite definitions
SQL for structured, relational data with strong consistency needs and complex joins/aggregations; NoSQL for genuinely unpredictable schemas, simple high-volume key lookups, or graph-shaped data. Full comparison in the SQL vs NoSQL article.

How to Prepare: A Practical Sequence

1

Rebuild the fundamentals from memory

SELECT, WHERE, JOIN, GROUP BY — practice until they're automatic, not something you have to reconstruct under pressure.

2

Drill the recurring patterns

Second-highest-value, duplicate detection, and top-N-per-group cover a disproportionate share of what actually gets asked.

3

Practice explaining reasoning out loud

Say why you picked a join type or window function while you write it — this is what live coding rounds actually evaluate.

4

Prepare an optimization talking point

Mention indexes or execution plans even on questions that don't explicitly ask — it signals production-readiness.

5

Practice on an unfamiliar schema, timed

Most 2026 interviews are live and timed, not untimed take-home problems — simulate that condition in practice.

Mistakes That Give Away a Beginner

  • Jumping straight to code without asking a clarifying question. Real interview questions are often deliberately ambiguous — asking "does this need to include cancelled orders?" is itself a signal of good judgment.
  • Reciting a memorized definition instead of reasoning through the specific scenario. Interviewers routinely follow up with a twist on the original question specifically to check for this.
  • Never mentioning performance on a question involving a large or unspecified table size — a missed opportunity to demonstrate production awareness.
  • Going silent while writing the query. In a live round, narrating your thinking is often weighted as much as the final answer.
  • Not testing the query against edge cases out loud — an empty table, NULLs, ties — even mentioning "I'd also check how this handles ties" is a strong closing signal.

Frequently Asked Questions

How do you find the second-highest salary in SQL without using LIMIT?

A common portable approach is SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees). This works because it finds the highest salary that is still less than the overall highest, which is exactly the second-highest value, and it avoids relying on database-specific LIMIT or OFFSET syntax.

What SQL topics come up most in data science interviews?

JOINs, GROUP BY and HAVING, window functions like ROW_NUMBER and RANK, subqueries versus CTEs, and finding duplicates or top-N-per-group results are the most consistently recurring topics. Query optimization and explaining your reasoning out loud have become more heavily weighted in interviews as of 2026, not just producing a correct final query.

Is it enough to just memorize SQL interview answers?

No. Interviewers are typically evaluating how a candidate reasons through an ambiguous problem, not whether they've memorized a specific query. Being able to explain why a particular JOIN, window function, or optimization choice was made matters as much as the syntax itself, especially in live coding rounds where follow-up questions are common.

How do you find duplicate rows in a SQL table?

Group by the column or columns that should be unique, then filter with HAVING COUNT(*) > 1. This returns every value that appears more than once, which identifies the duplicates without needing to compare rows to each other directly.

What is the difference between RANK, DENSE_RANK, and ROW_NUMBER in an interview context?

ROW_NUMBER assigns a unique number to every row with no ties. RANK gives tied rows the same rank but skips the next number by the number of ties. DENSE_RANK gives tied rows the same rank with no gap afterward. Interviewers frequently ask this specifically to see if a candidate understands tie-handling, not just window function syntax.

Should I bring up query optimization even if the interviewer doesn't ask?

It's generally a strong signal to mention at least briefly, since 2026 interview trends lean more toward performance-aware SQL than earlier years. Noting that you'd check an index or the execution plan on a large table, even without being asked directly, demonstrates the kind of production-readiness interviewers are increasingly screening for.

Conclusion: Patterns Over Memorization

Every question in this guide maps back to a specific mistake or trap covered somewhere earlier in this cluster — NULL handling, join fan-out, the GROUP BY column rule, sargability, gaps-and-islands. That's not a coincidence; interview questions are, almost by design, the moments where those exact misunderstandings surface fastest. Prepare by understanding why each pattern exists, not by memorizing 60 disconnected answers, and practice narrating your reasoning out loud — that's the actual skill being evaluated in a live round.

Work through the 18 questions above until you can write each one without hesitation, then move to the next article in this cluster, which compares the major database platforms you'll likely be asked to work with on the job.

🗃️ Continue the Applied & Career Tier

Next Up: PostgreSQL vs MySQL vs BigQuery vs Snowflake

The interview covers the language — this covers the platforms you'll actually be hired to use it on.

▶  Read the Database Comparison Guide