SQL Server Interview Question #88

What is the HAVING clause?

Aggregate Functions, GROUP BY & Window Functions Mid-Level Intermediate

Detailed Explanation

HAVING filters groups after aggregation, while WHERE filters individual rows before grouping.

HAVING is therefore used when the condition depends on an aggregate result, such as categories containing more than 10 products or customers whose total purchases exceed a specified amount.

Code Example

SELECT CategoryId,
       COUNT(*) AS ProductCount
FROM dbo.Products
GROUP BY CategoryId
HAVING COUNT(*) > 10;