Question

SELECT SUM(salary) 
FROM EMPLOYEE 
WHERE SUM(salary) > 10000;

causes error, I know that it needs to be put in HAVING clause.

My question is why? As in root cause for this syntax choice in SQL language design.

Was it helpful?

Solution

Where clause filters rows in the query (Before they are aggregated).

Having clause filters rows after they have been aggregated.

i.e. Sum, Avg, Count, Max, Min, or any aggregate functions are always in a HAVING filter.

'Row based filtering' is performed in the where clause.

This is generally covered in standard SQL basics so you may want to read some background as this could help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top