Section 2 · Lesson 1
Aggregate functions: COUNT, SUM, AVG, MIN, MAX
Turn thousands of rows into a few headline numbers, round them for reporting and avoid the integer-division trap.
So far every query returned one output row per table row. Managers rarely want that. They want one number: total spend last quarter, the average order value, how many customers we have. Aggregate functions collapse many rows into a single summary value.
The big five
| Function | Returns | Example on our data |
|---|---|---|
COUNT(*) |
number of rows | how many orders were placed |
SUM(col) |
total | total ad spend |
AVG(col) |
mean | average order amount |
MIN(col) |
smallest value | first signup date |
MAX(col) |
largest value | biggest single order |
SELECT COUNT(*) AS orders,
SUM(amount) AS total_amount,
MIN(amount) AS smallest,
MAX(amount) AS largest
FROM orders;
MIN and MAX also work on text and dates. Because our dates are YYYY-MM-DD text,
MIN(signup_date) returns the earliest signup.
Aggregates plus WHERE
WHERE runs first and the aggregate only sees the rows that survive. This is how you answer
“total spend in February” or “average value of refunded orders”:
SELECT SUM(spend) AS feb_spend
FROM daily_stats
WHERE date BETWEEN '2026-02-01' AND '2026-02-28';
Three flavors of COUNT
COUNT(*)counts rows, no matter what they contain.COUNT(age_group)counts rows whereage_groupis not NULL.COUNT(DISTINCT country)counts unique non-NULL values.
The difference is a quick data-quality check: if COUNT(*) and COUNT(column) disagree, the
column has missing values. SUM, AVG, MIN and MAX also ignore NULLs, so AVG is the
average of the known values only.
ROUND for readable numbers
Try SELECT SUM(amount) FROM orders WHERE status = 'cancelled'; and you’ll get
3879.2200000000003, because computers store decimals approximately. Use
ROUND(value, decimals) for anything a person will read:
SELECT ROUND(AVG(amount), 2) AS avg_order_value
FROM orders;
Round at the very end of a calculation, not in the middle, or small errors add up.
The integer-division trap
This is the bug that catches almost every analyst once. In SQLite (and PostgreSQL and SQL Server), dividing one integer by another throws away the fractional part:
SELECT 7 / 2; -- 3, not 3.5
SELECT 7 * 1.0 / 2; -- 3.5
SELECT CAST(7 AS REAL) / 2; -- 3.5
clicks and impressions are integers, so SUM(clicks) / SUM(impressions) returns 0 for
any realistic ad data. Multiply by 1.0 (or by 100.0 if you want a percentage) before
dividing. MySQL behaves differently and returns decimals, which is why tutorials written for
MySQL don’t mention this.
Don’t mix summary and detail
SELECT name, MAX(daily_budget) FROM campaigns;
MAX produces one row, but name has 40 values, so which name belongs in that row? SQLite
runs it anyway (it has a special rule that borrows name from the row holding the maximum),
but PostgreSQL, SQL Server and most other databases reject the query with an error. Don’t build
habits on that leniency. If you want a value per something, you need GROUP BY, which is the
next lesson.
About the video: it’s recorded in SQL Server, but
COUNT,SUM,AVG,MINandMAXbehave the same way here.
Practice
Write your answer, press Run to see results and Check answer to grade it. Shortcuts: Ctrl/⌘ + Enter run, Shift + Enter check.
Completed orders at a glance
DoneFinance wants one row summarizing completed orders (status = 'completed'), with three columns in this order: 1. the number of completed orders, 2. their total amount, rounded to 2 decimals, 3. their average amount (average order value), rounded to 2 decimals.
Need a hint?
- Filter to completed orders with
WHEREfirst; the aggregates then only see those rows. ROUND(SUM(amount), 2)rounds the total to 2 decimals.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
SELECT COUNT(*)
FROM orders;
Tables in this database
advertisers (12 rows)campaigns (40 rows)daily_stats (1,561 rows)customers (300 rows)orders (900 rows)ab_test (4,000 rows)Profile the customer table
DoneReturn one row describing customers, with three columns in this order: 1. the total number of customers, 2. the number of customers whose age_group is known (not NULL), 3. the number of different countries customers come from.
Need a hint?
COUNT(column)skips NULLs;COUNT(*)counts every row.COUNT(DISTINCT column)counts unique values.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
SELECT COUNT(*), COUNT(*), COUNT(country)
FROM customers;
Tables in this database
advertisers (12 rows)campaigns (40 rows)daily_stats (1,561 rows)customers (300 rows)orders (900 rows)ab_test (4,000 rows)March click-through rate
DoneFor March 2026 (date from 2026-03-01 through 2026-03-31), return one row from daily_stats with three columns in this order: total impressions, total clicks, and the click-through rate as a percentage (total clicks / total impressions × 100), rounded to 2 decimals. The starter returns 0 for the rate; find out why and fix it.
Need a hint?
- Both sums are integers, and SQLite divides integers as integers (the fraction is thrown away).
- Multiply by
100.0(or1.0) before dividing so the math happens in decimals, then wrap it inROUND(..., 2).
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
SELECT SUM(impressions),
SUM(clicks),
SUM(clicks) / SUM(impressions) * 100
FROM daily_stats
WHERE date BETWEEN '2026-03-01' AND '2026-03-31';
Tables in this database
advertisers (12 rows)campaigns (40 rows)daily_stats (1,561 rows)customers (300 rows)orders (900 rows)ab_test (4,000 rows)