Interview prep

SQL interview questions

85 interview-style SQL questions, from finding the second highest salary to login streaks and retention. Each one runs on a real database in your browser and checks your result against the expected output. No signup.

How to prepare for a SQL interview

Most SQL interviews reuse a small set of patterns: aggregate per group, join and keep or drop unmatched rows, rank within groups, compare a row to the one before it, and turn a messy question into steps. Practice each pattern until you can write it without looking it up.

Start with the easy questions in each topic, then time yourself on medium and hard ones. Say your approach out loud before you write the query; interviewers care about how you get there as much as the final answer.

  1. Easy Longest Item per Group Each Artist's Longest Song Free
  2. Medium Top 3 Salaries per Department Identify Top 3 Earners per Department Premium
  3. Medium Nth Purchase per Customer Find Customer's Third Purchase Premium
  4. Medium Longest Consecutive Login Streak Daily Active User Streaks Premium
  5. Medium Running Total Daily Running Total of Sales Premium
  6. Medium RANK vs DENSE_RANK Product Rankings with Tie Handling Premium
  7. Medium Compare to the Previous Row With LAG Compare Sales to Previous Day Premium
  8. Medium 90th Percentile Salary Calculate 90th Percentile Salary Free
  9. Medium 7-Day Moving Average 7-Day Moving Average of Sales Premium
  10. Medium Cumulative Sum Over Time Cumulative Student Enrollment Growth Premium
  11. Medium Select Every Other Row Select Every Other Row Premium
  12. Medium Best Seller per Category Best Seller in Each Category Premium
  13. Medium Salary vs Department Average Compare Employee Salary to Department Average Premium
  14. Medium Ranking With Ties Student GPA Ranking with Ties Premium
  15. Hard Year-over-Year Revenue Growth Year-Over-Year Revenue Growth Analysis Premium
  16. Hard Median Without a MEDIAN Function Department Salary Median (No MEDIAN Function) Premium
  17. Hard Longest Winning Streak Longest Winning Streak in Games Premium
  18. Hard User Session Duration Calculate User Session Duration Premium
  19. Hard Consecutive Increases (Gaps and Islands) Detect Consecutive Price Increases Premium
  1. Medium Employee and Manager Self Join Employee Reporting Structure Premium
  2. Medium Customers With No Orders Inactive Customers Report Premium
  3. Medium Join Four Tables Complete Order Detail Report Premium
  4. Hard Find Overlapping Date Ranges Detect Overlapping Room Bookings Premium
  1. Easy Departments With More Than 5 Employees Large Department Report Free
  2. Easy Find Duplicate Emails Find Duplicate Email Addresses Free
  3. Easy Average Order Value per Customer Average Order Value by Customer Free
  4. Easy Count Rows per Group Course Enrollment Numbers Free
  5. Easy Group Years by Decade Group Albums by Decade Free
  6. Easy Total Revenue per Customer Customer Lifetime Value Free
  7. Easy Extract the Email Domain Email Domain Analysis Free
  8. Easy Count Orders by Status Order Status Distribution Free
  9. Easy Earliest and Latest Date Hiring Date Range Free
  10. Easy List Unique Values per Category List Unique Product Categories Free
  11. Easy Conditional Count With SUM and CASE Conditional Counting with SUM+CASE Free
  12. Medium Pivot Rows Into Columns Pivot Sales by Month Premium
  13. Medium First and Last Order per Customer Customer Order Bookends Premium
  14. Medium Products Frequently Bought Together Frequently Bought Together Premium
  15. Medium Employees Hired on the Same Day Find Employees Hired on Same Day Premium
  16. Medium Counts and Totals Across Joined Tables Most Prolific Artists Premium
  17. Hard Unique Categories per Customer per Month Cross-Category Shopping Behavior Premium
  18. Hard Fuzzy Name Matching Fuzzy Product Name Matching Premium
  1. Easy Second Highest Salary Find the Runner-Up Salary Free
  2. Easy Rows Above the Average Students Exceeding Average GPA Free
  3. Medium EXISTS vs IN Find Customers With Recent Orders Free
  4. Hard Compound Annual Growth Rate (CAGR) Calculate CAGR (Compound Annual Growth Rate) Premium
  5. Hard Business Metrics in One Query Multi-Metric Executive Dashboard Premium
  1. Medium Find Missing IDs in a Sequence Detect Missing Order IDs Premium
  2. Medium Rows Above Their Group Average Above-Average Track Length per Album Premium
  3. Hard Monthly User Retention Rate Calculate Monthly Retention Rate Premium
  4. Hard Org Chart Hierarchy With a Recursive CTE Full Organizational Hierarchy Depth Premium
  5. Hard Cohort Retention Analysis Monthly Signup Cohort Retention Premium
  6. Hard Cumulative Distribution Cumulative Salary Distribution Premium
  7. Hard Fibonacci With a Recursive CTE Generate Fibonacci Sequence with SQL Premium
  1. Easy Employees Hired in the Last 90 Days New Hire Onboarding List Free
  2. Easy Combine Two Tables With UNION Merge Active and Inactive Users Free
  3. Easy Completion Rate as a Percentage Course Completion Percentage Free
  4. Easy Employees Without a Manager Employees Without Managers Free
  5. Easy Search by Name Pattern With LIKE Search Products by Name Pattern Free
  6. Easy Days Between Two Dates Calculate Account Age in Days Free
  7. Easy Convert Seconds to Minutes Convert Track Duration to Minutes Free
  8. Easy Salaries Over $100,000 Six-Figure Earners Free
  9. Easy Filter a Price Range With BETWEEN Mid-Range Product Filter Free
  10. Easy Replace NULL With COALESCE Handle Missing Data with COALESCE Free
  11. Easy Salary Bands With CASE WHEN Classify Employees by Salary Tier Free
  12. Hard Inventory Reorder Point Calculate Reorder Point for Inventory Premium
  1. Easy INSERT a New Row Stock a New Product Free
  2. Easy UPDATE Rows With a Condition Engineering Gets a Raise Free
  3. Easy UPDATE a Text Value Relabel the Genre Free
  4. Easy Create an Index Speed Up Customer Lookups Free
  5. Medium DELETE Rows With a Condition Academic Fresh Start Premium
  6. Medium Skip Duplicates With INSERT OR IGNORE Idempotent Customer Import Free
  7. Medium UPDATE With CASE Tiered Annual Bonus Premium
  8. Medium Composite Index on Two Columns The Two-Column Index Premium
  9. Medium Unique Index One Account per Email Premium
  10. Medium Copy Rows With INSERT ... SELECT Archive the Churned User Free
  11. Medium Reassign Rows With UPDATE The Manager Moved On Premium
  12. Hard UPDATE From Another Table Rebuild the Order Totals Premium
  13. Hard DELETE Rows With No Match Remove the Ghost Students Premium
  14. Hard Partial Index Index Only What You Query Premium
  15. Hard Upsert With INSERT ... ON CONFLICT Upsert the Stock Count Premium
  16. Hard DELETE Using a Subquery Drop the Legacy Catalog Premium
  1. Medium Transfer in a Transaction The Atomic Stock Transfer Free
  2. Medium Two Changes in One Transaction Cancel the Order, Cleanly Premium
  3. Medium INSERT and UPDATE in One Transaction Enroll and Count, Together Premium
  4. Hard SAVEPOINT and ROLLBACK TO The Reversible Experiment Premium