SQL Fundamentals

Lesson 4 of 6

Aggregating with GROUP BY

Aggregate functions summarize many rows into one value, COUNT, SUM, AVG, MIN, MAX. GROUP BY buckets rows before aggregating.

SELECT course_title, COUNT(*) AS student_count
FROM enrollments
GROUP BY course_title;

This counts how many enrollment rows exist per course_title, one summary row per group.

Filtering groups with HAVING

WHERE filters individual rows before grouping, HAVING filters the grouped results after aggregating:

SELECT course_title, AVG(xp) AS average_xp
FROM enrollments
JOIN students ON students.id = enrollments.student_id
GROUP BY course_title
HAVING AVG(xp) > 50;

You can't write WHERE AVG(xp) > 50, the average doesn't exist yet at the point WHERE runs, that's exactly what HAVING is for.

📝 Aggregation Quiz

Passing score: 70%
  1. 1.Which clause filters groups after aggregation, where WHERE cannot be used?

  2. 2.COUNT(*) counts the number of ____ in each group.

  3. 3.You can filter on an aggregate result like COUNT(*) using a plain WHERE clause.