Aggregate functions on their own give one answer for the whole table. GROUP BY splits the rows into groups first (one per department, one per city) and then runs the aggregate on each group, so you get a summary per group. HAVING then filters those groups, for example “only departments with more than two students”.
In this lesson you will learn
- How
GROUP BYforms groups - Which columns you may select alongside aggregates
- How to filter groups with
HAVING - The difference between
WHEREandHAVING - The order in which SQL actually runs a query
GROUP BY
SELECT dept, COUNT(*) AS students, ROUND(AVG(cgpa), 2) AS avg_cgpa
FROM students
GROUP BY dept
ORDER BY students DESC;
SELECT city, COUNT(*) AS students
FROM students
GROUP BY city
ORDER BY students DESC, city;
Output
+------+----------+----------+
| dept | students | avg_cgpa |
+------+----------+----------+
| CSE | 4 | 3.81 |
| EEE | 2 | 3.2 |
| BBA | 2 | 3.33 |
+------+----------+----------+
(3 rows)
+------------+----------+
| city | students |
+------------+----------+
| Dhaka | 3 |
| Chattogram | 2 |
| Khulna | 1 |
| Rajshahi | 1 |
| Sylhet | 1 |
+------------+----------+
(5 rows)
- Rows with the same
deptare collected into one group, andCOUNTandAVGrun once per group. - Every column in the
SELECTlist must either be inGROUP BYor be inside an aggregate. Selectingnamehere would make no sense, because each group contains several names. - The CSE average (3.81) uses three values, since Nadia’s CGPA is
NULL. ORDER BYcan sort by an aggregate alias such asstudents.
Filtering Groups with HAVING
-- Departments with more than two students
SELECT dept, COUNT(*) AS students
FROM students
GROUP BY dept
HAVING COUNT(*) > 2;
-- WHERE filters rows first, HAVING filters groups after
SELECT dept, ROUND(AVG(cgpa), 2) AS avg_cgpa
FROM students
WHERE city <> 'Dhaka'
GROUP BY dept
HAVING AVG(cgpa) >= 3.4;
Output
+------+----------+
| dept | students |
+------+----------+
| CSE | 4 |
+------+----------+
(1 row)
+------+----------+
| dept | avg_cgpa |
+------+----------+
| CSE | 3.67 |
| EEE | 3.41 |
+------+----------+
(2 rows)
HAVING COUNT(*) > 2keeps only groups with more than two rows. Only CSE qualifies.- In the second query,
WHERE city <> 'Dhaka'first removes Dhaka students, then the remaining rows are grouped, thenHAVINGdrops groups averaging below 3.4. BBA (3.33) is dropped.
How SQL Runs a Query
| Step | Clause | What happens |
|---|---|---|
| 1 | FROM / JOIN | Pick the tables |
| 2 | WHERE | Filter rows |
| 3 | GROUP BY | Form groups |
| 4 | HAVING | Filter groups |
| 5 | SELECT | Compute the output columns |
| 6 | ORDER BY | Sort |
| 7 | LIMIT | Cut the result |
This order explains why WHERE cannot use aggregates (groups do not exist yet) and why HAVING can.
Common Mistakes
- Selecting a column that is neither grouped nor aggregated.
- Using
HAVINGfor row filters that belong inWHERE. It works but is slower, because the database groups rows it then throws away. - Using
WHEREwith an aggregate, such asWHERE COUNT(*) > 2, which is an error.
Practice Exercises
Exercise 1. Show the average marks per course id, highest first.
Show answer
SELECT course_id, ROUND(AVG(marks), 1) AS avg_marks
FROM enrollments
GROUP BY course_id
ORDER BY avg_marks DESC;
Exercise 2. List cities with exactly one student.
Show answer
SELECT city FROM students GROUP BY city HAVING COUNT(*) = 1;
-- Khulna, Rajshahi, Sylhet
Frequently Asked Questions
Can I group by more than one column?
Yes. GROUP BY dept, city makes one group for each department-and-city combination.
Is GROUP BY the same as DISTINCT?
For selecting unique values they give the same rows, but only GROUP BY lets you compute aggregates for each group.
Summary
GROUP BYruns aggregates once per group.- Selected columns must be grouped or aggregated.
WHEREfilters rows before grouping;HAVINGfilters groups after.- SQL runs FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT.
Next: SQL JOINs.
