SQL 50: SQL interview practice questions
50 questions of the kind asked in interviews for data analysis, finance and developer roles, from a cancellation rate to gaps and islands. You write every query yourself, on real data in your browser, and find out straight away whether it is right.
In short: 17 basic, 17 intermediate and 16 advanced questions from four areas: online shop, finance, marketing and HR. Filter by difficulty and topic, see what you have already solved, and time yourself with the stopwatch. The first question of each level is free.
All 50 questions
Click a question to open it on its level page. A tick shows what you have already solved: from this browser without an account, from any device with one. On the right you see your best time if you solved the question with the stopwatch running.
50 questions
-
Basic
- Cancellation rate as a percentagePercentages · Aggregation and HAVING · Online shop
- Signed up but never orderedAnti-join · Dates · Marketing
- Overdue invoices with no paymentAnti-join · Dates · Finance
- Payments imported twiceDuplicates · Aggregation and HAVING · Finance
- Who doesn't report to Tom Bradley?NULL traps · Filtering · HR
- Who is still on probation?Dates · Filtering · HR
- Campaigns that ran in OctoberDates · Filtering · Marketing
- Orders at the weekendDates · Aggregation and HAVING · Online shop
- Customers with at least three ordersAggregation and HAVING · Online shop
- Average lines per orderAggregation and HAVING · Subqueries and EXISTS · Online shop
- Text after the commaText · Online shop
- Big basketsAggregation and HAVING · JOIN · Online shop
- Expenses by account in Q1JOIN · Aggregation and HAVING · Dates · Finance
- Order size bandsAggregation and HAVING · Online shop
- Customers per city, including no cityNULL traps · Aggregation and HAVING · Marketing
- Revenue by campaign, including noneNULL traps · JOIN · Aggregation and HAVING · Marketing
- Finding short payment termsDates · Filtering · Finance
-
Intermediate
- Earning more than the managerJOIN · HR
- Top salary per departmentRanking and top N · Subqueries and EXISTS · HR
- Deleting duplicate paymentsDuplicates · Subqueries and EXISTS · Finance
- Gaps in invoice numbersGaps and islands · LAG and LEAD · Finance
- June versus May by product groupLAG and LEAD · Percentages · Finance
- Customers in both yearsSubqueries and EXISTS · Dates · Marketing
- Revenue by quarterDates · Text · Aggregation and HAVING · Finance
- The most common first namesText · Aggregation and HAVING · Marketing
- Last order cancelledRanking and top N · Subqueries and EXISTS · Online shop
- Top 3 products per categoryRanking and top N · Online shop
- Slow movers in Q3Anti-join · Dates · Online shop
- Cross-selling: pans yes, knives noSubqueries and EXISTS · Anti-join · Marketing
- Days to the first orderDates · CTE (WITH) · Marketing
- Cart abandonment by deviceAggregation and HAVING · Percentages · CTE (WITH) · Marketing
- Revenue from new and returning customersCTE (WITH) · Ranking and top N · Marketing
- Bank balance at month endRunning totals · Aggregation and HAVING · Finance
- Top two customers per quarterRanking and top N · Aggregation and HAVING · Finance
-
Advanced
- Median order valueRanking and top N · Aggregation and HAVING · Online shop
- From which order is someone a regular?Running totals · Ranking and top N · Marketing
- Quarter-to-quarter retentionLAG and LEAD · Percentages · CTE (WITH) · Marketing
- Session length by deviceDates · CTE (WITH) · Aggregation and HAVING · Marketing
- Growth versus the same month last yearLAG and LEAD · Percentages · Dates · Online shop
- Monthly streaks of regular customersGaps and islands · Ranking and top N · Dates · Marketing
- Revenue share of the top ten customersPercentages · Ranking and top N · CTE (WITH) · Marketing
- Continuous sickness absenceGaps and islands · Dates · JOIN · HR
- Salary as of a reference dateDuplicates · Dates · Ranking and top N · HR
- Latest pay change as a percentageLAG and LEAD · Percentages · HR
- Payment delay per customerDates · CTE (WITH) · Aggregation and HAVING · Finance
- Budget peak per cost centreCTE (WITH) · Ranking and top N · Finance
- Longest growth streak per productLAG and LEAD · Gaps and islands · Finance
- Revenue over the last 30 daysRunning totals · Dates · Online shop
- Repeat purchase within 60 daysPercentages · Dates · CTE (WITH) · Marketing
- Overlapping campaigns in the same channelJOIN · Dates · Marketing
No question matches this filter.
How SQL 50 works
- Basic (questions 1 to 17): filtering, counting, NULL, dates, text, simple joins. Each question has a typical trap, such as integer division in a percentage.
- Intermediate (18 to 34): self join, top 3 per group, gaps in invoice numbers, comparing with the previous month, deleting duplicates.
- Advanced (35 to 50): median, gaps and islands, retention, rolling 30-day totals, salary as of a date, unpivot.
The result is checked, on hidden data too
How you write the query is up to you: NOT EXISTS or LEFT JOIN, a CTE or a subquery. What gets checked are the columns, rows and values, first on the data you can see, then on a slightly changed copy you can't. Typing in a result or forgetting a condition that happens not to matter for the visible data shows up there. If something is off, the message tells you whether rows are missing, values differ or the sort order is wrong.
The follow-up question is part of it
In an interview, a SQL question rarely ends with the right query. There is almost always a follow-up: what happens with ties? What if a month is missing? How would you do it without a window function? That's why every model answer comes with an explanation that includes the typical follow-up and a short answer.
Stopwatch
You can start a stopwatch above every question. It stops as soon as the question is solved and remembers your best time. The times stay in your browser, are never sent to the server and have no effect on points, streak or certificate.
Points and Premium
As with every exercise, you earn points the first time you solve a question: 10 for a basic, 15 for an intermediate and 20 for an advanced one, half with a hint, none after showing the answer. The first question of each level is free, the rest are part of Premium. You can read every question and its tables without an account.
The data
All questions run on the practice databases of the SQL course. Names, companies and figures are made up.
- Online shop: an office supplies shop with customers, orders and order lines, a sales list covering one year, and a kitchenware shop with orders and web tracking.
- Finance: a bike accessories manufacturer with ledger entries, cost centres, budget, sales, invoices and payments.
- Marketing: a sporting goods shop with campaigns, customers, orders and sessions.
- HR: the office supplies shop's employee table, extended with sick days and a salary history for some questions.
The databases are also available to download for SQLite, MySQL and PostgreSQL: sample database.
Tips for live SQL questions
- Ask before you write: what is one row of the table? Do cancelled orders count? What happens with ties?
- Start small: write the first CTE and run it, then add the next. Mistakes show up sooner.
- Check the result: is the row count roughly right? Does someone appear twice because a join multiplied rows?
- Name the edge cases: NULL, duplicates, a missing month. Bringing them up yourself shows experience.
The concept questions about indexes, transactions or WHERE versus HAVING are on the SQL interview questions page with short answers, together with the classic "second-highest salary" as an exercise. If you'd like to gauge your level first, take the SQL assessment test.
FAQ
Are these questions from real interviews?
No. They are typical patterns that come up again and again in SQL interviews, such as customers with no orders, month-over-month growth, top 3 per group, duplicates or repeat purchases. Every question is written from scratch for the practice data on this site. We don't know which questions a particular company uses.
In what order should I practise?
If you are already confident with GROUP BY and JOIN, you can start with the intermediate questions. Otherwise it's worth starting at question 1: the basic questions have traps that cost points in an interview too. The "Open questions only" filter shows what you still have left.
Which SQL database is used?
SQLite. The answers work almost the same everywhere; where a function has a different name in MySQL, PostgreSQL or SQL Server, the explanation gives the spelling there.
Is SQL 50 free?
The first question of each level is free, the other 47 are part of Premium. You can read every question with its tables without an account.
Do the questions count towards the SQL certificate?
No. The certificate requires the exercises of the course levels and the final test. SQL 50 is an extra practice set; points and your streak count as with any exercise.