Aggregate Functions, GROUP BY, HAVING, and Reporting Queries
Build useful summaries and reports using MySQL aggregation and grouped analysis.
Inside this chapter
- Common Aggregate Functions
- GROUP BY
- HAVING
- Reporting Mindset
- Business Example
Series navigation
Study the chapters in order for the clearest path from MySQL basics to advanced performance, consistency, and production operations. Use the navigation at the bottom to move smoothly through the full tutorial series.
Common Aggregate Functions
COUNT()SUM()AVG()MIN()MAX()
These functions help summarize records instead of returning every row individually.
GROUP BY
SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id;
GROUP BY lets MySQL summarize data by categories such as department, region, status, product, or month.
HAVING
SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 10;
HAVING filters grouped results after aggregation. This is very useful in reporting queries.
Reporting Mindset
Aggregate queries are crucial for dashboards, financial summaries, operational metrics, leaderboards, and trend reporting. Many business decisions depend on getting these summaries right.
Business Example
A sales dashboard may group orders by month and sum total revenue, count distinct customers, or compare average order value by region. Aggregation powers that entire view.