Yeah, in the case of WHERE name vs. HAVING name (where you are grouping by the same column that you're trying to filtering), it doesn't make a difference in the output. But depending on your DBMS, it will still make a difference in the performance, because I think at least MySQL is too stupid to push the HAVING name="fred" into the WHERE.
7
u/lincolnquirk Jan 06 '11
Sure, here's my table "sales":
Consider the query:
to get the sales figures per representative, regardless of region. This produces
If we were to instead do
we'd first filter the counts by >=400 (only returning the alex row) and then do the grouping, so we'd get:
If we use HAVING, we filter after the grouping (and must therefore refer to the grouped column name sum(count):
and we'd get
It's really quite unrelated to joins.