Grouped Aggregate Functions — Complete Guide
Grouped Aggregate Functions — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of MySQL Tutorial on Toolliyo Academy.
On this page
MySQL Tutorial · Lesson 31 of 100
Grouped Aggregate Functions
Basics ✓ → Advanced
Advanced · 2 — Production · ~6 min · MySQL — Functions & Window Functions
What is this?
Grouped aggregates combine GROUP BY with functions like SUM, AVG, COUNT per bucket. Filters on groups use HAVING; filters on raw rows use WHERE.
Why should you care?
Restaurant dashboard on Swiggy shows average delivery time per city — GROUP BY city, AVG(minutes).
See it live — copy this example
Run in MySQL Workbench or the mysql CLI.
SELECT c.city,
COUNT(o.order_id) AS orders,
SUM(o.total_inr) AS city_revenue
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
GROUP BY c.city
HAVING city_revenue >= 1000
ORDER BY city_revenue DESC;
What happened?
- Join attaches orders to customer cities.
- Aggregates compute per city.
- HAVING drops cities under ₹1000 total revenue.
Practice next
- Seed multi-city customers and orders.
- Run query; verify math on one city manually.
- Move city filter to WHERE vs HAVING — note difference.
- Add AVG(o.total_inr) per city.
- GROUP BY city, DATE(o.placed_at) if date column exists.
Remember
GROUP BY + aggregates = per-bucket metrics. HAVING filters summarized rows. Mind JOIN grain to avoid inflated SUM.
DataFlow regional sales
Leadership sees city_revenue ranked for warehouse placement.
Outcome: Inventory moves to high-GMV cities.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!