Aggregate Functions
Lesson opener
In one line: COUNT, SUM, AVG, MIN, MAX, GROUP BY, HAVING.
Why it matters: Scrolling every customer row to mentally average revenue doesn't work - you need one row of totals.
Before this lesson: SQL commands
Before you start
| Resource | Link |
|---|---|
| Previous | SQL commands |
| Next | Joins |
What you will learn
COUNT, SUM, AVG, MIN, MAX, GROUP BY, HAVING.

Aggregates without GROUP BY
[Whole-table summary stats]
WHY: Scrolling every customer row to mentally average revenue doesn't work — you need one row of totals.
WHEN: The exec deck needs total customers, average revenue, and sum of revenue across the whole base.
SELECT
COUNT(*) AS customers,
AVG(total_revenue) AS avg_rev,
SUM(total_revenue) AS total_rev
FROM customers;Breaking it down:
COUNT(*)→ how many rows (customers) in the tableAVG(total_revenue)→ mean revenue per customerSUM(total_revenue)→ add up all customer revenue into one number- No
GROUP BY→ entire table collapses to a single summary row
Use this when: You need headline KPIs before drilling into segments.
GROUP BY
[Summary per segment]
WHY: A single table-wide average hides that Platinum spends 3× Bronze — you need one row per group.
WHEN: Marketing wants count and average revenue broken out by segment for a slide.
SELECT segment,
COUNT(*) AS n,
AVG(total_revenue) AS avg_rev
FROM customers
GROUP BY segment
ORDER BY avg_rev DESC;Breaking it down:
GROUP BY segment→ one result row per distinct segment valueCOUNT(*) AS n→ customers in that segmentAVG(total_revenue)→ average revenue within that segment onlyORDER BY avg_rev DESC→ richest segments first
Use this when: Your report grain is "per category" — segment, region, product line, etc.
HAVING (filter after aggregate)
[Drop small segments]
WHY: WHERE can't filter on COUNT(*) — that number doesn't exist until after grouping, so you need HAVING.
WHEN: You only want segments with at least 20 customers so tiny groups don't skew the chart.
SELECT segment, COUNT(*) AS n
FROM customers
GROUP BY segment
HAVING COUNT(*) >= 20;Breaking it down:
GROUP BY segment→ aggregate per segmentHAVING COUNT(*) >= 20→ keep only groups with 20+ rows — filters groups, not individual rows
Use this when: Your rule is about group size or group totals, not a single row's value.
On sales table
[Monthly revenue trend]
WHY: Order-level rows don't show month-over-month totals — you need to roll up revenue by month.
WHEN: Ops reviews a simple revenue-by-month table for the last two years.
SELECT month, SUM(revenue) AS revenue
FROM sales
GROUP BY month
ORDER BY month;Breaking it down:
GROUP BY month→ one row per month string (YYYY-MM)SUM(revenue)→ total revenue for all orders in that monthORDER BY month→ chronological order for a trend line
Use this when: You're building the same numbers you'd get from an Excel pivot on month.
Essential commands
| Command / action | What it does |
|---|---|
COUNT(*) / COUNT(col) | Count rows or non-null values. |
SUM / AVG / MIN / MAX | Aggregate numeric columns. |
GROUP BY col | One result row per group. |
HAVING condition | Filter groups after aggregation. |
Practice
Before each query, name the three SQL beats — Source → Pattern → Result (SQL translation of Input → Logic → Output — see sql2).
Do this now:
- Segment summary with
GROUP BY(worked model below). - Monthly revenue rollup on
sales. - One query using
HAVINGto drop small groups. - Save
03_aggregates.sql.
Example framing for task 1:
-- Source: customers (segment, total_revenue)
-- Pattern: GROUP BY + aggregates (COUNT, AVG)
-- Result: one row per segment with headcount and average revenue
SELECT segment,
COUNT(*) AS n,
AVG(total_revenue) AS avg_rev
FROM customers
GROUP BY segment
ORDER BY avg_rev DESC;Framing prompts for the rest (fill these blanks in your .sql comments before writing each query):
- Source: ___
Pattern: ___
Result: ___
- Source: ___
Pattern: ___
Result: ___
Checklist
- [ ] GROUP BY correct grain
- [ ] HAVING used once
- [ ] Results match Excel pivot