COUNT, SUM and AVG
Aggregate functions: they turn many rows into one value, or one value per group.
Syntax
COUNT(*)COUNT(column)SUM(column)AVG(column)
- COUNT(*) counts rows; COUNT(column) counts non-NULL values.
- SUM, AVG, MIN and MAX work on numbers or dates.
Example
SELECT channel, COUNT(*) AS orders FROM orders GROUP BY channel;
The number of orders through each channel.
Tables used: orders. Try it in a lesson below.
Common mistakes
- AVG ignores NULLs, so an average over a column with missing values covers only the rows that have a value.
- An aggregate without GROUP BY summarizes the whole table into one row.