Newer Version Available
COUNT() and COUNT(fieldName)
Use one of the following forms of syntax for COUNT():
COUNT()
COUNT() returns the number of rows that match the filtering conditions.
For example:
For COUNT(), the size field of the QueryResult object returns the number of rows retrieved in the query. The records field returns null.
Note the following when using COUNT():
- COUNT() must be the only element in the SELECT list.
- The number of rows returned by COUNT() includes null values that match the filtering conditions of the query.
- You can use COUNT() with a LIMIT clause.
- You can't use COUNT() with an ORDER BY clause. Use COUNT(fieldName) instead.
- You can't use COUNT() with a GROUP BY clause for API version 19.0 and later. Use COUNT(fieldName) instead.
COUNT(fieldName)
COUNT(fieldName) returns the number of rows that match the filtering conditions and have a non-null value for fieldName.
For example:
For COUNT(fieldName), the AggregateResult object in the records field returns the number of rows. The size field does not reflect the count. For example:
The count is returned in expr0, which is the implied alias for the aggregated field, of the AggregateResult object. For more information, see Use Aliases with GROUP BY.
There are advantages to using COUNT(fieldName) instead of COUNT(). You can include multiple COUNT(fieldName) items in a SELECT clause. For example, the following query returns the number of opportunities, and the number of opportunities associated with a campaign.
You can use GROUP BY clause with COUNT(fieldName) to analyze your records and return summary reporting information. For example, the following query returns the number of leads for each LeadSource value: