SQLite Aggregate Functions
Aggregate Functions
An aggregate function calculates one value from many rows. For example, it can count the rows, or find the highest price.
These are the most used aggregate functions in SQLite:
| Function | Returns |
|---|---|
| MIN() | The smallest value |
| MAX() | The largest value |
| COUNT() | The number of rows |
| SUM() | The sum of the values |
| AVG() | The average of the values |
TOTAL() | The sum of the values, as a decimal number. Gives 0.0 when there are no values |
GROUP_CONCAT() | All the values joined together into one text |
Example
This example counts the products, and finds the lowest, highest and average price:
Aggregates for Each Group
Together with GROUP BY, an aggregate function calculates one value for each group. This example counts the products in each category. You will learn more about GROUP BY in a later chapter:
Aggregates and NULL
Aggregate functions skip NULL values. The only exception is COUNT(*), which counts all the rows.