Example:
SELECT Name,MAX(NumberofLocations__c) FROM Account GROUP BY name ORDER BY Name LIMIT 2
Field that are aggregated cannot be used in GROUP BY or ORDER BY.
In the above example, NumberOfLocations__c cannot be used in GROUP BY or ORDER BY because NumberOfLocations__c is aggregated.