QUICK START:HTMLCSSJavaScriptTypeScriptPythonSQLReactNext.jsNode.jsDSASystem DesignDevOpsCybersecurityAI / ML
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

sql
sql
1
2
3
4
5
6
7
8
9
10
SELECT
region,
COUNT(order_id) AS total_orders,
SUM(amount) AS total_revenue,
ROUND(AVG(amount), 2) AS avg_order_value
FROM orders
WHERE status = 'COMPLETED'
GROUP BY region
HAVING SUM(amount) > 10000
ORDER 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 Code

Industry 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.