SQLite Aggregate Functions: COUNT, SUM, AVG, MIN, MAX
SQLite aggregate functions summarize multiple input rows into a single result. Use them to count records, calculate totals and averages, find extreme values, or combine text. Add GROUP BY when you need one summary per category.
Choose an aggregate function
| Task | Function | Important behavior |
|---|---|---|
| Count all rows | count(*) |
Includes rows whose columns are NULL |
| Count non-NULL values | count(column) |
Ignores NULL in that column |
| Sum numeric values | sum() |
Returns NULL if there are no non-NULL inputs |
| Sum with a zero default | total() |
Returns 0.0 for no non-NULL inputs |
| Calculate a mean | avg() |
Ignores NULL inputs |
| Find lowest / highest | min() / max() |
Ignore NULL inputs |
| Combine text values | group_concat() |
Skips NULL values; ordering is not guaranteed without an aggregate ORDER BY |
Example: summarize sales by team
Run this self-contained query in the SQLite Playground:
WITH sales(team, amount) AS (
VALUES ('A', 10), ('A', 20), ('B', 5), ('B', NULL)
)
SELECT
team,
count(*) AS row_count,
count(amount) AS priced_count,
sum(amount) AS total_amount,
avg(amount) AS average_amount
FROM sales
GROUP BY team
ORDER BY team;
| team | row_count | priced_count | total_amount | average_amount |
|---|---|---|---|---|
| A | 2 | 2 | 30 | 15.0 |
| B | 2 | 1 | 5 | 5.0 |
Team B has two rows but only one non-NULL amount. That is why count(*) differs from count(amount), and why its average is 5.0, not 2.5.
For filtered aggregates, distinct counts and empty-result behavior, see the SQLite COUNT guide. To calculate totals without collapsing rows, use SQLite window functions.
Reference: SQLite built-in aggregate functions.
-
avg
Calculate averages with SQLite avg(), understand NULL values, empty groups, numeric conversion, and grouped averages with runnable examples. -
count
Learn COUNT(*), COUNT(column), COUNT(DISTINCT), and FILTER in SQLite, including how NULL values and empty result sets affect counts. -
group_concat
Learn to combine rows with SQLite group_concat(), choose a separator, control ordering, skip NULL values, and remove duplicates with runnable examples. -
max
The SQLitemax()function calculates the maximum value of all specified values in a group and returns it. -
min
The SQLitemin()function computes the minimum value of all specified values in a group and returns it. -
sum
The SQLitesum()function computes the sum of all specified values in a group and returns it.