Chapter 8: Aggregate Functions & Grouping

This is a super important chapter because now we move from just looking at individual rows to analyzing the big picture! Aggregate functions help us answer questions like:

  • How many students do we have?
  • What is the average age?
  • Who is the youngest/oldest student?
  • How many students per gender?

And GROUP BY + HAVING let us group data and filter groups — exactly like creating Excel pivot tables!

Let’s first prepare good sample data with more variety so we can see aggregates properly. Run this in your my_coaching database (copy-paste all at once):

SQL

Now we have 10 students — 5 female, 5 male, different join dates, some inactive, etc.

1. Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), MAX()

These functions summarize many rows into one single value.

Function What it does Example Use Case
COUNT() Counts number of rows (or non-NULL values) Total students, how many have phone
SUM() Adds up all values in a column Total fees (if we had a fees column)
AVG() Average (mean) of values Average age of students
MIN() Smallest value Youngest student’s birth date
MAX() Largest value Oldest student’s birth date

Important:

  • Aggregates ignore NULL values (except COUNT(*))
  • You can use them with WHERE to filter first

Examples – Run these one by one!

A. Total number of students

SQL

B. Number of students who have provided phone number

SQL

C. Number of active students

SQL

D. Earliest and latest join date

SQL

E. Youngest and oldest student (by birth date)

SQL

F. Approximate average age (2026 – birth year)

SQL

2. GROUP BY – Grouping Rows & Aggregating Per Group

GROUP BY lets us split data into groups and apply aggregates per group.

Syntax:

SQL

Examples – Real reports!

A. Number of students per gender

SQL

Result:

text

B. Number of students who joined per month

SQL

C. Number of active vs inactive students

SQL

D. Average birth year per gender

SQL

3. HAVING Clause – Filtering Groups (vs WHERE)

Clause What it filters When to use
WHERE Filters individual rows before grouping Filter before aggregation
HAVING Filters groups after grouping Filter based on aggregate results (COUNT, AVG, etc.)

Important rule: WHERE comes before GROUP BY HAVING comes after GROUP BY

Example – Wrong vs Right

Wrong (won’t work):

SQL

Correct:

SQL

Real example – Genders with at least 5 students

SQL

Another example – Join months with more than 2 students

SQL

4. ROLLUP – Adding Subtotal & Grand Total Rows

ROLLUP adds summary rows automatically — super useful for reports!

Syntax:

SQL

Example – Students per gender with totals

SQL

Result (beautiful!):

text

Multiple levels (e.g., per gender and per active status):

SQL

You’ll see:

  • Rows for each gender + active combination
  • Subtotal per gender
  • Grand total at the bottom

That’s it for Chapter 8! 🎉 You can now create professional-looking reports with counts, averages, groups, and subtotals!

Homework for today (do it right now – very important!) Run these 5 queries and paste the output of each here:

SQL

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *