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.)
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.
The difference is when the clause is applied. WHERE is applied much earlier in the creation of the result set and I believe can take advantage of indexes and other optimizations. HAVING is applied much later.
HAVING is usually much slower than using WHERE as the WHERE clause usually is more restrictive earlier on. However, HAVING is very useful when wanting to filter on aggregated or aliased columns as these are created after the application of the WHERE clause.
HAVING specifies a search condition for a group or an aggregate function used in SELECT statement. HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause.
A HAVING clause is like a WHERE clause, but applies only to groups as a whole, whereas the WHERE clause applies to individual rows. A query can contain both a WHERE clause and a HAVING clause. The WHERE clause is applied first to the individual rows in the tables . Only the rows that meet the conditions in the WHERE clause are grouped. The HAVING clause is then applied to the rows in the result set. Only the groups that meet the HAVING conditions appear in the query output. You can apply a HAVING clause only to columns that also appear in the GROUP BY clause or in an aggregate function
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad: