Applying Selection After Grouping with HAVING

You use the HAVING clause to apply selection criteria after data aggregation has been performed. Records are selected and summarized, and then only those meeting the conditions specified in the HAVING clause are displayed:

SELECT 
	Products.CategoryID, 
	Sum(Products.UnitsInStock) AS SumOfUnitsInStock
FROM Products
GROUP BY Products.CategoryID
HAVING Sum(Products.UnitsInStock) > 400;

The HAVING clause determines which groups created by the GROUP BY clause are returned in the query output. Note that although the HAVING clause and the WHERE clause are similar, they aren’t interchangeable. HAVING is used only in conjunction with the GROUP BY clause.