Rana Maninder Singh
Lesson 3 of 11SQL & DBMS (MySQL)

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

ResourceLink
PreviousSQL commands
NextJoins


What you will learn

COUNT, SUM, AVG, MIN, MAX, GROUP BY, HAVING.

Excel vs Python vs SQL — same task, three tools
Excel vs Python vs SQL — same task, three tools

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:

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:

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:

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:

Use this when: You're building the same numbers you'd get from an Excel pivot on month.


Essential commands

Command / actionWhat it does
COUNT(*) / COUNT(col)Count rows or non-null values.
SUM / AVG / MIN / MAXAggregate numeric columns.
GROUP BY colOne result row per group.
HAVING conditionFilter 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:

  1. Segment summary with GROUP BY (worked model below).
  2. Monthly revenue rollup on sales.
  3. One query using HAVING to drop small groups.
  4. 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):

  1. Source: ___

Pattern: ___

Result: ___

  1. Source: ___

Pattern: ___

Result: ___

Checklist

  • [ ] GROUP BY correct grain
  • [ ] HAVING used once
  • [ ] Results match Excel pivot