DEV Community

Feng Zhang
Feng Zhang

Posted on • Originally published at prachub.com

Top 50 SQL Interview Questions with Answers (2026)

SQL interviews are predictable in one useful way, the same patterns show up again and again.

PracHub reviewed 649 SQL interview questions and pulled out the topics that come up most often. The original list, "Top 50 SQL Interview Questions with Answers (2026)", is a solid map of what companies actually ask, not a textbook walk through SQL syntax.

If you are preparing for interviews, this is where to focus.

Start with joins and window functions

If your prep time is limited, spend it on joins first, then window functions.

Those two areas show up in almost every SQL interview because they show how you think. Can you combine datasets cleanly? Can you answer analytical questions without writing five nested queries? Can you handle real business logic instead of toy examples?

If a LEFT JOIN still takes you a minute to think through, stop and drill it until it is automatic.

1) Joins: table stakes

These are the questions that should feel routine.

Typical join questions include:

  1. Find all customers who have never placed an order, usually with a LEFT JOIN and a NULL check
  2. Find the second highest salary in each department
  3. Join users and orders to calculate total spend per user
  4. Find employees whose salary is above their department average
  5. Use a self-join to find pairs of employees in the same department
  6. Find customers who placed orders in both January and February
  7. Show each product and its most recent order date
  8. LEFT JOIN three tables such as users, orders, and products
  9. Find users who signed up but never activated
  10. Join on a date range, such as orders placed within 7 days of signup

Why interviewers like these questions:

  • They test whether you understand join types
  • They expose weak handling of NULLs
  • They show whether you can translate business rules into SQL

A lot of candidates know INNER JOIN and freeze when the problem needs anti-joins, self-joins, or date conditions. That gap matters.

2) Window functions: where difficulty jumps

Window functions are where interviews often separate junior and senior candidates.

You can get pretty far with GROUP BY, but many interview questions need row-level context and aggregate context at the same time. That is what window functions are for.

Common examples:

  1. Running total of sales by date
  2. Top 3 products by revenue in each category with RANK or ROW_NUMBER
  3. Month-over-month revenue growth with LAG
  4. Moving average of daily active users over 7 days
  5. Rank employees by salary within department
  6. Difference between each row and the previous row
  7. Cumulative percentage of total sales
  8. First and last order for each customer with FIRST_VALUE or LAST_VALUE
  9. Sessionization, grouping events within 30 minutes of each other
  10. Retention, such as percentage of users active 7 days after signup

These questions test whether you understand partitions, ordering, and window frames. They also test whether you know when a window function is better than a subquery.

If you want one strong signal for interview readiness, it is this: can you write a correct LAG, ROW_NUMBER, or running total query without trial and error?

3) CTEs and subqueries: can you break a hard problem into steps?

A lot of SQL interview questions are not hard because of syntax. They are hard because the logic has multiple stages.

That is where CTEs help. They let you structure a query in chunks that another person can actually read.

Questions in this group include:

  1. Rewrite a nested subquery as a CTE
  2. Build an employee hierarchy with a recursive CTE
  3. Find the longest streak of consecutive login days per user
  4. Calculate a funnel: signup to activation to first purchase to repeat purchase
  5. Find duplicates and keep only the most recent row
  6. Build a cohort table by signup month
  7. Chain multiple CTEs to calculate a metric step by step
  8. Find users whose spending increased every month for 3 straight months
  9. Use a correlated subquery to find orders above the average for their product category
  10. Pivot rows into columns with a CTE, without using PIVOT

This category matters because interviewers are watching how you organize a solution.

Messy SQL is often a sign of messy thinking. A clean chain of CTEs tells the interviewer that you can take a vague analytics question and turn it into a clear sequence of steps.

4) Aggregation: basic, but easy to get wrong

Aggregation questions look simple, then punish sloppy thinking.

Most people can write GROUP BY customer_id. The mistakes happen around edge cases, filtering, distinct counts, and post-aggregation conditions.

Common prompts:

  1. Top 5 customers by total order value
  2. Count unique products ordered per month
  3. Average order value excluding outliers above the 99th percentile
  4. Months where revenue exceeded 1 million
  5. Group by category, region, and month
  6. Departments with more than 10 employees and average salary above 100k using HAVING
  7. Distinct users who performed at least 3 actions in one day
  8. Find the mode of a column
  9. Conditional aggregation with SUM(CASE WHEN ...)
  10. Median salary in databases that do not support PERCENTILE_CONT

This is where candidates often mix up WHERE and HAVING, forget COUNT(DISTINCT ...), or write queries that work only for the happy path.

If your SQL tends to break on NULLs, ties, or duplicate rows, aggregation questions will expose it fast.

5) Data manipulation and optimization: more common in some roles, still fair game

These show up more in data engineering interviews, but data scientists and analysts see them too.

Topics include:

  1. UPDATE a table using values from another table
  2. Delete duplicate rows while keeping one copy
  3. Insert transformed rows from one table into another
  4. Write a MERGE or UPSERT
  5. Explain DELETE vs TRUNCATE vs DROP
  6. Add an index and explain when it helps or hurts
  7. Rewrite a slow query to avoid a full table scan
  8. Explain what to look for in a query execution plan
  9. Partition a large table by date and explain the tradeoff
  10. Handle NULL values correctly in comparisons and aggregations

This section matters because interviews are not always pure query-writing exercises. Sometimes you need to explain behavior, tradeoffs, or performance.

A candidate who can write SQL and talk through why a query is slow usually comes across much stronger than someone who can only produce syntax.

How to use this list well

Do not treat these 50 prompts like trivia cards.

Use them as a prioritization tool:

  • Start with joins and window functions
  • Practice writing answers from scratch, without autocomplete
  • Focus on correctness first, then readability
  • For each question, know the common failure mode

A few examples of failure modes worth watching:

  • Returning duplicate rows after a join
  • Using INNER JOIN where a LEFT JOIN is needed
  • Filtering aggregated results in WHERE instead of HAVING
  • Confusing RANK() and ROW_NUMBER()
  • Mishandling NULL comparisons
  • Solving a window function question with a slow, tangled subquery

That last point matters. Interviewers usually care about your approach, not just whether the final query runs.

Practice on real interview-style questions

If you want more than a checklist, PracHub has a broader set of SQL interview practice questions with an in-browser SQL editor. The source article says the platform includes 649 SQL interview questions and lets you filter by difficulty and company.

That is useful because SQL prep gets better when you move from reading solutions to actually writing them under mild pressure.

And if you want the full categorized list in one place, go back to the original PracHub post: "Top 50 SQL Interview Questions with Answers (2026)".

The main takeaway is simple. SQL interviews are less random than they look. If you get strong at joins, window functions, CTEs, aggregation, and basic optimization, you are covering most of what interviewers keep asking.

Top comments (0)