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.

25 min 3 practice tasks
SQL Aggregate Functions | COUNT, SUM, AVG, MAX, MIN | #SQL Course 21 by Data with Baraa on YouTube

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 where age_group is 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, MIN and MAX behave 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.

1

Completed orders at a glance

Finance 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?
  1. Filter to completed orders with WHERE first; the aggregates then only see those rows.
  2. ROUND(SUM(amount), 2) rounds the total to 2 decimals.
SQL · SQLite
SELECT COUNT(*)
FROM orders;
Tables in this database
advertisers (12 rows)
id, name, industry, country
campaigns (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
daily_stats (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
customers (300 rows)
id, first_name, last_name, email, country, segment, age_group, signup_date
orders (900 rows)
id, customer_id, campaign_id, order_date, amount, status
ab_test (4,000 rows)
user_id, variant, device, time_on_page_sec, converted, revenue
2

Profile the customer table

Return 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?
  1. COUNT(column) skips NULLs; COUNT(*) counts every row.
  2. COUNT(DISTINCT column) counts unique values.
SQL · SQLite
SELECT COUNT(*), COUNT(*), COUNT(country)
FROM customers;
Tables in this database
advertisers (12 rows)
id, name, industry, country
campaigns (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
daily_stats (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
customers (300 rows)
id, first_name, last_name, email, country, segment, age_group, signup_date
orders (900 rows)
id, customer_id, campaign_id, order_date, amount, status
ab_test (4,000 rows)
user_id, variant, device, time_on_page_sec, converted, revenue
3

March click-through rate

For 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?
  1. Both sums are integers, and SQLite divides integers as integers (the fraction is thrown away).
  2. Multiply by 100.0 (or 1.0) before dividing so the math happens in decimals, then wrap it in ROUND(..., 2).
SQL · SQLite
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)
id, name, industry, country
campaigns (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
daily_stats (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
customers (300 rows)
id, first_name, last_name, email, country, segment, age_group, signup_date
orders (900 rows)
id, customer_id, campaign_id, order_date, amount, status
ab_test (4,000 rows)
user_id, variant, device, time_on_page_sec, converted, revenue