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

  • 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