SOQL statements that summarize or group records are called aggregate queries. Aggregate queries in SOQL run at the database level rather than in Apex. This results in much better performance and simpler code. This section covers three aspects of aggregate SOQL queries:
Aggregate functions—Rather than simply returning the discrete values of a database field in a
SELECT statement, aggregate functions such as
SUM apply a simple calculation on each record and return the accumulated result.
GROUP BY syntax ...