← All topics

πŸ—„ 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.

100 questions, most asked first

Ordered so the most useful questions for learning each pattern come first.

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