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

  • What is the difference In Count() And Count(fieldname) In Soql.?

    Posted by Kirandeep on April 17, 2020 at 4:09 pm

    What is the difference In Count() And Count(fieldname) In Soql.?

    krati replied 4 years ago 2 Members · 1 Reply
  • 1 Reply
  • krati

    Member
    April 17, 2020 at 4:19 pm

    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.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos