13. Filtering Groups with HAVING
Core Idea: HAVING filters grouped rows after aggregation occurs. In contrast, WHERE filters individual base table records before any grouping is performed.
Syntax Shape
Multiple Conditions in HAVING
Practical Scenarios
Identifying high-volume product categories, locating departments exceeding headcount thresholds, or detecting repeating duplicate email entries.
Advanced Deep Dive ADVANCED
HAVING vs WHERE Execution Order: WHERE filters row-by-row before aggregations are computed. HAVING evaluates against the synthesized summary rows.
Performance Best Practice: Filter non-aggregated criteria in WHERE to minimize row volume before the costly grouping and aggregation steps.
Aggregate Functions: HAVING conditions can reference any aggregate function (COUNT, SUM, MAX, MIN, AVG), even ones not present in the SELECT list.
Null Grouping Logic: Summary groups consisting of NULL values will be included unless filtered explicitly with HAVING Category IS NOT NULL.