SQL interview questions with practical examples
SQL interview questions usually test more than syntax: you need to translate a requirement into a query and explain why the solution works. Common tasks combine joins, aggregation, subqueries, and window functions.
Updated · SQL Arena team
What you will practice
- Solve top-N-per-group problems
- Find second-highest values safely
- Detect duplicates and missing relationships
- Work with date sequences and gaps
- Write SQL under interview-style time pressure
A practice problem in the trainer
This is how a problem looks in SQL Arena: the task, the editor, the result and the database schema.
For every department in the North region, return the two employees with the highest salaries. Output department_name, employee_name, and salary. Break salary ties by employee_id.
WITH ranked AS ( SELECT e.*, ROW_NUMBER() OVER ( PARTITION BY department_id ORDER BY salary DESC, employee_id ) AS rn FROM employees e ) SELECT d.department_name, r.employee_name, r.salary FROM ranked r JOIN departments d ON d.id = r.department_id WHERE d.region = 'North' AND r.rn <= 2 ORDER BY d.department_name, r.rn;
| # | department_name | employee_name | salary |
|---|---|---|---|
| 1 | Analytics | Noah | 108000 |
| 2 | Analytics | Liam | 99000 |
| 3 | Platform | Emma | 115000 |
| 4 | Platform | Sofia | 104000 |
For every department in the North region, return the two employees with the highest salaries. Output department_name, employee_name, and salary. Break salary ties by employee_id.
WITH ranked AS ( SELECT e.*, ROW_NUMBER() OVER ( PARTITION BY department_id ORDER BY salary DESC, employee_id ) AS rn FROM employees e ) SELECT d.department_name, r.employee_name, r.salary FROM ranked r JOIN departments d ON d.id = r.department_id WHERE d.region = 'North' AND r.rn <= 2 ORDER BY d.department_name, r.rn;
SQL interview question: step-by-step solution
The important phrase is “for every department.” A global ORDER BY salary DESC LIMIT 2 answers a different question: it finds only two employees across the entire company. To keep two rows from each department, the query first assigns a position independently inside each department.
ROW_NUMBER() does that when combined with PARTITION BY department_id. Within every partition, ORDER BY salary DESC gives the largest salary row number 1 and the next one row number 2. employee_id is a tie-breaker, so equal salaries still have a deterministic order. The outer query can then keep rn <= 2.
The departments table is joined after ranking so the result can display department_name and restrict the answer to the North region. The final ORDER BY makes the returned rows predictable. This pattern is worth recognizing in SQL coding interviews because similar questions appear as top products per category, latest events per account, or highest values per group.
Common SQL interview questions usually fall into two groups: conceptual questions and hands-on query tasks. Conceptual questions may cover JOIN behavior, NULL, aggregation, indexes, subqueries, or window functions. Practical questions provide tables and a requirement and ask you to produce the correct result. Difficult SQL interview questions often combine several ideas, such as ranking rows within each group and then joining the ranked result to another table. A strong solution should be correct and also easy to explain step by step.
WITH ranked AS ( SELECT e.*, ROW_NUMBER() OVER ( PARTITION BY department_id ORDER BY salary DESC, employee_id ) AS rn FROM employees e ) SELECT d.department_name, r.employee_name, r.salary FROM ranked r JOIN departments d ON d.id = r.department_id WHERE d.region = 'North' AND r.rn <= 2 ORDER BY d.department_name, r.rn;
| department_name | employee_name | salary |
|---|---|---|
| Analytics | Noah | 108000 |
| Analytics | Liam | 99000 |
| Platform | Emma | 115000 |
| Platform | Sofia | 104000 |
Common mistakes in SQL interview questions
Using LIMIT for a per-group requirement
LIMIT applies to the final result set, not independently to every department. For top-N per group, rank rows inside partitions first and filter the assigned rank afterward.
Leaving ties undefined
ORDER BY salary DESC alone does not specify which equal-salary row comes first. If the problem provides or permits a tie-breaker, include it so the ranking is deterministic and easier to reason about.
Filtering ROW_NUMBER in the same WHERE clause
PostgreSQL does not let the WHERE clause on that SELECT level reference a window result such as rn. Compute the window value in a CTE or subquery, then filter it in the outer query.
Practice problems on this topic
Interview preparation trackA useful SQL interview practice set should force you to recognize different patterns instead of repeating one query with renamed tables. After top-N per group, practice second maximum, duplicates, anti-joins, missing relationships, date gaps, running calculations, and ranking. SQL Arena includes straightforward exercises such as finding records with active related rows, pages with no likes, or entities with unfinished processing, followed by harder tasks involving historical membership, ranking by time spent in a set, and value changes over time. When you want a closer interview simulation, Interview Mode uses a server-side timer, disables hints, and produces a report after the session. One Interview Mode session per day is available for free. Outside that mode, you can use ER diagrams, EXPLAIN, and an AI mentor for hints and error explanations; 10 AI mentor uses are free. Task solutions are checked against hidden data, so a query that only matches the visible sample is not enough. SQL Arena has 1,192 tasks across PostgreSQL, MySQL, ClickHouse, and BigQuery. The free tier includes 292 tasks with no card required, while harder tasks are part of Premium. For a more directed path, the “Interview Preparation” track groups relevant exercises so you can move through common interview patterns without searching the full catalog.
- EasyfreeClients with non-expired applicationspostgresqlmysqlclickhouse
- EasyfreePages with no likespostgresqlmysqlclickhouse
- EasyfreeParts with unfinished assemblypostgresqlmysqlclickhouse
- EasyfreeLaptop and mobile viewspostgresqlmysqlclickhouse
- MediumPremiumBonds that ever entered the portfoliopostgresqlmysqlclickhouse
- MediumPremiumBonds never entered the portfoliopostgresqlmysqlclickhouse
- MediumPremiumRank bonds by days in portfoliopostgresqlmysqlclickhouse
- MediumPremiumPortfolio market-value dynamicspostgresqlmysqlclickhouse
Reference on this topic
In-depth articles with syntax and examples in the SQL Arena reference.
Frequently asked questions
What are common SQL interview questions?
Common topics include JOINs, GROUP BY, aggregate functions, NULL handling, subqueries, and window functions. Interviews may ask conceptual questions, query-writing tasks, or both.
How should I practice SQL interview questions?
Solve queries without looking at the answer first, then explain your reasoning step by step. Practice individual topics before moving to problems that combine joins, aggregation, subqueries, and window functions.
What are difficult SQL interview questions?
Difficult questions often involve top-N rows per group, multiple aggregation stages, window functions, anti joins, or several related tables. The challenge is usually choosing the correct sequence of operations.
What SQL interview questions are common for data engineers?
They can include joins, aggregation, window functions, deduplication, date logic, and transformations across multiple tables. The exact scope varies, but strong query reasoning is usually more important than memorizing unusual syntax.
How do I explain an SQL solution in an interview?
Describe which tables you need, how they relate, when filtering happens, and why you chose each aggregation or window function. Mention important edge cases when they can change the result.
Practice SQL interview questions and query patterns
Work through different interview patterns first, then use Interview Mode to see whether you can recognize the approach, write the query, and verify the result without relying on hints.
Practice SQL InterviewsMore practice from CodeRang
- Python ArenaPython Arena has 1300+ Python 3.12 problems covering functions, classes, async code and technical interview tasks.1300+ tasks · Python 3.12
- Playwright ArenaPlaywright Arena has 30 JavaScript and E2E problems where you write a test and immediately see what it did in the browser.30 tasks · JavaScript / E2E