SQL interview questions, with a live database to run them on
Most SQL interview question lists give you the question and a pasted answer. That does not prepare you for the screen where you have to write it, run it and get the right rows. Every question below opens an exercise with the tables loaded, so you write the query, run it, and get told whether it is right, including on data you cannot see.
What SQL interviews actually test
Across analyst, data engineer and backend screens the question types repeat. Interviewers want to see that you can join without duplicating rows, aggregate with the right GROUP BY, filter aggregates with HAVING, and reach for a window function when a ranking or running total is asked. The hardest questions combine two of these.
- Filtering and sorting: WHERE with several conditions, IN, LIKE, NULL handling, ORDER BY on two keys, LIMIT.
- Aggregation: COUNT, SUM, AVG, MIN and MAX with GROUP BY; HAVING to filter groups; COUNT DISTINCT.
- Joins: inner versus left, finding rows with no match, self-joins for manager and employee, joining three tables.
- Subqueries and CTEs: rows above the average, the second or third highest value, breaking a query into named steps.
- Window functions: RANK and DENSE_RANK, running totals, LAG for month-over-month change, share of total.
- Data cleaning: duplicates, text stored in inconsistent cases, dates stored as strings.
Warm-up questions: SELECT, WHERE, ORDER BY
These are the questions a screen opens with. Get them right fast; interviewers notice hesitation here more than anywhere.
- Pick two columns Free
- Filter rows Free
- Top three earners Free
- Each country once Free
- Two countries Pro
- Pattern match Pro
- No manager Pro
- Sort on two columns Pro
- Label with CASE Pro
GROUP BY and HAVING questions
Count per group, total per group, groups above a threshold. The classic mistake is putting an aggregate condition in WHERE instead of HAVING.
JOIN questions
"Customers with no orders" is the single most asked SQL interview question; it is a LEFT JOIN with a NULL check, or NOT EXISTS. The self-join (employees and their managers) is the second.
Subquery and CTE questions
Second highest salary, employees above the average, department above the company average. These separate people who know syntax from people who can decompose a problem.
Window function questions
Rank within a group, running total, month-over-month change with LAG, top two per category, percentage of total. Window functions are what senior screens use to find the ceiling.
Data-cleaning questions
Real screens increasingly hand you a messy table. Finding duplicates, normalizing inconsistent country names, and summing revenue stored as text are all here.
How to prepare in a week
- Days 1 and 2: Basics and Aggregation until GROUP BY and HAVING are automatic.
- Days 3 and 4: Joins. Do the no-match and self-join questions until you can write them without thinking.
- Day 5: Subqueries and CTEs. Rewrite each subquery answer as a CTE and vice versa.
- Day 6: Window functions. Rank, running total, LAG, top-N per group.
- Day 7: Timed mode across everything you failed on the first try.
Run the questions on a real database
Six exercises are free without an account. Pro unlocks all 60, with hidden-data checks, hints, worked solutions and progress saved to your account.
Start practicing freeQuestions
Which SQL dialect is used?
SQLite, running in your browser. The questions use standard SQL that works unchanged in PostgreSQL, MySQL and SQL Server, including window functions and CTEs.
How are answers checked?
Your query runs against the tables you see and again against a hidden copy with different rows, so a query that only works by coincidence is caught. Row order is checked only when the question asks for it.
Are the answers shown?
A hint is available at any time, and a worked solution opens after two attempts, or straight away once solved. In timed mode both stay closed until the clock runs out.
Is this enough to pass a SQL screen?
It covers the question types that appear in analyst, data engineer and backend screens. Pair it with practice explaining your query out loud, since most interviews ask you to talk through it.