Activity Forums Salesforce® Discussions What is the difference between Count() And Count(fieldname) In Salesforce SOQL?

  • shariq

    Member
    September 19, 2018 at 7:40 am

    Hi,

    COUNT() :

    COUNT() must be the only element in the SELECT list.

    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.

    Thanks

  • Anjali

    Member
    September 19, 2018 at 7:47 am

    Hi Anurag,

    COUNT(): It is an optional clause that can be used in a SELECT statement in a SOQL query to discover the number of rows that a query returns.This function returns the number of rows that match the filtering conditions and COUNT() must be the only element in the select list. The resulting query result size field which returns the number of rows and the records will return null.In simple words, COUNT() returns the number of items in a group, including NULL values and duplicates.

    COUNT(fieldname) :This function returns the number of rows that match the filtering conditions and have a non-null value records. An Aggregate Result object in the records field contains the number of rows. Do not use the size field for the resulting records.

    Again in simple words, COUNT(expression) evaluates an expression for each row in a group and returns the number of non-null values.

  • Parul

    Member
    September 19, 2018 at 9:24 am

    Hi

    COUNT() is an optional clause that can be used in a SELECT statement in a SOQL query to discover the number of rows that a query returns.

    In simple words, COUNT() returns the number of items in a group, including NULL values and duplicates.

    For example:

    SELECT COUNT() FROM Account WHERE Name LIKE 'a%'

    SELECT COUNT() FROM Contact, Contact.Account WHERE Account.Name = 'Tester tube'

    Note the following when using COUNT():

    COUNT() must be the only element in the SELECT list. that is you can not add any other field with count().

    Count(fieldname):

    This function returns the number of rows that match the filtering conditions and have a non-null value records. An Aggregate Result object in the records field contains the number of rows. Do not use the size field for the resulting records. Again in simple words, COUNT(expression) evaluates an expression for each row in a group and returns the number of non-null values.

    So count() includes nulls, the other method doesn't.

    For Example:

    SELECT COUNT(Id) FROM Account WHERE Name LIKE 'xyz%'

    Note: COUNT(Id) in SOQL is equivalent to COUNT(*) in SQL.

     

    Thanks

  • Aman

    Member
    September 29, 2018 at 4:52 pm

    Hi,

    COUNT() is an older SOQL function that was available prior to other aggregate functions. It returns an integer. If you use COUNT(fieldName) the result of the query will be a List instead.

    COUNT() is equivalent to COUNT(*) in SQL. It return the total row count. COUNT(fieldName) only counts the number of non-null records. If you want to use COUNT(fieldName), the following code should work:

    List<AggregateResult> result  = [select count(Id) total from Account];

    System.debug(result[0].get('total'));

    Thanks

Log In to reply.

Popular Salesforce Blogs