Get Started Free

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

Core

Joins, anti-joins, aggregation, lists and structs, ranking, dirty dates.

  1. 1Revenue by categoryJOINGROUP BYwindow over an aggregateMedium
  2. 2Top 5 customersJOINCOUNT(DISTINCT)LIMITMedium
  3. 3Customers who never returned anythinganti-joinNOT EXISTSMedium
  4. 4Busiest day of the weekdate functionsaverage of totalssubqueryMedium
  5. 5Average order value by countrytwo-level aggregationAVGMedium
  6. 6Revenue by customer tagUNNESTLIST columnsLEFT JOINHard
  7. 7Read a nested spec sheetSTRUCTLIST functionsNULL handlingMedium
  8. 8Second most expensive in each categoryDENSE_RANKQUALIFYtiesHard
  9. 9Orders before sign-updata qualityjoin conditiondate_diffHard
  10. 10Return rate by productconditional aggregationHAVINGpercentagesMedium

Advanced

Window functions, gaps and islands, sessions, funnels, ASOF joins, percentiles, date spines.

  1. 11Monthly revenue and running totalwindow functionsrunning totaldate_truncHard
  2. 12Month-over-month growthLAGpercent changeNULL for the first rowHard
  3. 13Top 2 sellers in every categoryRANKPARTITION BYQUALIFYHard
  4. 14Longest streak of order daysgaps and islandsROW_NUMBERdate arithmeticExpert
  5. 15Rebuild sessions from raw eventssessionizationLAGrunning SUMExpert
  6. 16The checkout funnelconditional aggregationbool_orfunnelsHard
  7. 17Convert at the rate of the dayASOF JOINpoint-in-time lookupscurrenciesExpert
  8. 18Page load percentilesquantile_contnested STRUCTGROUP BY ALLHard
  9. 19Did they come back within 30 days?LEADintervalssingle-row summaryHard
  10. 20Frequently bought togetherself-joinpairsmarket basketExpert
  11. 21Every day of December, including the quiet onesdate spinerange()LEFT JOINHard
  12. 22Customer spend quartilesNTILEbucketssummary per bucketHard

Performance

The same answer, written so the query plan is efficient. Graded on the result and the plan.

  1. 23Previous order without a self-joinwindow vs self-joinLAGquery planHard
  2. 24Latest order per customerQUALIFYarg_maxgreatest-n-per-groupHard
  3. 25Pivot in one passFILTER clausePIVOTsingle scanMedium
  4. 26Aggregate before you joinjoin orderpre-aggregationquery planHard
  5. 27Keep the date filter pushablefilter pushdownsargable predicateszone mapsMedium
  6. 28Abandoned carts in one passconditional aggregationarg_minsingle scanHard
  7. 29Ten slowest page loadsTop-NORDER BY + LIMITquery planMedium
  8. 30ASOF JOIN instead of a correlated lookupASOF JOINcorrelated subqueriesquery planExpert

The dataset

Every question uses these tables. Open one to see its columns.

customers20 rows
idINTEGER
primary key
nameVARCHAR
countryVARCHAR
currencyVARCHAR
EUR, GBP, USD or JPY
signed_upDATE
tagsVARCHAR[]
a list: vip, newsletter, b2b, returns-often
products10 rows
idINTEGER
primary key
nameVARCHAR
categoryVARCHAR
priceDECIMAL(10,2)
EUR
specsSTRUCT
weight_g INTEGER, color VARCHAR, sizes VARCHAR[]
orders300 rows
idINTEGER
primary key
customer_idINTEGER
โ†’ customers.id
placed_atTIMESTAMP
2024
statusENUM
placed, paid, shipped, delivered, returned
currencyVARCHAR
the customer's currency
order_items576 rows
order_idINTEGER
โ†’ orders.id
product_idINTEGER
โ†’ products.id
quantityINTEGER
unit_priceDECIMAL(10,2)
EUR
web_events20,000 rows
event_idBIGINT
tsTIMESTAMP
session_idVARCHAR
customer_idINTEGER
NULL for anonymous visitors
event_typeVARCHAR
page_view, search, add_to_cart, checkout
pageSTRUCT
path, referrer
deviceSTRUCT
browser, mobile BOOLEAN, screen STRUCT(w, h)
product_idsINTEGER[]
load_msINTEGER
fx_rates720 rows
currencyVARCHAR
USD, GBP, JPY
valid_fromTIMESTAMP
per_eurDOUBLE
units of currency per 1 EUR

Practicing MongoDB too? Try the MongoDB interview questions.

VisuaLeaf for SQL

Write these queries on your own databases

VisuaLeaf's SQL editor, visual query builder and explain plans work against PostgreSQL, MySQL, SQL Server, DuckDB and more.