DELAPARRA
Index
Color
NL
Category: SQL

COUNT / SUM / AVG / MIN / MAX

Explained by Updated

What is COUNT / SUM / AVG / MIN / MAX?

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.

Definition source Coders Lingo, CC BY 4.0

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;

At a glance

CategorySQL
Explained by
Updated

More SQL terms

All terms starting with C

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.