Aggregate Functions and GROUP BY
Aggregation is where SQL becomes genuinely powerful — you can summarize millions of rows into meaningful statistics in a single query. COUNT, SUM, AVG, MAX, and MIN are the core aggregate functions.
Key Concepts
COUNT, SUM, AVG, MAX, MIN
SELECT COUNT(*) FROM students counts all rows. SELECT AVG(gpa) FROM students returns the average GPA. SELECT MAX(score) FROM tests returns the top score. These run over entire columns and return a single summary value.
GROUP BY
GROUP BY lets you aggregate by category. SELECT grade, AVG(gpa) FROM students GROUP BY grade gives the average GPA per grade level. Without GROUP BY, AVG(gpa) returns one number for the whole table.
HAVING — Filtering Groups
WHERE filters rows before grouping. HAVING filters groups after aggregation. SELECT grade, COUNT(*) as total FROM students GROUP BY grade HAVING total > 5 shows only grades that have more than 5 students.
🆕 Aggregation Lab
See how aggregate functions summarize data. Click to run different aggregations.
✅ Check Your Understanding
1. What does COUNT(*) return?
2. What does GROUP BY do?
3. What is the difference between WHERE and HAVING?