Reviewed by Aditya Kumar · Last reviewed 2026-03-24
**WHERE**: Filters rows before GROUP BY; cannot use aggregates. **HAVING**: Filters after GROUP BY; can use aggregates. Example: SELECT dept, AVG(salary) FROM employees WHERE hire_date > '2020' GROUP BY dept HAVING AVG(salary) > 50K. WHERE reduces input; HAVING filters groups....
WHERE: Filters rows before GROUP BY; cannot use aggregates. HAVING: Filters after GROUP BY; can use aggregates. Example: SELECT dept, AVG(salary) FROM employees WHERE hire_date > '2020' GROUP BY dept HAVING AVG(salary) > 50K. WHERE reduces input; HAVING filters groups. Performance: Filter in WHERE when possible—fewer rows to aggregate.
Red Flag: HAVING with non-aggregate condition—redundant. Pro-Move: 'We moved department IN (...) from HAVING to WHERE—cut aggregation input by 60%, query 3× faster.'
According to DataEngPrep.tech, this is one of the most frequently asked SQL interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.