T-SQL 101: 88 Filtering groups of data by using HAVING
I’ve previously talked about how the WHERE clause is used to limit the rows that are included in a query. If we’re using a GROUP BY, then WHERE is determining what goes into the grouping. But what if you want to apply a limit that’s based on the outcome of the grouping? That’s what HAVING does.
If I execute the following query:
SELECT Size,
COUNT(ProductID) AS NumberOfProducts
FROM dbo.Products
WHERE IsShownOnPriceList <> 0
GROUP BY Size;
I see this output:
2021-03-08