HAVING:
The HAVING clause used with a GROUP BY clause to filter the results returned by aggregate functions, such as SUM().
A HAVING clause is similar to a WHERE clause. The difference is that you can include aggregate functions in a HAVING clause, but not in a WHERE clause.
Syntax:
SELECT fieldList or aggregate functions FROM objectType [GROUP BY fieldlist] [HAVING havingConditionExpression]
Example:
We are interested in LeadSource values that have generated more than 10 leads, you can filter the results by using a HAVING clause.
SELECT LeadSource, COUNT(Name) FROM Lead GROUP BY LeadSource HAVING COUNT(Name) > 10
Before Using HAVING gothroug this points:
1. A HAVING clause can filter by aggregated values. It can also contain filter by any fields included in the GROUP BY clause.
2. Similar to a WHERE clause, a HAVING clause supports all the comparison operators, such as =, in conditional expressions, which can contain multiple conditions using the logical AND, OR, and NOT operators.
3. A HAVING clause can't contain any semi- or anti-joins.