SOQL LIMIT

The LIMIT clause is used to limit the number of rows returned by a SOQL query. The maximum number of records that should be fetched is specified in the Limit clause. The LIMIT clause is optional, but it is highly recommended to use

Example #1: Find the first 50 clinics in Florida ordered by their name.

SELECT Id, Name
FROM Clinic__c
ORDER BY Name
LIMIT 50

You can also apply a limit on SOQL queries containing group by clause. In that case only the first few rows of aggregated records are returned.

Example #2: Find the top 10 clinics with maximum revenue from booking amount.

SELECT Sum(Booking_Amount__c), clinic__c
FROM Appointment__c
GROUP BY Clinic__c
ORDER BY Sum(Booking_Amount__c) DESC
LIMIT 10