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) >= 1
The following screenshot shows us the output of the preceding query execution:
The output of the SOQL execution shows us the number of employees in each and every city whose number of records in each and every city is greater than one...