The WHERE clause can be used even if a HAVING is being used. They mean very different things. The way to think about it is as follows:
- The WHERE clause acts as a filter at the record level
- Anything that gets through is then put into groups specified by your GROUP BY
- Then, the HAVING clause filters out groups, based on aggregate (SUM, COUNT,
MIN, etc.) condition
So, if I have a table : ( STORE_ID, STATE_CODE, SALES)
Select STATE, SUM(SALES)
from MyTable
Where SALES > 100
Group By STATE
Having Sum(Sales) > 1000
This will first filter to read only the Store records with Sales over 100. For each Group (by State) it will sum the Sales of only those stores with Sales of 100 or more. Then, it will drop any State unless the State-level summation is more than 1000. [Note: The state summation excludes any store of sales 100 or less.]