posted 18 years ago
I'll give this a try.
Short answer:
Use HAVING instead of WHERE when you want to establish a condition that involves a grouping (aggregating) function. (Grouping functions are things like count(), max(), sum(), etc.)
Longer answer:
Either WHERE or HAVING clauses allow you to restrict the data you're selecting by establishing conditions.
(Usually people say stuff like "WHERE allows you to qualify on the original data in the tables and HAVING allows you to qualify on groups," which is indeed how you typically use them, but that's not 100% accurate.)
Conditions in WHERE clauses cannot contain grouping functions.
The conditions you establish in a HAVING clause *must* either (1) contain a grouping function, or (2) if the HAVING clause specifies table fields, those table fields must be in a GROUP BY clause.