Intermediate 18 min readModule: Module 5: Aggregate Functions & GROUP BY / HAVING
Aggregate Functions (COUNT, SUM, AVG) & HAVING
Summarize data groups, calculate financial metrics, and filter aggregated groups with HAVING.
What You Will Learn in This Lesson
- Summarizing metrics with COUNT(*), SUM(col), AVG(col), MIN, MAX
- Grouping rows by dimensions with GROUP BY
- Why HAVING filters groups while WHERE filters individual rows
Introduction & Core Concept
Aggregate functions calculate a single summary result from multiple input row values. GROUP BY groups rows that have the same values into summary rows.
WHY DOES THIS MATTER IN THE REAL WORLD?
Aggregations drive executive dashboards, financial summaries, and business intelligence metrics.
Revenue by Region with HAVING Filter
sqlsql
12345678910SELECTregion,COUNT(order_id) AS total_orders,SUM(amount) AS total_revenue,ROUND(AVG(amount), 2) AS avg_order_valueFROM ordersWHERE status = 'COMPLETED'GROUP BY regionHAVING SUM(amount) > 10000ORDER BY total_revenue DESC;
Line-by-Line Technical Breakdown
1COUNT(*) counts all rows; COUNT(column) counts non-null values only.
Try It Yourself (Interactive Editor)
Modify the code in real-time and click Run to test live browser output and console logs.
Intelligent Code Runner & Live Sandbox[SQL]
SQL SOURCE EDITOR
Interactive Live CodeIndustry Best Practices & Professional Standards
- Every non-aggregated column in the SELECT list must appear in the GROUP BY clause.
Lesson Summary & Core Takeaways
- Aggregations and GROUP BY synthesize raw records into actionable insights.