SQL aggregate functions: SUM, AVG, MIN and MAX
What did we sell, what does the team earn on average, when was the last order? Aggregate functions boil a whole column down to one number, much like SUM, AVERAGE, MIN and MAX in a spreadsheet.
In short: SELECT SUM(salary), ROUND(AVG(salary), 2), MIN(salary), MAX(salary) FROM employees; returns the total, the rounded average, and the smallest and largest value in one row. All four skip NULL. You can put a calculation inside the parentheses too: SUM(quantity * unit_price).
What you should know first: counting with COUNT, calculations with ROUND, and the NULL lesson.
Try it now
Three figures, one query, one result row.
Task For a meeting with management you need three figures about the product range from the products table, in this order: the lowest price, the highest price and the average price, rounded to 2 decimal places.
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
How SUM, AVG, MIN and MAX work
Like COUNT, these functions take every row WHERE lets through and turn them into a single value. You can use as many of them in one query as you like:
SELECT SUM(stock) AS units_in_stock,
MIN(price), MAX(price),
ROUND(AVG(price), 2)
FROM products;
| units_in_stock | MIN(price) | MAX(price) | ROUND(AVG(price), 2) |
|---|---|---|---|
| 1181 | 2.79 | 449 | 116.96 |
| Function | Returns | Excel |
|---|---|---|
SUM(x) | total | SUM |
AVG(x) | average (total divided by the number of values) | AVERAGE |
MIN(x) | smallest value | MIN |
MAX(x) | largest value | MAX |
AVG doesn't round, so without ROUND you'd see 116.9625. Money needs cents, so you round, and you do it on the outside: ROUND(AVG(price), 2) rounds the finished average.
Totals of a calculation
The order_items table has no column for the value of a line, only quantity and unit_price. You work it out inside the parentheses. For order 1003:
SELECT SUM(quantity) AS units,
ROUND(SUM(quantity * unit_price), 2) AS order_value
FROM order_items
WHERE order_id = 1003;
| units | order_value |
|---|---|
| 116 | 920.3 |
The database multiplies quantity by unit_price for each of the three lines first and then adds the results. It's what you'd do in a spreadsheet with a helper column, minus the helper column.
Practice: revenue for one product
Now across several orders with different unit prices.
Task Purchasing is negotiating with the paper supplier and needs two figures for copy paper (product_id 1) across all order items: units (all packs sold, added up) and revenue (quantity times unit price, summed over all items and rounded to 2 decimal places). The columns must have exactly these names.
order_items 38 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.9 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
NULL is skipped
Customer service has two people, but only one has a salary on file:
SELECT AVG(salary), SUM(salary), COUNT(salary), COUNT(*)
FROM employees
WHERE department = 'Customer service';
| AVG(salary) | SUM(salary) | COUNT(salary) | COUNT(*) |
|---|---|---|---|
| 38400 | 38400 | 1 | 2 |
The average is 38400, not 19200. AVG divides by the number of values that exist, not by the number of rows. Most of the time that's what you want, because a missing salary isn't a salary of 0. If a missing value really should count as 0, replace it first: AVG(COALESCE(salary, 0)).
When no rows match at all, SUM, AVG, MIN and MAX return NULL, while COUNT returns 0. If a report needs a 0, write COALESCE(SUM(price), 0).
Task HR is preparing the budget. From the employees table, show three numbers: total_salary (all annual salaries on file, added up), avg_salary (the average salary, rounded to 2 decimal places) and missing_salary (how many employees don't have a salary entered yet). The columns must have exactly these names.
employees 10 rows, 7 columns
| employee_id | first_name | last_name | department | salary | hire_date | manager_id |
|---|---|---|---|---|---|---|
| 1 | Karen | Mitchell | Management | 93600 | 2018-01-02 | NULL |
| 2 | Tom | Bradley | Sales | 62400 | 2019-04-01 | 1 |
| 3 | Julia | Reyes | Sales | 46800 | 2021-06-15 | 2 |
| 4 | Marcus | Allen | Sales | 49200 | 2020-09-01 | 2 |
| 5 | Sarah | Long | Warehouse | 39600 | 2022-02-01 | 1 |
| 6 | Felix | Turner | Warehouse | 35400 | 2024-03-18 | 5 |
| 7 | Nina | Cooper | Accounting | 52800 | 2019-10-01 | 1 |
| 8 | David | Ross | Customer service | 38400 | 2023-05-02 | 2 |
| 9 | Laura | Hughes | Customer service | NULL | 2025-09-01 | 2 |
| 10 | Paul | Ward | Accounting | 43200 | 2022-11-14 | 7 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Watch out: AVG of whole numbers
Stock is a whole number. In the exercises, AVG(stock) returns 98.41666… with lots of decimal places, because SQLite always gives the average as a decimal. SQL Server does it differently: according to the Microsoft docs, AVG over an int column returns int, so you get 98 with no decimals. The fix is the same trick as with division: AVG(stock * 1.0) makes the database work with decimals.
MIN and MAX on text and dates
MIN and MAX aren't limited to numbers. Text is compared alphabetically, dates by time:
SELECT MIN(order_date) AS first_order, MAX(order_date) AS last_order
FROM orders;
| first_order | last_order |
|---|---|
| 2026-01-08 | 2026-06-14 |
This works because the date is stored as text in the format YYYY-MM-DD, where alphabetical order is also time order. Stored as text like 31/01/2026, 31 January would count as later than 14 June, because text is compared character by character and "3" comes after "1". On names, MIN(name) in the customers table returns "Chloe Evans" and MAX(name) "Westbrook Dental Clinic". SUM and AVG, on the other hand, only work with numbers.
Common mistakes
SUM times SUM instead of SUM of a calculation
SUM(quantity) * SUM(unit_price) multiplies all quantities by all prices and comes out far too high. The exercise says "Columns and row count match, but not every value." What you want is SUM(quantity * unit_price).
Spreadsheet names
Coming from Excel, it's easy to type AVERAGE(price). The exercise then says "The function AVERAGE does not exist in this practice database (SQLite)." In SQL it's AVG.
An aggregate in WHERE
WHERE price > AVG(price) sounds logical but fails, because WHERE checks each row on its own before any average exists. The exercise says "COUNT, SUM or AVG cannot go directly in WHERE" and points you to a subquery, which you learn in level 3.
A name next to MAX
SELECT name, MAX(price) FROM products shows the standing desk at 449 in SQLite, because with a single MIN or MAX, SQLite takes the other columns from the matching row. That is a SQLite quirk; most other databases reject the query. For "the most expensive product", use ORDER BY price DESC with LIMIT 1.
Averaging over all rows
SUM(salary) / COUNT(*) counts missing salaries too and drags the average down. AVG only counts values that exist.
SUM, AVG, MIN and MAX in MySQL, PostgreSQL, SQL Server and SQLite
All four functions exist everywhere, and everywhere they skip NULL. The differences are in the type of the result:
| Database | What's different |
|---|---|
| MySQL | SUM and AVG return DECIMAL for whole numbers and DECIMAL columns, DOUBLE for FLOAT and DOUBLE. With no matching rows both return NULL. |
| PostgreSQL | avg of integer returns numeric, sum of integer returns bigint. The manual points out that sum of no rows is NULL, not zero, and suggests coalesce. |
| SQL Server | AVG and SUM of int return int, so the average has no decimals. MIN and MAX also work on text and datetime; text is compared by the collating sequence. |
| SQLite (these exercises) | avg always returns a decimal. sum returns a whole number if all values are whole numbers, and NULL with no values. There is also total(), which returns 0.0 with no values. |
FAQ
What's the difference between SUM and COUNT?
COUNT counts rows, SUM adds up values. Three order lines of 10 units each: COUNT(quantity) is 3, SUM(quantity) is 30.
How do I get the most expensive product, not just its price?
MAX only gives you the value. For the whole row, sort descending and take the first one: SELECT name, price FROM products ORDER BY price DESC LIMIT 1.
Can I nest aggregate functions?
Not directly. MAX(SUM(quantity)) is an error. Questions like "which customer bought the most" are solved with GROUP BY and, later, subqueries.
Can I keep calculating with the result?
Yes. MAX(price) - MIN(price) is the price range, and SUM(quantity * unit_price) * 1.08 adds 8% sales tax. The result of an aggregate is an ordinary number.