Summarising data
Aggregate functions
Goal: Compute counts, sums, averages, minima and maxima · 8 min
Aggregate functions collapse many rows into one value:
| function | result |
|---|---|
| COUNT(*) | number of rows |
| COUNT(col) | number of non-NULL values |
| SUM(col) | total |
| AVG(col) | average |
| MIN(col) / MAX(col) | smallest / largest |
They respect WHERE: SELECT AVG(price) FROM products WHERE category = 'furniture'; Use ROUND(x, 2) to round results for display.
Worked example: Price statistics
- Six products in total.
- Sum 1495, average ≈ 249.17.
- Cheapest 25, most expensive 900.
SELECT COUNT(*), SUM(price), ROUND(AVG(price), 2), MIN(price), MAX(price) FROM products;Output:
6 | 1495.0 | 249.17 | 25.0 | 900.0