SQL GROUP BY Interview Questions

Aggregation questions test whether you can turn rows into a summary: totals, counts and averages per group. They are the most common warm-up questions in data analyst interviews.

Watch the difference between WHERE and HAVING. WHERE filters rows before grouping; HAVING filters the groups after.

Patterns to know

  • Counting and summing per group with GROUP BY
  • Filtering groups with HAVING, for example finding duplicates
  • Conditional aggregation with SUM(CASE WHEN ...) to pivot rows into columns
  • Percentages and averages with correct decimal math
  • Grouping by a piece of a date, such as month or year

Easy questions

  1. Easy Second Highest Salary Find the Runner-Up Salary Free
  2. Easy Departments With More Than 5 Employees Large Department Report Free
  3. Easy Find Duplicate Emails Find Duplicate Email Addresses Free
  4. Easy Average Order Value per Customer Average Order Value by Customer Free
  5. Easy Count Rows per Group Course Enrollment Numbers Free
  6. Easy Group Years by Decade Group Albums by Decade Free
  7. Easy Rows Above the Average Students Exceeding Average GPA Free
  8. Easy Total Revenue per Customer Customer Lifetime Value Free
  9. Easy Extract the Email Domain Email Domain Analysis Free
  10. Easy Count Orders by Status Order Status Distribution Free
  11. Easy Earliest and Latest Date Hiring Date Range Free
  12. Easy List Unique Values per Category List Unique Product Categories Free
  13. Easy Conditional Count With SUM and CASE Conditional Counting with SUM+CASE Free

Medium questions

  1. Medium Longest Consecutive Login Streak Daily Active User Streaks Premium
  2. Medium Running Total Daily Running Total of Sales Premium
  3. Medium Find Missing IDs in a Sequence Detect Missing Order IDs Premium
  4. Medium Pivot Rows Into Columns Pivot Sales by Month Premium
  5. Medium 90th Percentile Salary Calculate 90th Percentile Salary Free
  6. Medium First and Last Order per Customer Customer Order Bookends Premium
  7. Medium 7-Day Moving Average 7-Day Moving Average of Sales Premium
  8. Medium Products Frequently Bought Together Frequently Bought Together Premium
  9. Medium Cumulative Sum Over Time Cumulative Student Enrollment Growth Premium
  10. Medium Employees Hired on the Same Day Find Employees Hired on Same Day Premium
  11. Medium Rows Above Their Group Average Above-Average Track Length per Album 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 Counts and Totals Across Joined Tables Most Prolific Artists Premium

Hard questions

  1. Hard Monthly User Retention Rate Calculate Monthly Retention Rate Premium
  2. Hard Median Without a MEDIAN Function Department Salary Median (No MEDIAN Function) Premium
  3. Hard Unique Categories per Customer per Month Cross-Category Shopping Behavior Premium
  4. Hard Longest Winning Streak Longest Winning Streak in Games Premium
  5. Hard Compound Annual Growth Rate (CAGR) Calculate CAGR (Compound Annual Growth Rate) Premium
  6. Hard User Session Duration Calculate User Session Duration Premium
  7. Hard Fuzzy Name Matching Fuzzy Product Name Matching Premium
  8. Hard Cohort Retention Analysis Monthly Signup Cohort Retention Premium
  9. Hard Cumulative Distribution Cumulative Salary Distribution Premium
  10. Hard Consecutive Increases (Gaps and Islands) Detect Consecutive Price Increases Premium
  11. Hard Business Metrics in One Query Multi-Metric Executive Dashboard Premium

More SQL interview topics