Tsallaka zuwa abun ciki
Justice Tech School

HAVING

WHERE filters rows before they are grouped and cannot use aggregate functions. HAVING filters the groups after aggregation, so it can.

Example
SELECT country, COUNT(*) AS students
FROM students
GROUP BY country
HAVING COUNT(*) >= 2
ORDER BY students DESC;

Result

country | students
Nigeria | 4
Ghana   | 2
Example
SELECT country, ROUND(AVG(score), 1) AS average
FROM students
WHERE score IS NOT NULL
GROUP BY country
HAVING AVG(score) > 70;

Result

country | average
Ghana   | 78.0
Nigeria | 84.0

Exercise

Filter the groups.

... GROUP BY country  COUNT(*) > 1

Quiz Test

Which clause can use an aggregate function in its condition?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.