CHAPTER 10
Turning millions of rows into a single meaningful number.
🌟 Think of it this way: Aggregate functions are like a cashier totalling your bill. You bring 20 items (rows) to the counter; they give you back one number (the total). The individual items are collapsed into a single summary.
How many employees?
SELECT COUNT(*) AS total_employees FROM employees;
SQL
SELECT COUNT(*) AS all_rows,
COUNT(manager_id) AS has_manager
FROM employees;Full payroll summary
SELECT
COUNT(*) AS headcount,
SUM(salary) AS total_payroll,
AVG(salary) AS avg_salary,
MIN(salary) AS min_salary,
MAX(salary)
FROM employees;