π Interview Prep
SQL & Databases
Joins, GROUP BY, subqueries, CTEs and window functions, run on real tables in your browser. Asked for back-end, data and full-stack roles.
π Learn it from scratch
Visual, hands-on lessons for every pattern in this topic. Do the lesson, then come back and the questions click.
100 questions, most asked first
Ordered so the most useful questions for learning each pattern come first.
All patternsSubqueriesWindow functionsJoinsGroup bySelf joins & CTEsChanging dataDates & stringsCASE & NULLSorting & limitsAggregatesFilteringBasicsIndexes & transactionsSchema design
#1πSecond-Highest SalarySubqueriesMediumFree#2πNth-Highest Salary (with DENSE_RANK)Window functionsMediumFree#3πCustomers Who Never OrderedJoinsEasyFree#4πHighest-Paid Employee in Each DepartmentWindow functionsMediumFree#5πFind Duplicate EmailsGroup byEasyFree#6πEmployees Earning More Than Their ManagersSelf joins & CTEsEasyFree#7πAverage Salary by DepartmentGroup byEasyFree#8πTop 3 Salaries in Each DepartmentWindow functionsHardFree#9π¬INNER vs LEFT vs RIGHT vs FULL JOINJoinsEasyFree#10πWHERE vs HAVINGGroup byEasyFreeπUnlock 90 more SQL & Databases questions Β· all 1,200 questions for $39/year (about 11Β’ a day)See full access#11πRank ScoresWindow functionsMediumπ#12πRunning Total of RevenueWindow functionsMediumπ#13πNumber of Orders per Customer (Including Zero)JoinsEasyπ#14πDelete Duplicate RowsChanging dataMediumπ#15πUsers With 3+ Consecutive Login DaysWindow functionsHardπ#16πMonthly RevenueDates & stringsEasyπ#17πMonth-over-Month Growth (LAG)Window functionsMediumπ#18πProducts Never OrderedSubqueriesEasyπ#19πCustomers Who Bought From Every CategoryGroup byHardπ#20πEach Customer's Second OrderWindow functionsMediumπ#21π3-Order Moving AverageWindow functionsMediumπ#22πEmployees Hired in a Date RangeDates & stringsEasyπ#23πHandling NULLs with COALESCECASE & NULLEasyπ#24πSalary Bands with CASECASE & NULLEasyπ#25πPivot Order Statuses into ColumnsCASE & NULLMediumπ#26πTop Customer by Total SpendSorting & limitsEasyπ#27πEach Category's Share of RevenueWindow functionsMediumπ#28πEach Customer's First Order DateAggregatesEasyπ#29πNext-Day RetentionSelf joins & CTEsHardπ#30πDaily Active UsersAggregatesEasyπ#31πWho Referred the Most Customers?Self joins & CTEsEasyπ#32πOrg Chart with a Recursive CTESelf joins & CTEsHardπ#33πDepartments With No EmployeesJoinsEasyπ#34πEmployees Without a DepartmentFilteringEasyπ#35πUNION vs UNION ALLBasicsEasyπ#36π¬Subquery vs JOIN vs CTESubqueriesMediumπ#37πEmployees Paid Above Their Department AverageSubqueriesMediumπ#38π¬EXISTS vs INSubqueriesMediumπ#39π¬How do indexes work?Indexes & transactionsMediumπ#40π¬Primary keys and foreign keysSchema designEasyπ#41π¬Normalization (1NF, 2NF, 3NF)Schema designMediumπ#42π¬ACID transactionsIndexes & transactionsMediumπ#43π¬Transaction isolation levelsIndexes & transactionsHardπ#44π¬SQL injection and how to prevent itIndexes & transactionsMediumπ#45πCOUNT(*) vs COUNT(column) vs COUNT(DISTINCT)AggregatesEasyπ#46πDISTINCT vs GROUP BYGroup byEasyπ#47π¬SQL's logical order of executionBasicsMediumπ#48πPagination (LIMIT/OFFSET vs keyset)Sorting & limitsMediumπ#49πExtract email domainsDates & stringsEasyπ#50πDays Between a Customer's OrdersWindow functionsMediumπ#51πMedian SalaryWindow functionsHardπ#52πThe Most Popular ProductAggregatesEasyπ#53πSplit Employees into Salary QuartilesWindow functionsMediumπ#54πCancellation Rate per CustomerCASE & NULLMediumπ#55πAverage Order Value by CityAggregatesEasyπ#56πCustomers Who Ordered in Consecutive MonthsSelf joins & CTEsHardπ#57π¬DELETE vs TRUNCATE vs DROPChanging dataEasyπ#58πUpsert (insert or update)Changing dataMediumπ#59πUpdate Using Another TableChanging dataMediumπ#60πDesign a Table with ConstraintsSchema designMediumπ#61π¬What is a view?Schema designEasyπ#62π¬Stored procedures and triggersSchema designMediumπ#63π¬OLTP vs OLAPIndexes & transactionsMediumπ#64π¬SQL vs NoSQLSchema designMediumπ#65π¬When to denormalizeSchema designMediumπ#66π¬How would you speed up a slow query?Indexes & transactionsMediumπ#67π¬The N+1 query problemJoinsMediumπ#68π¬What are window functions?Window functionsMediumπ#69π¬CTE vs subquery vs temporary tableSelf joins & CTEsMediumπ#70πFind Gaps in a SequenceWindow functionsMediumπ#71πBest-Selling Product in Each CategoryWindow functionsMediumπ#72πCustomers Who Never CancelledGroup byEasyπ#73πCity With the Most CustomersGroup byEasyπ#74πOrders Above the Average AmountSubqueriesEasyπ#75πEach Customer's Most Recent OrderWindow functionsMediumπ#76πUnits Sold per Product (Including Zero)JoinsEasyπ#77πHires per YearDates & stringsEasyπ#78πSalary Gap to the Department's Top EarnerWindow functionsMediumπ#79πSame-Day Duplicate EventsGroup byEasyπ#80πRevenue by Category and MonthGroup byMediumπ#81π¬Optimistic vs pessimistic lockingIndexes & transactionsHardπ#82π¬Replication vs shardingIndexes & transactionsMediumπ#83π¬Soft deletesSchema designEasyπ#84π¬Schema migrations in productionSchema designMediumπ#85π¬Choosing column types (money, time, IDs)Schema designMediumπ#86πJSON columns in a relational databaseSchema designMediumπ#87πSearching text with LIKEFilteringEasyπ#88πFiltering with IN and BETWEENFilteringEasyπ#89πCase-Insensitive Duplicate EmailsDates & stringsMediumπ#90πCumulative Customer SignupsWindow functionsMediumπ#91πSubtotals with a Grand Total RowGroup byMediumπ#92πPairs of Customers in the Same CitySelf joins & CTEsMediumπ#93πEvery Combination with CROSS JOINJoinsEasyπ#94πINTERSECT and EXCEPTBasicsMediumπ#95πRow With the Max Value per Group (without window functions)JoinsMediumπ#96πOrders by Signup CohortGroup byHardπ#97πCopy Rows with INSERT β¦ SELECTChanging dataEasyπ#98πCombine Rows into a List (GROUP_CONCAT / STRING_AGG)Group byMediumπ#99πAggregate of AggregatesSubqueriesMediumπ#100π¬How to approach any SQL interview questionBasicsEasyπ