MySQL MIN, MAX, COUNT, AVG, SUM
Aggregate functions turn many rows into one value: the cheapest price, the number of orders, the average salary, the total revenue. Used without GROUP BY, they summarise the whole set of rows that passed the WHERE filter.
| Function | Returns |
|---|---|
COUNT(*) |
number of rows |
COUNT(expr) |
number of non-NULL values of expr |
COUNT(DISTINCT expr) |
number of different non-NULL values |
SUM(expr) |
total of the values |
AVG(expr) |
average of the values |
MIN(expr) / MAX(expr) |
smallest / largest value |
MIN and MAX
MIN and MAX work on numbers, text (alphabetical order) and dates:
SELECT MIN(unit_price) AS cheapest,
MAX(unit_price) AS dearest,
MIN(name) AS first_name_alphabetically,
MAX(hire_date) AS newest_hire
FROM products, employees;+----------+---------+---------------------------+-------------+
| cheapest | dearest | first_name_alphabetically | newest_hire |
+----------+---------+---------------------------+-------------+
| 1.25 | 24.50 | Aged Gruyere 250g | 2024-05-06 |
+----------+---------+---------------------------+-------------+
Careful: listing two tables in FROM combines every product with every employee (a cross join, see later). For MIN/MAX that happens not to change the answer, but it would ruin a COUNT or SUM. Query each table separately:
SELECT MIN(unit_price) AS cheapest, MAX(unit_price) AS dearest FROM products;
SELECT MAX(hire_date) AS newest_hire FROM employees;+----------+---------+
| cheapest | dearest |
+----------+---------+
| 1.25 | 24.50 |
+----------+---------+
+-------------+
| newest_hire |
+-------------+
| 2024-05-06 |
+-------------+
COUNT
SELECT COUNT(*) AS orders,
COUNT(shipped_date) AS shipped,
COUNT(DISTINCT customer_id) AS different_customers
FROM orders;+--------+---------+---------------------+
| orders | shipped | different_customers |
+--------+---------+---------------------+
| 22 | 17 | 13 |
+--------+---------+---------------------+
22 orders, 17 of which have a shipping date, placed by 13 different customers.
SUM and AVG
The value of an order line is quantity * unit_price * (1 - discount). Sum it to get revenue:
SELECT SUM(quantity) AS items_sold,
SUM(quantity * unit_price * (1 - discount)) AS revenue,
ROUND(AVG(quantity * unit_price * (1 - discount)), 2) AS avg_line_value
FROM order_details;+------------+----------+----------------+
| items_sold | revenue | avg_line_value |
+------------+----------+----------------+
| 127 | 555.9495 | 10.90 |
+------------+----------+----------------+
ROUND(x, 2) keeps two decimals. AVG of decimal values returns extra digits, so rounding for display is common.
Aggregates and the WHERE clause
WHERE runs before aggregation, so it decides which rows get summarised:
SELECT COUNT(*) AS sales_reps, AVG(salary) AS avg_salary, SUM(salary) AS payroll
FROM employees
WHERE title = 'Sales Rep';+------------+-------------+----------+
| sales_reps | avg_salary | payroll |
+------------+-------------+----------+
| 3 | 3733.333333 | 11200.00 |
+------------+-------------+----------+
You cannot use an aggregate inside WHERE (WHERE salary > AVG(salary) is an error), because WHERE looks at one row at a time. Use a subquery for that, or HAVING for conditions on groups.
NULLs are skipped
All aggregates except COUNT(*) ignore NULLs. On an empty set (no rows match), COUNT returns 0 and the others return NULL:
SELECT COUNT(*) AS n, SUM(unit_price) AS total, MAX(unit_price) AS top
FROM products
WHERE unit_price > 1000;+---+-------+------+
| n | total | top |
+---+-------+------+
| 0 | NULL | NULL |
+---+-------+------+
Tip:
AVG(col)divides by the number of non-NULL values. If NULL should count as 0, useAVG(IFNULL(col, 0)).
Test yourself
What does COUNT(email) count?
- All rows
- Rows where email is not NULL
- Distinct emails
- Rows where email is NULL
What does SUM(unit_price) return when no rows match?
- 0
- NULL
- An error
- An empty string
Why is WHERE salary > AVG(salary) invalid?
- AVG needs GROUP BY
- WHERE is evaluated row by row, before aggregation
- salary is a reserved word
- AVG only works on integers