The HAVING clause in SOQL is almost similar to WHERE except that HAVING allows you to filter using functions such as SUM, COUNT, AVG, MIN or MAX.


Find all clinics where the total booking amount in the previous month exceeds 10000 USD.

SELECT Sum(Booking_Amount__c), clinic__c
FROM Appointment__c
WHERE Appointment_time__c = LAST_MONTH
HAVING Sum(Booking_Amount__c) > 10000

Note how the WHERE clause and HAVING clause are used together in the above query.

The WHERE clause applies a filter on the rows to retrieve, and then these rows will be aggregated. The HAVING clause applies a filter on the aggregated rows returned finally.