The WHERE
clause is used to restrict records, and is also used by the
query optimizer to determine which indexes and tables to use. HAVING
is a "filter" on the final resultset, and is applied after
GROUP BY
, so sql cannot use it to optimize the query.
WHERE
is applied for each row while extracting. HAVING
extracts all rows then filter the result.
Thus WHERE
cannot be used for aggregate functions, because they require the full rowset to be extracted.