Newer Version Available

This content describes an older version of this product. View Latest

ORDER BY

Use the optional ORDER BY in a SELECT statement of a SOQL query to control the order of the query results, such as alphabetically beginning with z. If records are null, you can use ORDER BY to display the empty records first or last.

You can use ORDER BY in a SELECT statement to control the order of the query results. The syntax is:

1[ORDER BY fieldOrderByList {ASC|DESC} [NULLS {FIRST|LAST}] ]

There’s no guarantee of the order of results unless you use an ORDER BY clause in a query.

And even if you use ORDER BY, the order of results can vary if there are duplicate values for the fields you’re using in the ORDER BY clause. For example, if there a multiple Account records with the same Industry, the order of the results for this query can vary:

1SELECT Name, Industry FROM Account ORDER BY Industry

To avoid this issue, add the Id (or any other field that is unique in the results) to the ORDER BY clause. For example:

1SELECT Name, Industry FROM Account ORDER BY Industry, Id

Note

Syntax Description
ASC or DESC Specifies whether the results are ordered in ascending (ASC) or descending (DESC) order. Default order is ascending.
NULLS FIRST or NULLS LAST Orders null records at the beginning (NULLS FIRST) or end (NULLS LAST) of the results. By default, null values are sorted first.

For example, the following query returns a query result with Account records in alphabetical order by first name, sorted in descending order, with accounts that have null names appearing last:

1SELECT Name
2FROM Account
3ORDER BY Name DESC NULLS LAST

The following factors affect results returned with ORDER BY:

  • ORDER BY is compatible with relationship query syntax.
  • Multiple column sorting is supported, by listing more than one fieldExpression clause.
  • Relationship queries with foreign key values in an ORDER BY clause behave differently depending on the version of the Lightning Platform API. In an ORDER BY clause, if the foreign key value in a record is null, the record is returned in version 13.0 and later, but not returned in versions before 13.0.
    1SELECT Id, CaseNumber, Account.Id, Account.Name
    2FROM Case
    3ORDER BY Account.Name
    Any case record for which AccountId is empty is returned in version 13.0 and later.
  • The sort order depends on your user locale setting. For English locales, SOQL uses the UTF-8 values of the uppercase character to create a sort order. In other words, sorting for English locales is case insensitive.

    For non-English locales, SOQL uses a pre-defined order that is natural for the locale specified. This is because different cultures may sort the same characters in different ways, and this nuance cannot be captured by the UTF-8 values alone.

The following limitations apply to data types when using ORDER BY:

  • These data types are not supported: multi-select picklist, rich text area, long text area, encrypted (if enabled), and data category group reference (if Salesforce Knowledge is enabled).
  • All other data types are supported, with the following caveats:
    • Corporate currency always sorts using corporate currency value, if available.
    • phone data does not include any special formatting when sorting, for example, non-numeric characters such as dash or parentheses are included in the sorting.
    • picklist sorting is defined by the picklist sort determined during setup.
External objects have the following limitations for the ORDER BY clause.
  • The following limits apply only to the OData 2.0 and 4.0 adapters for Salesforce Connect.
    • NULLS FIRST and NULLS LAST are ignored.
    • External objects don’t support the ORDER BY clause in relationship queries.
  • The following limits apply only to custom adapters for Salesforce Connect.
    • NULLS FIRST and NULLS LAST are ignored.

You can use ORDER BY with the optional LIMIT qualifier, in a SELECT statement:

1SELECT Name
2FROM Account
3WHERE industry = 'media'
4ORDER BY BillingPostalCode ASC NULLS LAST LIMIT 125