Aggregate functions
Aggregate functions (COUNT, SUM, AVG, MIN, MAX) turn many rows into a single value. Use GROUP BY to get one result per group and HAVING to filter the groups. WHERE filters rows before grouping; HAVING filters after.
SELECT COUNT(*) FROM orders; SELECT AVG(total) FROM orders; -- Orders per status SELECT status, COUNT(*) AS count FROM orders GROUP BY status; -- Categories with an average price over 20 SELECT category, AVG(price) AS avg_price FROM products WHERE in_stock = true GROUP BY category HAVING AVG(price) > 20;