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
- Easy Second Highest Salary Find the Runner-Up Salary Free
- 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 Rows Above the Average Students Exceeding Average GPA 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 questions
- Medium Longest Consecutive Login Streak Daily Active User Streaks Premium
- Medium Running Total Daily Running Total of Sales Premium
- Medium Find Missing IDs in a Sequence Detect Missing Order IDs Premium
- Medium Pivot Rows Into Columns Pivot Sales by Month Premium
- Medium 90th Percentile Salary Calculate 90th Percentile Salary Free
- Medium First and Last Order per Customer Customer Order Bookends Premium
- Medium 7-Day Moving Average 7-Day Moving Average of Sales Premium
- Medium Products Frequently Bought Together Frequently Bought Together Premium
- Medium Cumulative Sum Over Time Cumulative Student Enrollment Growth Premium
- Medium Employees Hired on the Same Day Find Employees Hired on Same Day Premium
- Medium Rows Above Their Group Average Above-Average Track Length per Album 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 Counts and Totals Across Joined Tables Most Prolific Artists Premium
Hard questions
- Hard Monthly User Retention Rate Calculate Monthly Retention Rate Premium
- Hard Median Without a MEDIAN Function Department Salary Median (No MEDIAN Function) Premium
- Hard Unique Categories per Customer per Month Cross-Category Shopping Behavior Premium
- Hard Longest Winning Streak Longest Winning Streak in Games Premium
- Hard Compound Annual Growth Rate (CAGR) Calculate CAGR (Compound Annual Growth Rate) Premium
- Hard User Session Duration Calculate User Session Duration Premium
- Hard Fuzzy Name Matching Fuzzy Product Name Matching Premium
- Hard Cohort Retention Analysis Monthly Signup Cohort Retention Premium
- Hard Cumulative Distribution Cumulative Salary Distribution Premium
- Hard Consecutive Increases (Gaps and Islands) Detect Consecutive Price Increases Premium
- Hard Business Metrics in One Query Multi-Metric Executive Dashboard Premium