Aggregate Functions
Learn how COUNT, SUM, AVG, MIN, and MAX summarize data across multiple rows in a single result.
Introduction
So far, every query has returned one row of output per row of input. But often you do not want individual rows — you want a summary. How many students are there? What is the average score? Who scored the highest?
Aggregate functions answer exactly these kinds of questions by summarizing data across many rows into a single value.
- What aggregate functions are and what problem they solve.
- How to use COUNT(), SUM(), AVG(), MIN(), and MAX().
- A worked example computing the average and highest score across all students.
- The difference between COUNT(*) and COUNT(column).
- How aggregate functions set up the next lesson: GROUP BY.
What Are Aggregate Functions?
An aggregate function takes many rows of data as input and returns a single summarizing value as output — a count, a total, an average, a minimum, or a maximum.
This is fundamentally different from the queries you have written so far, where every matching row appeared individually in the result.
| Function | What It Calculates |
|---|---|
| COUNT() | The number of rows (or non-NULL values in a column). |
| SUM() | The total of all values in a numeric column. |
| AVG() | The average of all values in a numeric column. |
| MIN() | The smallest value in a column. |
| MAX() | The largest value in a column. |
COUNT, SUM, AVG, MIN, MAX
Consider a "students" table with a marks column. Each of the five aggregate functions can be applied to that column to answer a different question.
| id | name | marks |
|---|---|---|
| 1 | Alex | 88 |
| 2 | Sam | 70 |
| 3 | Priya | 91 |
| 4 | Neha | 82 |
SELECT COUNT(*) AS total_students,
SUM(marks) AS total_marks,
AVG(marks) AS average_marks,
MIN(marks) AS lowest_marks,
MAX(marks) AS highest_marks
FROM students;+-----------------+-------------+---------------+--------------+---------------+
| total_students | total_marks | average_marks | lowest_marks | highest_marks |
+-----------------+-------------+---------------+--------------+---------------+
| 4 | 331 | 82.7500 | 70 | 91 |
+-----------------+-------------+---------------+--------------+---------------+A single query just answered five different summary questions about the entire table at once, without you having to manually add up numbers yourself.
Worked Example
Suppose you specifically need the average score across all students, and the highest score achieved, for a class report.
SELECT AVG(marks) AS average_score, MAX(marks) AS highest_score
FROM students;+---------------+----------------+
| average_score | highest_score |
+---------------+----------------+
| 82.7500 | 91 |
+---------------+----------------+The average across all four students is 82.75, and the highest single score is 91 — both computed directly by MySQL, without pulling all the raw rows back and calculating it yourself.
COUNT(*) vs COUNT(column)
COUNT(*) counts every row in the result, regardless of what is inside any particular column, including rows that contain NULL values. COUNT(column) counts only the rows where that specific column is not NULL.
This distinction matters. Suppose one student's marks have not been entered yet and are stored as NULL.
| id | name | marks |
|---|---|---|
| 1 | Alex | 88 |
| 2 | Sam | 70 |
| 3 | Priya | 91 |
| 4 | Neha | NULL |
SELECT COUNT(*) AS all_rows, COUNT(marks) AS rows_with_marks
FROM students;+-----------+------------------+
| all_rows | rows_with_marks |
+-----------+------------------+
| 4 | 3 |
+-----------+------------------+COUNT(*) returned 4, counting every row including Neha's. COUNT(marks) returned 3, because it skips any row where marks is NULL.
Use COUNT(*) when you want the total number of rows. Use COUNT(column_name) when you specifically want to know how many rows have a real, non-NULL value in that column.
Looking Ahead to GROUP BY
So far, every aggregate function has summarized the entire table into a single row. But what if you wanted the average marks per grade, or the count of students per class, instead of one grand total?
That is exactly what the GROUP BY clause, covered in the next lesson, unlocks — it lets you apply these same aggregate functions separately to different groups of rows within a single query, dramatically increasing their power.
Common Mistakes
- Mixing an aggregate function with a plain column in SELECT without GROUP BY, which either errors or produces confusing results.
- Assuming COUNT(column) behaves identically to COUNT(*) — it does not, since it ignores NULL values.
- Forgetting that AVG() and SUM() ignore NULL values within their column, rather than treating them as zero.
- Applying an aggregate function inside a WHERE clause — aggregate functions belong in HAVING instead, which you will learn alongside GROUP BY.
Best Practices
- Alias every aggregate column (like AVG(marks) AS average_marks) so results are easy to read.
- Use COUNT(*) for row totals and COUNT(column) when NULLs should be excluded.
- Combine multiple aggregate functions in one query when you need several summary statistics at once.
- Remember that NULL values are automatically excluded from SUM, AVG, MIN, and MAX calculations.
Frequently Asked Questions
Do aggregate functions ignore NULL values?
Yes, for SUM, AVG, MIN, MAX, and COUNT(column) — all of them skip NULL values automatically. COUNT(*) is the exception, since it counts rows regardless of NULLs.
What is the difference between COUNT(*) and COUNT(column)?
COUNT(*) counts every row in the result set. COUNT(column) counts only the rows where that specific column has a non-NULL value.
Can I use more than one aggregate function in the same query?
Yes. You can select COUNT(), SUM(), AVG(), MIN(), and MAX() all together in a single SELECT statement, as shown in this lesson.
Why do aggregate functions matter more once I learn GROUP BY?
Without GROUP BY, an aggregate function summarizes the whole table into one row. With GROUP BY, it can summarize each group separately — for example, the average marks per grade — which is far more useful in practice.
Key Takeaways
- Aggregate functions summarize many rows into a single value.
- COUNT(), SUM(), AVG(), MIN(), and MAX() are the five core aggregate functions.
- COUNT(*) counts all rows; COUNT(column) counts only non-NULL values in that column.
- SUM, AVG, MIN, and MAX all ignore NULL values automatically.
- Aggregate functions become dramatically more useful once combined with GROUP BY.
Summary
Aggregate functions let you compute summary statistics — counts, totals, averages, minimums, and maximums — directly inside a query, instead of pulling raw rows back and calculating manually. COUNT(*) and COUNT(column) behave differently around NULL values, which is a subtle but important distinction.
In this lesson, you learned all five core aggregate functions and computed the average and highest score across the students table. Next, you will learn GROUP BY, which lets you apply these same functions to separate groups of rows within one query.
- You can use COUNT(), SUM(), AVG(), MIN(), and MAX().
- You computed the average and highest score across all students.
- You understand the difference between COUNT(*) and COUNT(column).
- You are ready to learn GROUP BY.