Aggregate functions
Aggregate functions compute one value from many rows. They are the base of every report or dashboard a manager asks for.
| number of rows | COUNT(*) |
| total | SUM(column) |
| average | AVG(column) |
| smallest / largest | MIN(column) MAX(column) |
SQL
SELECT COUNT(*) AS employees, MIN(salary) AS lowest, MAX(salary) AS highest, ROUND(AVG(salary), 2) AS average FROM employees;
You can aggregate with a condition. For example the total value of order 1:
SQL
SELECT SUM(quantity * unit_price) AS order_total FROM order_items WHERE order_id = 1;
Watch out
COUNT(*) counts all rows, while COUNT(column) skips rows where that column is NULL. Try COUNT(manager_id).