Part 54: Filtering Aggregations: HAVING vs WHERE

Part 54 Technology Updated September 12, 2026

Part 54: Filtering Aggregations: HAVING vs WHERE

One of the most critical distinctions in SQL is when to use WHERE versus HAVING.


1. WHERE vs HAVING

  • WHERE: Filters individual rows BEFORE grouping and aggregation.
  • HAVING: Filters summarized groups AFTER grouping and aggregation.
SELECT department, COUNT(*) AS head_count, AVG(salary) AS avg_sal
FROM employees
WHERE is_active = TRUE         -- 1. Filters rows first
GROUP BY department           -- 2. Groups remaining rows
HAVING COUNT(*) > 5;           -- 3. Filters groups with > 5 active employees

Key Takeaways

  • Use WHERE to filter individual rows.
  • Use HAVING to filter aggregated group metrics.

In Part 55, we explore Multi-Column Grouping and Aggregation Hierarchies!