SQL interview questions and answers
Worked questions grouped by topic and tagged easy, medium or hard. Every answer is a real query with the reasoning written out. Read a topic, then use practice mode to test yourself on it.
Question sets by topic
Seven topics and 84 questions, each with a worked answer in real SQL. Start with the topic your interview is likely to open with, or go to practice mode for a shuffled deck.
Practice mode
All 84 questions in one shuffled deck. Read the question, answer it out loud, then reveal the worked answer. Filter to easy, medium or hard.
Start practisingSQL basics
NULL handling, UNION vs UNION ALL, keys, CASE expressions and the logical order of a query.
- 8 easy
- 4 medium
- 0 hard
SQL joins
INNER and LEFT JOIN, anti-joins, self joins, join fan-out mistakes and LATERAL top-n queries.
- 4 easy
- 6 medium
- 2 hard
Aggregation & HAVING
WHERE vs HAVING, COUNT differences, conditional aggregation and GROUP BY errors.
- 3 easy
- 7 medium
- 2 hard
Window functions
OVER, ROW_NUMBER vs RANK, running totals, LAG, frames and deduplication.
- 1 easy
- 8 medium
- 3 hard
Subqueries & CTEs
IN vs EXISTS, correlated subqueries, recursive CTEs and the NOT IN NULL trap.
- 4 easy
- 7 medium
- 1 hard
Indexes & performance
Composite index order, covering indexes, sargability and EXPLAIN.
- 2 easy
- 8 medium
- 2 hard
Schema design & normalization
1NF 2NF and 3NF, surrogate vs natural keys, many-to-many tables and denormalization.
- 2 easy
- 8 medium
- 2 hard
Every question at a glance
Each link opens the question on its topic page with the worked answer underneath.
SQL basics
- What is the logical evaluation order of a SELECT query? Средний
- What does NULL mean, and why does NULL = NULL not return true? Easy
- How do you count or sum correctly when NULLs are involved? Easy
- What is the difference between UNION and UNION ALL? Easy
- What does DISTINCT do, and when can it hide a bug? Easy
- What is the difference between DELETE and TRUNCATE? Easy
- What is the difference between CHAR, VARCHAR and TEXT? Easy
- What is the difference between a PRIMARY KEY and a UNIQUE constraint? Средний
- How does pagination work, and why is OFFSET slow on deep pages? Средний
- What does the BETWEEN operator do, and what is the classic date bug? Easy
- What does ORDER BY guarantee, and why add a tiebreaker? Средний
- How does a CASE expression work, and where can it be used? Easy
SQL joins
- What is the difference between an INNER JOIN and a LEFT JOIN? Easy
- Write a query that returns each customer and their order count, including customers with zero orders. Easy
- What is join fan-out, and how does it corrupt aggregates? Средний
- How do you find rows in one table with no match in another (an anti-join)? Средний
- What does a FULL OUTER JOIN do, and when would you use it? Средний
- What is a CROSS JOIN, and what is a legitimate use? Easy
- What is a self join, and how do you list each employee with their manager? Средний
- What changes when you move a filter from the ON clause to WHERE in a LEFT JOIN? Средний
- What happens when you join on a key that has duplicates on either side? Easy
- How do you join a table to an aggregate of another table without fan-out? Средний
- How do you get the top three orders per customer? Hard
- How do you emulate a FULL OUTER JOIN in MySQL? Hard
Aggregation & HAVING
- What is the difference between WHERE and HAVING? Easy
- What is the difference between COUNT(*), COUNT(column) and COUNT(DISTINCT column)? Easy
- Write total revenue per order status. Easy
- Return customers with more than five completed orders. Средний
- Filter groups by an aggregate that never appears in the SELECT list. Средний
- What does GROUP BY with multiple columns mean? Средний
- Group by month: why can you not just GROUP BY order_date, and what do you do instead? Средний
- Why does selecting a column that is not in GROUP BY fail? Средний
- Count orders of each status in one pass over the table. Hard
- How do WHERE and HAVING interact in a query with a JOIN? Средний
- Group customers into spend buckets and count per bucket. Hard
- How do you concatenate one column values per group, like all product names per order? Средний
Window functions
- What does AVG(salary) OVER () do, and how is it different from GROUP BY? Easy
- What is the difference between ROW_NUMBER, RANK and DENSE_RANK? Средний
- Return the highest-paid employee in each department. Средний
- Compute a running total of order revenue by date. Средний
- What do LAG and LEAD do? Show month-over-month revenue change. Средний
- Compute each employee's share of their department salary bill. Средний
- Compute a 3-day moving average of daily revenue. Hard
- Split employees into four salary quartiles with NTILE. Средний
- Gaps and islands: find each streak of consecutive active days per user. Hard
- Why does putting a window function in WHERE fail, and what do you do instead? Средний
- What does FIRST_VALUE do, and what is the LAST_VALUE trap? Средний
- Delete duplicate rows, keeping the latest one per natural key. Hard
Subqueries & CTEs
- What is a scalar subquery, and where can it appear? Easy
- What is the difference between IN and EXISTS? Easy
- Write a correlated subquery: return orders above the average for their own customer. Средний
- Why can NOT IN return no rows when the subquery contains a NULL? Средний
- What is a derived table, and when is one needed? Easy
- What is a CTE, and why use one instead of a subquery? Средний
- Write a recursive CTE that walks the employee manager hierarchy. Hard
- Return the second-highest salary per department. Show two ways. Средний
- What is a semi-join, and how do you write one? Средний
- When can the optimizer flatten a subquery into a JOIN, and when not? Средний
- How does a subquery in FROM differ from a view? Средний
- What does the IN operator with a literal list do, and what are its edge behaviors with NULL? Easy
Indexes & performance
- What is an index, and what does it cost? Easy
- You have an index on (city, signup_date). Which queries can use it? Средний
- What does sargable mean? Why does WHERE LOWER(email) = ? ignore the index? Средний
- What is a covering index, and what is an index-only scan? Средний
- What is the difference between a clustered and a non-clustered index? Easy
- Why can LIKE '%term' not use a B-tree index, and what are the alternatives? Средний
- When does an index hurt, and how do you decide whether to keep one? Средний
- What is a partial index, and when is it the right choice? Hard
- How do you read an EXPLAIN plan? What are the red flags? Средний
- The optimizer is ignoring your index. What are the likely causes? Hard
- Why do foreign keys need their own indexes, and what breaks without them? Средний
- Why is SELECT * discouraged, beyond style? Средний
Schema design & normalization
- What is first normal form (1NF)? Easy
- Explain second and third normal form with a concrete example. Средний
- Surrogate keys or natural keys: how do you choose? Easy
- How do you model a many-to-many relationship? Средний
- How do you model an employee manager relationship in the same table? Средний
- When is denormalization justified? Средний
- How would you store hierarchical data? Compare adjacency list, nested sets and closure table. Hard
- When should a column be NULLable, and what are the tradeoffs? Средний
- How do you model a one-to-one relationship? Средний
- How do you store data that changes over time (temporal / versioned data)? Hard
- What is the EAV pattern, and why is it dangerous? Средний
- How much integrity should live in the schema versus the application? Средний
SQL interview questions FAQ
Are these SQL interview questions free?
Yes. Every topic page, every worked answer and the practice mode are free, with no sign-up. SQLly makes a desktop SQL workbench, and these pages are the notes we would want before a SQL screen.
What SQL dialect are the answers in?
Standard SQL where it exists. Dialect-specific examples are PostgreSQL unless marked otherwise, and the answers name the SQL Server, MySQL and SQLite spelling where they differ: limit clauses, date truncation, string aggregation, recursive CTE syntax and QUALIFY.
How many questions are there, and how are they organized?
More than 80 questions across seven topics: basics, joins, aggregation and HAVING, window functions, subqueries and CTEs, indexes and performance, and schema design. Each question is tagged easy, medium or hard.
What is practice mode?
A quiz view that draws from every question in the catalog, shows the question first and reveals the worked answer on click. You can shuffle the deck and filter by difficulty. It runs in your browser and keeps answered state in memory, so a reload starts over.
What level are these questions for?
Easy questions are junior warm-ups, medium questions are the standard mid-level screen, and the hard ones are the senior follow-ups: frame traps, recursive CTEs, join flattening and hierarchy storage.
Now write some SQL against a real database
SQLly is a native SQL workbench for macOS, Windows and Linux: IntelliSense that knows your schema, a formatter, guardrails for risky queries, and every engine in one app.