Reviewed by Aditya Kumar · Last reviewed 2026-03-24
HAVING filters **groups** after aggregation; WHERE filters **rows** before aggregation. **Why order matters**: HAVING operates on aggregated values (SUM, COUNT, AVG) that don't exist until after GROUP BY. Example: SELECT dept, AVG(sal) FROM emp GROUP BY dept HAVING AVG(sal) >...
HAVING filters groups after aggregation; WHERE filters rows before aggregation. Why order matters: HAVING operates on aggregated values (SUM, COUNT, AVG) that don't exist until after GROUP BY. Example: SELECT dept, AVG(sal) FROM emp GROUP BY dept HAVING AVG(sal) > 50000. You can't use WHERE AVG(sal) > 50000 because aggregates aren't computed yet. Logical order: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY. Scalability: Filter raw rows with WHERE when possible—more efficient (fewer rows before aggregate). Use HAVING only for aggregate conditions.
Red Flag: Using HAVING for row-level filters—wastes compute. Pro-Move: 'WHERE filters early; HAVING only for aggregate thresholds.'
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.