Subscriber only lesson.
Sign up to this course to view this lesson.
The Having Clause lets us drill down and search within our aggregate groups.
When to use
Whenever you use "Group By" and want to search within that aggregate, use Having.
The Having Clause looks like this:
SELECT COUNT([Country]), [Country]
GROUP BY [Country]
HAVING COUNT([Country]) < 2
Like with Group By, this will give us a column with the number of customers from each country, and a corresponding column to show which country they are from. But in this case, due to the Having Clause, it will only return countries with less than 2 people.
Hints & tips
- Having lets you search within your Group By Aggregate count.
- You can only search columns listed in the aggregate count or in the Group By clause with Having.
Lesson notes are only available for subscribers.