• Difference Between Group By & Order By Clause in SOQL Salesforce

    Published By: Venu Gutta
    Published on: Saturday 1 April 2017
    A- A+
    ORDER BY CLAUSE: The ORDER BY clause is used to sort the query result by specific fields by ascending or descending order. By default it returns query result in  ascending order.  If we want Query result in Descending order then we need to write "Order by FieldName DESC ".

    example: if we want to get latest Ten records created on Account, then query will be like below

    select NAME from ACCOUNT where createddate=today order by createddate desc limit 10

    above soql returns First Ten account records which was created  today.
    o/p:


    GROUP BY Clause:  The SOQL GROUP BY clause can be used for collecting data across multiple records and group the results by one or more columns.

    Group By Clause examples: 
    for example  i want to find out maximum "Annual Revenue" from Account records whose rating is "HOT".

    Query: SELECT rating, maX(annualRevenue) from account where rating='hot' group by rating

    for suppose if we want to find out Industry wise Maximum "Annual revenue" from account record whose rating is hot. Then query will be like below

    SELECT rating,industry, Max(annualrevenue) from account where rating='hot' group by rating, industry

    I want to find out how many Banking Industry Account records having no phone number

    SELECT industry, count(phone) from account where industry='Banking' group by industry

  • No Comment to " Difference Between Group By & Order By Clause in SOQL Salesforce "