Grouping and Filtering Aggregates
SQL GROUP BY with HAVING Explained
Why HAVING is different from WHERE
By Sharanmeet Singh••
Tags:SQLGROUP BYHAVINGAggregation
SQL Query Generator
Generate SQL queries for database operations
SQL GROUP BY with HAVING
GROUP BY
lets you aggregate rows, and HAVING
filters groups.
Example
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
- Groups employees by department
- Returns only departments with more than 5 employees
Difference Between WHERE and HAVING
- WHERE → Filters rows before grouping
- HAVING → Filters groups after grouping
Tip: Always use WHERE for row-level filtering and HAVING for aggregate filtering.