Loading lessons...
Aggregate Functions
Aggregate Functions
Aggregate functions compute a single value from many rows.
The most common ones
| Function | Returns |
|---|---|
| COUNT() | how many rows |
| SUM() | total of a numeric column |
| AVG() | average of a numeric column |
| MIN() | smallest value |
| MAX() | largest value |
Examples
SELECT COUNT(CustomerID) FROM Customers;
SELECT SUM(Price) FROM Products;
SELECT AVG(Price) FROM Products;
SELECT MIN(Price) FROM Products;
SELECT MAX(Price) FROM Products;
COUNT vs COUNT(*)
COUNT(column)counts rows where that column is not NULL.COUNT(*)counts all rows.
They ignore NULL (except COUNT(*))
- SUM, AVG, MIN, MAX ignore NULL values.
- COUNT(column) ignores NULLs too.
Combine with WHERE
SELECT COUNT(*) FROM Orders
WHERE CustomerID = 1;
Counts only the rows that match the condition.
TL;DR
- Aggregates turn many rows into one value.
- COUNT, SUM, AVG, MIN, MAX.
- They ignore NULLs, except COUNT(*).
- WHERE filters before aggregating.