Using the HAVING clause

The HAVING clause is very similar to the WHERE clause. However, the only difference between the HAVING and WHERE clause is that the HAVING clause is used only with the aggregate functions.

The HAVING clause is used to specify the search condition in the GROUP BY clause or the aggregate functions. The HAVING clause limits the grouped records returned by a SOQL statement. However, the WHERE clause limits the records returned by a SOQL statement.

A HAVING clause in SOQL is used to specify that the SOQL SELECT statement should only return the records whose aggregate values meet the specified conditions.

A sample query is given as follows:

SELECT City__c, COUNT(Employee_Name__c) FROM Employee__c GROUP BY City__c HAVING COUNT(City__c) ...

Get Getting Started with SOQL now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.