Newer Version Available
Considerations When Using HAVING
When you’re creating SOQL queries with a HAVING
clause, there are some considerations to keep in mind.
- A HAVING clause can filter by aggregated values. It can
also contain filter by any fields included in the GROUP
BY clause. To filter by any other field values, add the filtering condition to
the WHERE clause. For example, the following query is
valid:
1swfobject.registerObject("clippy.codeblock-0", "9"); 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17SELECT LeadSource, COUNT(Name) 18FROM Lead 19GROUP BY LeadSource 20HAVING COUNT(Name) > 100 and LeadSource > 'Phone'The following query is invalid as City is not included in the GROUP BY clause:
1SELECT LeadSource, COUNT(Name) 2FROM Lead 3GROUP BY LeadSource 4HAVING COUNT(Name) > 100 and City LIKE 'San%' - 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.
- A HAVING clause can't contain any semi- or anti-joins. A semi-join is a subquery on another object in an IN clause to restrict the records returned. An anti-join is a subquery on another object in a NOT IN clause to restrict the records returned.