Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
HAVING Clause.docx58.8 KB HAVING Clause - Solution.docx
59.4 KB
Quick reference
HAVING Clause
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.
Instructions
The Having Clause looks like this:
SELECT COUNT([Country]), [Country]
FROM [Customer].[dbo].[Current_Customers]
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.