Chapter 4 · Aggregations
SQL GROUP BY Practice
Aggregations turn many rows into one answer: total revenue, average order value, number of customers per state. GROUP BY splits the rows into groups and computes one result per group.
This is where most analyst questions live. Learn when to filter with WHERE (before grouping) versus HAVING (after grouping), and how COUNT(*) differs from COUNT(column).
Start practising74 exercises · 45 easy, 27 medium, 2 hard
Key ideas
- COUNT, SUM, AVG, MIN, MAX
- Summarise a column. COUNT(*) counts rows; COUNT(col) skips NULLs.
- GROUP BY
- One output row per group. Every selected column must be grouped or aggregated.
- HAVING
- Filter groups after aggregating, e.g. only states with more than 10 customers.
- ROUND
- Round averages and money for readable results: ROUND(AVG(x), 2).
Example: Orders and average order value by status
SELECT status,
COUNT(*) AS orders,
ROUND(AVG(total_amount), 2) AS avg_order
FROM orders
GROUP BY status
HAVING COUNT(*) > 50
ORDER BY orders DESC;GROUP BY makes one row per status; HAVING then drops statuses with 50 orders or fewer.
In interviews
"WHERE or HAVING?" comes up constantly. WHERE filters rows before grouping; HAVING filters the groups. Filtering early with WHERE is also faster.
All 74 Aggregations exercises
Ordered from easiest to hardest. Each one runs on a real database with instant feedback.
- 01Total Number of OrdersecommerceEasy
- 02Total Revenue by CategoryecommerceEasy
- 03Average Order ValueecommerceEasy
- 04Orders Per CustomerecommerceEasy
- 05Highest and Lowest Priced ProductsecommerceEasy
- 06Count Customers per StateecommerceEasy
- 07Average Order Value by StatusecommerceEasy
- 08Customers with Multiple OrdersecommerceEasy
- 09Payment Method DistributionecommerceEasy
- 10Monthly Order VolumeecommerceEasy
- 11Top 10 Spending CustomersecommerceEasy
- 12Count Delivered OrdersecommerceEasy
- 13Count Orders by Status CategoryecommerceEasy
- 14Total Discount Amount Per OrderecommerceEasy
- 15Total Number of AccountsfinanceEasy
- 16Total Balance by Account TypefinanceEasy
- 17Average Loan PrincipalfinanceEasy
- 18Transactions Per AccountfinanceEasy
- 19Minimum and Maximum Loan Interest RatesfinanceEasy
- 20Account Types with Many AccountsfinanceEasy
- 21Monthly Transaction VolumefinanceEasy
- 22Average Account Balance by StatusfinanceEasy
- 23Loan Portfolio Status SummaryfinanceEasy
- 24Total Deposits Per AccountfinanceEasy
- 25Count of Accounts Per Branch CityfinanceEasy
- 26Loan Count by StatusfinanceEasy
- 27Total Fees Charged Per AccountfinanceEasy
- 28Count Patients by StatehealthcareEasy
- 29Average Doctor Salary by DepartmenthealthcareEasy
- 30Visit Count by StatushealthcareEasy
- 31Total Billing Amount by StatushealthcareEasy
- 32Top 10 Most Prescribed MedicationshealthcareEasy
- 33Departments with More Than 4 DoctorshealthcareEasy
- 34Abnormal Lab Result PercentagehealthcareEasy
- 35Min and Max Billing AmountshealthcareEasy
- 36Count Patients per Blood TypehealthcareEasy
- 37Earliest and Latest Doctor Hire DateshealthcareEasy
- 38Average Billing Amount by StatushealthcareEasy
- 39Top 5 Most Prescribed MedicationshealthcareEasy
- 40Departments on Each FloorhealthcareEasy
- 41Male Patients Grouped by CityhealthcareEasy
- 42Doctor Count per SpecialtyhealthcareEasy
- 43Total Pending Billing AmounthealthcareEasy
- 44Patient Count by Birth DecadehealthcareEasy
- 45Visit Count by Day of WeekhealthcareEasy
- 46Categories with Many ProductsecommerceMedium
- 47Monthly Order CountecommerceMedium
- 48Average Rating Per ProductecommerceMedium
- 49Average Shipping Duration by CarrierecommerceMedium
- 50Revenue Impact of DiscountsecommerceMedium
- 51Order Count and Average by StatusecommerceMedium
- 52Top 5 Customers by Total SpendecommerceMedium
- 53Monthly Revenue SummaryecommerceMedium
- 54Product Statistics per CategoryecommerceMedium
- 55Payment Method BreakdownecommerceMedium
- 56Total Revenue Per Customer with NameecommerceMedium
- 57Product Rating Breakdown with CASEecommerceMedium
- 58Customer Count by Age GroupfinanceMedium
- 59Deposit Volume by Day of WeekfinanceMedium
- 60Average Loan Principal by Loan TypefinanceMedium
- 61Monthly Transaction Count and VolumefinanceMedium
- 62Branch with the Most Customer AccountsfinanceMedium
- 63Interest Collected per Loan TypefinanceMedium
- 64Monthly Deposit and Withdrawal TotalsfinanceMedium
- 65Customers Holding Multiple Account TypesfinanceMedium
- 66Visit Count by Month and YearhealthcareMedium
- 67Patients with More Than 3 VisitshealthcareMedium
- 68Patients Who Saw Multiple DoctorshealthcareMedium
- 69All Medications per Patient (Comma-Separated)healthcareMedium
- 70High-Volume High-Revenue DoctorshealthcareMedium
- 71Patients with Visits in Multiple MonthshealthcareMedium
- 72Abnormal Result Rate by Lab TesthealthcareMedium
- 73Portfolio Diversification ScorefinanceHard
- 74Patients with All Visit Status TypeshealthcareHard