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