SQL Interview Questions
30 hands-on practice problems that run on a real DuckDB database in your browser. Write the query, hit Check answer, and see right away if it's correct and, for the performance questions, if the plan is efficient.
0 of 30 solved ยท saved in this browser
- A real datasetAn online store: customers, products, 300 orders, 20,000 web events with nested fields, and exchange rates. Including anonymous visitors and a bad import.
- Real DuckDB SQLWindow functions,
QUALIFY,ASOF JOIN, lists and structs. Nothing to install, nothing leaves your browser. - Hidden test casesEvery answer also runs on hidden datasets with more customers and orders, so it has to be right, not just match what you can see.
Core
Joins, anti-joins, aggregation, lists and structs, ranking, dirty dates.
- 1Revenue by categoryMedium
- 2Top 5 customersMedium
- 3Customers who never returned anythingMedium
- 4Busiest day of the weekMedium
- 5Average order value by countryMedium
- 6Revenue by customer tagHard
- 7Read a nested spec sheetMedium
- 8Second most expensive in each categoryHard
- 9Orders before sign-upHard
- 10Return rate by productMedium
Advanced
Window functions, gaps and islands, sessions, funnels, ASOF joins, percentiles, date spines.
- 11Monthly revenue and running totalHard
- 12Month-over-month growthHard
- 13Top 2 sellers in every categoryHard
- 14Longest streak of order daysExpert
- 15Rebuild sessions from raw eventsExpert
- 16The checkout funnelHard
- 17Convert at the rate of the dayExpert
- 18Page load percentilesHard
- 19Did they come back within 30 days?Hard
- 20Frequently bought togetherExpert
- 21Every day of December, including the quiet onesHard
- 22Customer spend quartilesHard
Performance
The same answer, written so the query plan is efficient. Graded on the result and the plan.
The dataset
Every question uses these tables. Open one to see its columns.
customers20 rows
id | INTEGER primary key |
name | VARCHAR |
country | VARCHAR |
currency | VARCHAR EUR, GBP, USD or JPY |
signed_up | DATE |
tags | VARCHAR[] a list: vip, newsletter, b2b, returns-often |
products10 rows
id | INTEGER primary key |
name | VARCHAR |
category | VARCHAR |
price | DECIMAL(10,2) EUR |
specs | STRUCT weight_g INTEGER, color VARCHAR, sizes VARCHAR[] |
orders300 rows
id | INTEGER primary key |
customer_id | INTEGER โ customers.id |
placed_at | TIMESTAMP 2024 |
status | ENUM placed, paid, shipped, delivered, returned |
currency | VARCHAR the customer's currency |
order_items576 rows
order_id | INTEGER โ orders.id |
product_id | INTEGER โ products.id |
quantity | INTEGER |
unit_price | DECIMAL(10,2) EUR |
web_events20,000 rows
event_id | BIGINT |
ts | TIMESTAMP |
session_id | VARCHAR |
customer_id | INTEGER NULL for anonymous visitors |
event_type | VARCHAR page_view, search, add_to_cart, checkout |
page | STRUCT path, referrer |
device | STRUCT browser, mobile BOOLEAN, screen STRUCT(w, h) |
product_ids | INTEGER[] |
load_ms | INTEGER |
fx_rates720 rows
currency | VARCHAR USD, GBP, JPY |
valid_from | TIMESTAMP |
per_eur | DOUBLE units of currency per 1 EUR |
Practicing MongoDB too? Try the MongoDB interview questions.