COUNT / SUM / AVG / MIN / MAX: The standard aggregate functions. COUNT(*) counts rows; COUNT(col) ignores NULLs; the rest reduce a column to one value. COUNT, SUM, AVG, MIN and MAX are SQL's aggregate functions. They reduce many rows to one value: how many orders, the total revenue, the average order size, the cheapest and most expensive product.
Frequently asked questions about COUNT / SUM / AVG / MIN / MAX
How is COUNT / SUM / AVG / MIN / MAX used in practice?
Combined with GROUP BY, they become the basis of most reports. SELECT customer_id, SUM(total) FROM orders GROUP BY customer_id gives revenue per customer in one query.
What should you keep in mind with COUNT / SUM / AVG / MIN / MAX?
Watch how they treat NULL. COUNT(*) counts all rows, but COUNT(column) skips rows where that column is NULL. AVG ignores nulls too, which can make an average look higher than you expect. Doing these calculations in the database is almost always much faster than loading every row into PHP or JavaScript and adding them up there.
What does an example of COUNT / SUM / AVG / MIN / MAX look like?
SELECT COUNT(*), SUM(total), AVG(total), MIN(total), MAX(total) FROM orders;