01 / 20
SUM AVG COUNT MAX

Basic Aggregate
Functions in SQL

Learn to summarize entire columns of data with five powerful functions: COUNT, SUM, AVG, MIN, and MAX.

SQL

What Are Aggregate Functions?

An aggregate function takes a whole column of values and collapses them into a single result. Instead of seeing every individual row, you get one summary number: a total, an average, a count, or an extreme value.

Row 1: 150 Row 2: 200 Row 3: 350 Row 4: 100 Row 5: 200 Many rows SUM() 1000 One result

Why Aggregate Functions Matter

Real databases can contain millions of rows. You rarely want to read every single row: you want answers. Aggregate functions let you ask big-picture questions directly in SQL.

Business
"What is our total revenue this quarter?"
Science
"What is the average temperature across all sensors?"
Education
"How many students scored above 90?"

Meet the Five Functions

COUNT
Counts how many rows (or non-NULL values) exist
SUM
Adds up all numeric values in a column
AVG
Calculates the arithmetic mean of numeric values
MIN
Returns the smallest value in a column
MAX
Returns the largest value in a column

The General Syntax

SELECT FUNCTION(column_name)
FROM table_name
WHERE condition; -- optional filter

COUNT: Counting Rows

-- Count ALL rows (including NULLs)
SELECT COUNT(*) FROM students;

-- Count only non-NULL emails
SELECT COUNT(email) FROM students;
COUNT(*)
Counts every row, even if all columns are NULL
COUNT(column)
Counts rows where that column is not NULL

SUM and AVG: Totals and Means

-- Total of all scores
SELECT SUM(score) FROM exams;

-- Average score
SELECT AVG(score) FROM exams;

MIN and MAX: Extremes

-- Lowest price in the catalog
SELECT MIN(price) FROM products;

-- Most recent order date
SELECT MAX(order_date) FROM orders;

Key Distinction: COUNT(*) vs COUNT(column)

idnameemail
1Alicealice@mail.com
2BobNULL
3Carolcarol@mail.com
4DaveNULL
5Eveeve@mail.com
COUNT(*) → 5
Counts all rows, including those with NULL emails
COUNT(email) → 3
Skips the 2 rows where email is NULL

How to Apply: Step by Step

1
Identify the question. What summary do you need: a count, total, average, minimum, or maximum?
2
Choose the function. Match the question to COUNT, SUM, AVG, MIN, or MAX.
3
Pick the column. Place the target column name inside the parentheses. Use * with COUNT if you want all rows.
4
Add a WHERE clause (optional). Filter rows before aggregation if needed.
5
Run and verify. Check that the result makes sense given your data.

Example 1: Simple COUNT

Question: How many orders were placed in the orders table?

SELECT COUNT(*) AS total_orders
FROM orders;
total_orders
847

Example 2: SUM with a Filter

Question: What is the total revenue from orders placed in January 2024?

SELECT SUM(amount) AS jan_revenue
FROM orders
WHERE order_date >= '2024-01-01'
AND order_date < '2024-02-01';
jan_revenue
42,350.00

Example 3: Multiple Aggregates at Once

Question: Show the count, average, min, and max score from the exams table.

SELECT
  COUNT(score) AS num_scores,
  AVG(score) AS avg_score,
  MIN(score) AS lowest,
  MAX(score) AS highest
FROM exams;
num_scoresavg_scorelowesthighest
12073.528100

Common Mistakes to Avoid

✕ Mixing Aggregates with Raw Columns
Writing SELECT name, COUNT(*) without GROUP BY is invalid: the database cannot return one count alongside many names.
✕ Forgetting NULLs
Using COUNT(column) when you mean COUNT(*) will silently skip NULLs and undercount your rows.
✕ SUM/AVG on Text
Applying SUM or AVG to a text column produces an error (or nonsense in some databases). These need numeric data.

Summary Comparison

Function Returns Works On Ignores NULL?
COUNT(*) Row count Any No
COUNT(col) Non-NULL count Any Yes
SUM Total Numeric Yes
AVG Mean Numeric Yes
MIN Smallest Any orderable Yes
MAX Largest Any orderable Yes

Question 1 of 5

Which SQL function would you use to find the total sales amount across all rows in a sales table?

Question 2 of 5

A table has 10 rows. The email column is NULL in 3 of those rows. What does COUNT(email) return?

Question 3 of 5

Which query correctly finds the highest price in a products table?

Question 4 of 5

A scores column contains: 80, NULL, 90, 70, NULL. What does AVG(score) return?

Question 5 of 5

What is wrong with this query?

SELECT name, COUNT(*) FROM students;