Aggregate Functions
Learn how SQLite aggregate functions turn many rows into useful summary values like counts, totals, averages, minimums, and maximums.
So far, many of your queries have shown one result row for each row in a table. That is useful when you want details.
But many everyday questions are not detail questions. They are summary questions:
- How many orders did we receive?
- What is the total amount sold?
- What is the average rating?
- What was the smallest or largest value?
Aggregate functions answer questions like these by reading many rows and returning one summary value.
From many rows to one answer
Think of an aggregate as a tiny summarizing machine. Many rows go in. One value comes out.
The five core aggregate functions are:
| Function | Question it answers | Example |
|---|---|---|
COUNT(*) | How many rows are there? | number of orders |
SUM(column) | What is the total? | total dollars sold |
AVG(column) | What is the average? | average score |
MIN(column) | What is the smallest value? | lowest price |
MAX(column) | What is the largest value? | highest price |
Counting rows with COUNT(*)
COUNT(*) counts rows. It does not care what values are inside those rows. It simply answers: "How many rows are in this result?"
The table has four order rows, so the answer is one row containing the number 4.
Adding values with SUM
SUM(column) adds the non-NULL values in a column. This is how you answer total questions.
SQLite reads the amount values and adds them into one total.
Finding an average with AVG
AVG(column) finds the average of the non-NULL values in a column. You can use ROUND when you want a friendlier number.
The 2 in ROUND(..., 2) means "show two digits after the decimal point."
What does an aggregate function do?
It shows every row exactly as it appears in the table.
It combines many rows into a smaller summary, often one value.
It changes the table's column names permanently.
It always sorts rows alphabetically.
Finding the smallest and largest values
MIN(column) returns the smallest value. MAX(column) returns the largest value.
These functions are useful for "best," "worst," "earliest," "latest," "cheapest," and "most expensive" questions.
Building a one-row report
You can use several aggregates in the same query. Each aggregate looks at the same set of rows and produces its own summary value.
One query gives you a compact report: count, total, average, smallest, and largest.
Aggregates respect WHERE
WHERE filters rows first. Then the aggregate summarizes only the rows that survived the filter.
Read this as: "Keep only Ada's rows, then add their amounts."
NULL values are skipped by most aggregates
NULL means a value is missing or unknown. Most aggregate functions skip NULL values:
SUM(column)skipsNULLAVG(column)skipsNULLMIN(column)skipsNULLMAX(column)skipsNULLCOUNT(column)skipsNULL
The big exception is COUNT(*), which counts rows whether they contain NULL or not.
There are four customers, but only two have phone numbers. COUNT(*) returns 4; COUNT(phone) returns 2.
NULL and averages
Skipping NULL usually gives a better answer than treating unknown values as zero.
The average is based on the three known ratings: 5, 4, and 3. The missing rating is not counted as zero.
A helpful habit
Use COUNT(*) when you mean "How many rows?" Use COUNT(column) when you mean "How many rows have a value in this column?"
Check your understanding
Which function answers "How many rows are in this result?"
COUNT(*)
SUM(*)
AVG(*)
MAX(*)
A table has 5 rows. The email column is NULL in 2 rows. What will COUNT(email) return?
5
3
2
0
Why does AVG(rating) skip NULL ratings?
Because SQLite cannot store missing values.
Because NULL means unknown, and unknown values should not be averaged as if they were real numbers.
Because AVG only works on text.
Because NULL is always the same as zero.
What does this query return: SELECT MIN(price), MAX(price) FROM products;?
One row for every product.
One row containing the smallest price and the largest price.
A sorted list of all prices.
Only products with missing prices.