TetSharing
SQL tutorial · 14 chapters
  1. Get started
  2. SELECT columns
  3. WHERE filters
  4. AND, OR, NOT, IN, BETWEEN
  5. ORDER BY and LIMIT
  6. LIKE patterns
  7. NULL values
  8. COUNT, SUM, AVG, MIN, MAX
  9. GROUP BY and HAVING
  10. INNER JOIN
  11. LEFT JOIN
  12. Subqueries
  13. INSERT, UPDATE, DELETE
  14. CREATE TABLE and constraints

Final SQL quiz
Cheat sheet: sql cheat sheet

Chapter 8 of 14 · By Ethan Cole · Updated November 21, 2026

SQL Aggregate Functions: COUNT, SUM, AVG, MIN, MAX

So far every query has returned rows from the table. Aggregate functions collapse many rows into a single value: how many books are there, how many copies are in stock, what is the average price? They are the foundation of every report and dashboard.

The five you will use daily

  • count(*) counts rows.
  • sum(column) adds up the values.
  • avg(column) computes the mean.
  • min(column) and max(column) find the smallest and largest values. They also work on text (alphabetical order) and on ISO dates stored as text.

Without a GROUP BY clause, which is the next chapter, an aggregate query treats the whole table, or whatever rows pass the WHERE filter, as one group and returns exactly one row.

SQLite · editable

Expected output

books | copies | avg_price | cheapest | newest
10 | 101 | 22.57 | 11.0 | 2024
genres
3

Rounding

avg(price) returns a floating-point number such as 22.568. Wrap it in round(value, 2) to get two decimals. Rounding belongs at the very end of a calculation; rounding each price first and then averaging can produce a slightly different answer.

COUNT variations

  • count(*) counts every row.
  • count(column) counts rows where that column is not NULL.
  • count(DISTINCT column) counts different non-NULL values. Our ten books fall into three genres.

Filtering before aggregating

WHERE runs before the aggregates, so SELECT avg(price) FROM books WHERE genre = 'Tech'; averages only Tech books. You cannot put an aggregate inside WHERE, as in WHERE price > avg(price), because the average does not exist yet at that stage. That needs a subquery, covered in a later chapter.

Mixing aggregates and plain columns

In standard SQL, a query that uses aggregates may only select plain columns that are also listed in GROUP BY. SQLite is lenient and lets you write SELECT title, max(price) FROM books, returning the title from the row with the maximum price. Other databases reject that query, so do not rely on it if your SQL needs to be portable.

Common mistakes

  • Expecting count(column) to count rows with NULLs.
  • Using an aggregate inside WHERE.
  • Summing floating-point money and expecting exact cents; store cents as integers for exact totals.

Summary

  • count, sum, avg, min and max reduce many rows to one value.
  • Without GROUP BY, the whole filtered table is one group.
  • count(DISTINCT col) counts unique values; round() tidies decimals.
  • WHERE filters rows before aggregation.

Quiz: COUNT, SUM, AVG, MIN, MAX

1. What does SELECT count(*) FROM books WHERE stock = 0; return?

WHERE filters first, then count(*) counts the remaining rows.

2. Which function returns the number of different genres?

DISTINCT inside count counts unique non-NULL values.

3. Why does WHERE price > avg(price) fail?

Use a subquery to compare against an aggregate.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.