# Aggregate Functions

> Turn many rows into one answer with count, sum, avg, min and max, and learn exactly how each one treats NULL and empty results.

Subject: SQL | Lesson 19 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/sql/aggregate-functions
Previous: [Join Pitfalls: Duplicates & Fan-Out](https://codewithanimation.in/sql/join-pitfalls.md) | Next: [GROUP BY](https://codewithanimation.in/sql/group-by.md)

## Many rows in, one value out

An **aggregate function** reads a set of rows and returns a single value. The five you will use most are `count`, `sum`, `avg` (average), `min` and `max`.

A query that only has aggregates in its `SELECT` list returns exactly one row: `SELECT count(*), max(points) FROM players;`.

## Questions about the whole table

"How many players are there?", "What is the best score?" and "What is the average?" are questions about a whole set of rows. Asking the database is faster and safer than downloading every row and adding them up in your own code.

## NULL changes the numbers

```sql
SELECT count(*), count(points), sum(points), avg(points)
FROM players;
```

The `players` table has 8 rows, but Chen and Eli have `NULL` points. The result is 8, 6, 153 and 25.5.

`count(*)` counts rows, whatever is inside them. `count(points)` counts only the rows where `points` is not NULL. `sum`, `avg`, `min` and `max` also skip NULL. So `avg` is 153 divided by 6 values, not by 8 rows.

`count(DISTINCT team)` counts each different team once, so it gives 3. Adding `FILTER (WHERE ...)` after an aggregate limits only that one aggregate, for example `count(*) FILTER (WHERE points >= 24)`.

## Common mistake: Empty input and the WHERE trap

When no rows are selected, `count` returns 0, but `sum`, `avg`, `min` and `max` return `NULL`. The sum of nothing is NULL, not 0. If you want 0, wrap it: `coalesce(sum(points), 0)`.

You cannot write an aggregate in `WHERE`. `WHERE` decides which rows go into the calculation, so it must run before any aggregate exists. To filter on an aggregate you need `HAVING`, which comes two lessons later.

`avg` of whole numbers is not rounded to a whole number. PostgreSQL returns an exact decimal (numeric), so use `round(avg(points), 1)` for a tidy result.

## Tip: Pick the count that matches the question

Use `count(*)` for "how many rows", and `count(column)` for "how many rows have a value here". When a number looks too small or too large, check how many NULLs the column holds.

## Try it: See which rows each function uses

Pick a function and read the last column: it shows which rows are used and which are skipped. Then switch to the WHERE points > 100 rows and see what each function returns for nothing.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/aggregate-functions

## Check yourself

### Question 1

`players` has 8 rows and 2 of them have NULL `points`. What does `count(points)` return?

- A. 6
- B. 8
- C. 2

Correct answer: A

count(column) counts only non-NULL values, so the two NULL rows are skipped and 6 remain.

- Why not B: 8 is count(*), which counts rows. count(points) skips the rows where points is NULL.
- Why not C: 2 is the number of NULL rows. count(points) counts the others.

### Question 2

A column holds the values 10, 20 and NULL. What does `avg(column)` return?

- A. 15
- B. 10
- C. NULL

Correct answer: A

avg adds the non-NULL values (30) and divides by how many there are (2), so the answer is 15.

- Why not B: 10 would be the answer if NULL were counted as 0 (30 divided by 3). avg skips NULL instead.
- Why not C: avg returns NULL only when there are no non-NULL values at all.

### Question 3

A query selects `count(*)` and `sum(points)` but no row passes the WHERE test. What does it return?

- A. 0 and NULL
- B. 0 and 0
- C. No rows at all

Correct answer: A

With no rows, count gives 0 and the other aggregates give NULL. Use coalesce(sum(points), 0) if you need 0.

- Why not B: The sum of no values is NULL, not 0. Only count returns 0 for no rows.
- Why not C: A query with only aggregates always returns exactly one row, even when the input is empty.

## Remember this

- An aggregate function turns a set of rows into one value: count, sum, avg, min, max.
- count(*) counts rows. count(column), sum, avg, min and max skip NULL values.
- On no rows, count gives 0 and the others give NULL, not 0.
- An aggregate cannot be used in WHERE, because WHERE runs before the aggregate is computed.
