Aggregate functions calculate on sets of rows.
Functions
COUNT(*)� all rowsCOUNT(col)� non-NULL valuesSUM(),AVG(),MIN(),MAX()
Aggregate functions calculate on sets of rows.
COUNT(*) � all rowsCOUNT(col) � non-NULL valuesSUM(), AVG(), MIN(), MAX()SELECT COUNT(*) FROM users; SELECT COUNT(DISTINCT country) FROM users; SELECT SUM(total) AS revenue FROM orders WHERE status = "completed"; SELECT AVG(price) AS avg_price FROM products; SELECT MIN(price), MAX(price) FROM products;
SELECT COUNT(*) AS total_users, COUNT(DISTINCT city) AS unique_cities, AVG(age) AS avg_age, MIN(age) AS youngest, MAX(age) AS oldest, SUM(score) AS total_score FROM users; -- With filter SELECT COUNT(*) AS adults FROM users WHERE age >= 18; -- Aggregates with CASE SELECT AVG(CASE WHEN gender = "M" THEN score END) AS male_avg, AVG(CASE WHEN gender = "F" THEN score END) AS female_avg FROM users;
Total revenue per category.
SELECT category, SUM(total) AS revenue
FROM orders o JOIN products p ON o.product_id = p.id
WHERE status = "completed"
GROUP BY category;
COUNT(*) vs COUNT(col)?
COUNT(*) counts all rows; COUNT(col) excludes NULLs.
No. Use HAVING after GROUP BY to filter aggregated results.