Newer Version Available

This content describes an older version of this product. View Latest

Considerations When Using HAVING

Note the following when you are creating queries with a HAVING clause:

  • 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");SELECT LeadSource, COUNT(Name)
    2FROM Lead
    3GROUP BY LeadSource
    4HAVING 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.