Top 50 SQL Interview Questions with Answers (2026)

Quick Overview
The 50 most commonly asked SQL questions in data science and engineering interviews, grouped by topic: basic queries, joins, window functions, CTEs, aggregations, and optimization. PracHub has 649 SQL questions from companies like Google, Meta, Amazon, and Netflix.
Top 50 SQL Interview Questions with Answers (2026)
SQL interviews follow patterns. After looking at the 649 SQL questions on PracHub, certain question types come up far more often than others. This list focuses on what actually gets asked, not what a textbook would cover.
How to use this list
The questions are grouped by topic and roughly ordered by difficulty within each group. If you are short on time, start with joins and window functions — they appear in almost every SQL interview.
Joins (questions 1-10)
These are table stakes. If you cannot write a LEFT JOIN without thinking, stop here and drill until it is automatic.
- Find all customers who have never placed an order (LEFT JOIN + NULL check)
- Write a query to find the second highest salary in each department
- Join a users table with an orders table to find total spend per user
- Find employees whose salary is above their department average
- Self-join: find all pairs of employees who work in the same department
- Find customers who placed orders in both January and February
- Write a query showing each product and its most recent order date
- Left join three tables: users, orders, and products
- Find users who signed up but never activated their account
- Join with date range: find all orders placed within 7 days of signup
Window Functions (questions 11-20)
Window functions separate junior from senior SQL candidates. Interviewers use them to test whether you can think beyond simple aggregation.
- Calculate a running total of sales by date
- Find the top 3 products by revenue in each category (RANK or ROW_NUMBER)
- Calculate month-over-month revenue growth using LAG
- Find the moving average of daily active users over 7 days
- Rank employees by salary within their department
- Find the difference between each row and the previous row
- Calculate cumulative percentage of total sales
- Find the first and last order for each customer using FIRST_VALUE/LAST_VALUE
- Identify sessions: group events that are within 30 minutes of each other
- Calculate retention: what percentage of users are active 7 days after signup
CTEs and Subqueries (questions 21-30)
Common Table Expressions make complex queries readable. Interviewers want to see that you can break a problem into steps.
- Rewrite a nested subquery as a CTE
- Recursive CTE: build an employee hierarchy from a manager_id column
- Find the longest streak of consecutive login days per user
- Calculate a funnel: signup > activation > first purchase > repeat purchase
- Find duplicate records and keep only the most recent
- Build a cohort table showing retention by signup month
- Chain multiple CTEs to calculate a complex metric step by step
- Find users whose spending increased every month for 3 consecutive months
- Correlated subquery: find orders above the average for their product category
- Use a CTE to pivot rows into columns without PIVOT
Aggregation (questions 31-40)
Basic aggregation is expected. What trips people up is GROUP BY with edge cases and HAVING clauses.
- Find the top 5 customers by total order value
- Count the number of unique products ordered per month
- Calculate the average order value, excluding outliers above the 99th percentile
- Find months where total revenue exceeded 1 million
- Group by multiple columns: category + region + month
- HAVING clause: find departments with more than 10 employees and average salary above 100k
- Count distinct users who performed at least 3 actions in a single day
- Find the mode (most frequent value) of a column
- Conditional aggregation: SUM(CASE WHEN...) to create pivot-like output
- Calculate the median salary (without using PERCENTILE_CONT in databases that lack it)
Data Manipulation and Optimization (questions 41-50)
These come up more in data engineering interviews, but data scientists get them too.
- Write an UPDATE that sets a column based on values from another table
- Delete duplicate rows while keeping one copy
- Insert rows from one table into another with transformation
- Write a MERGE/UPSERT statement
- Explain the difference between DELETE, TRUNCATE, and DROP
- Add an index and explain when it helps vs. hurts
- Rewrite a slow query to avoid a full table scan
- Explain query execution plans: what to look for
- Partition a large table by date and explain the tradeoff
- Write a query that handles NULL values correctly in comparisons and aggregations
Where to practice
PracHub has 649 SQL interview questions with an in-browser SQL editor. The questions are from real interviews at Google, Meta, Amazon, Netflix, and 50+ other companies. You can filter by difficulty and company to focus on what matters for your target role.
Comments (0)