๐ 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
Ranked by how often each question comes up in real interviews. #1 is the most common.
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๐