Español Practice SQL

SQL COUNT, SUM, AVG, MIN and MAX

Aggregates turn many rows into one number: how many, how much, on average, the smallest and the largest.

SQL skills · Summaries

Practice this nowLearn it step by step

Free. No sign up. Your progress stays in this browser.

What you will learn

These five functions are the start of every report. They work on the rows that passed WHERE.

You will also see why COUNT(*) and COUNT(column) can give different answers.

The basics

An aggregate function turns many rows into one value. The five you will use every day are COUNT, SUM, AVG, MIN and MAX.

SELECT COUNT(*), SUM(amount), ROUND(AVG(amount), 2), MIN(due_date), MAX(due_date)
FROM invoices
WHERE status = 'Open'

WHERE runs first, so the aggregates only see the rows that passed the filter. The result is a single row.

NULL and aggregates

COUNT(*) counts every row. COUNT(column) counts only rows where that column is not NULL. SUM, AVG, MIN and MAX also skip NULL values, so the average of 10, NULL and 20 is 15.

Rounding

ROUND(value, 2) keeps two decimals. Averages are rarely round numbers, so reports usually round them.

Worked examples

Example 1

How many open invoices, and what do they add up to?

SELECT COUNT(*), SUM(amount)
FROM invoices
WHERE status = 'Open'

One row with two numbers.

Example 2

The average deal, rounded to two decimals.

SELECT ROUND(AVG(amount), 2)
FROM deals

Averages are rarely round numbers, so reports round them.

Reading helps, typing makes it stick. Every task gives you a fresh table, and your query is checked by a real database.

Try a task

Common mistakes

  • Expecting AVG to count missing values as zero. It skips them.
  • Using COUNT(column) when you mean every row. It skips NULL values.
  • Putting a condition on SUM in WHERE. That belongs in HAVING.

Where you will use it at work

  • Finance: total outstanding amount.
  • Support: average response time.
  • Product: how many users are on the Pro plan.

Questions people ask

What does COUNT(DISTINCT column) do?

It counts the different values, for example how many different customers placed an order.

Do MIN and MAX work on text and dates?

Yes. On dates they give the earliest and the latest; on text, the first and last in alphabetical order.

Why is my SUM empty?

If no row passes the filter, SUM returns NULL, not zero. COALESCE(SUM(amount), 0) shows zero instead.

Ready to write it yourself?

Practice COUNT, SUM, AVG, MIN, MAX

Related guides