ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

MySQL Relational Database Tutorial• 6 min read

Aggregations, GROUP BY & The HAVING Clause

Aggregate data across business categories using COUNT, SUM, AVG, and master filtering aggregated rows with HAVING.

WHERE vs. HAVING: The Critical Difference

WHERE: Filters individual raw rows before any grouping or aggregation takes place.
HAVING: Filters grouped summary rows after the aggregation function has computed.
Filtering Groups with HAVING
SELECT 
    Category,
    COUNT(*) AS ProductCount,
    AVG(Price) AS AvgPrice
FROM Products
WHERE InStock = 1                -- Filters raw rows
GROUP BY Category
HAVING COUNT(*) >= 5             -- Filters aggregated groups
   AND AVG(Price) < 100.00;
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Why can you not use an aggregate function like SUM(Total) > 1000 inside a standard WHERE clause?