---
schema_version: "1"
id: 267fac9cf35d10e580f584090e065618051ffa7785324c86febbb1d9573b053f
title: ORDER BY
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-soql-sosl/guides/soql-sosl/sforce-api-calls-soql-select-orderby.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-soql-sosl/guide/sforce-api-calls-soql-select-orderby.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: a8dc8d3a3db9f9cfc867b32eb8e24203ccb6f40d4ac197b45c1487df18641970
products:
  - SOQL and SOSL Reference
  - Platform Developer Content
---

# ORDER BY

Use the optional `ORDER BY` clause in the `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.

```soql

[ORDER BY fieldOrderByList {ASC|DESC} [NULLS {FIRST|LAST}] ]

```

| 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. |

There’s no guarantee of the order of results unless you use an `ORDER BY` clause in a query.

Even using `ORDER BY`, the order of results can vary if there are duplicate values for the fields that you’re using in the `ORDER BY` clause. For example, if there are multiple Account records with the same Industry, the order of the results for this query can vary.

```soql
SELECT Name, Industry FROM Account ORDER BY Industry
```

To avoid this issue, add the Id, or any other field that’s unique in the results, to the `ORDER BY` clause. For example:

```soql
SELECT Name, Industry FROM Account ORDER BY Industry, Id
```

This example returns the same data in descending order.

```soql
SELECT Name, Industry FROM Account ORDER BY Industry DESC, Id
```

This example 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.

<!-- otherprops: soql -->

```codesegment
SELECT Name
FROM Account
ORDER BY Name DESC NULLS LAST
```

These 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, the record is returned even if the foreign key value in a record is null.

  <!-- otherprops: soql -->

  ```codesegment
  SELECT Id, CaseNumber, Account.Id, Account.Name
  FROM Case
  ORDER BY Account.Name
  ```

  Any case record for which `AccountId` is empty is returned.

- 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, the order is based on the [Default Unicode Collation Element Table](http://userguide.icu-project.org/collation) (DUCET).

These limitations apply to data types when you use `ORDER BY`:

- These data types aren’t 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 these caveats.

  - Corporate currency always sorts by using corporate currency value, if available.
  - `phone` data doesn’t 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.

- If you use ORDER BY to sort on a relationship field which can contain a NULL, the NULLS FIRST and NULLS LAST expressions aren’t supported. For example, if you have a contact record that’s not associated with an account, the Contact.AccountId is NULL. If you issue this SELECT statement, NULL values are still returned at the top: `SELECT Id, Email, AccountId FROM Contact ORDER BY AccountId NULLS LAST`.

You can use `ORDER BY` with the optional LIMIT qualifier in a `SELECT` statement.

<!-- otherprops: soql -->

```codesegment
SELECT Name
FROM Account
WHERE industry = 'media'
ORDER BY BillingPostalCode ASC NULLS LAST LIMIT 125
```
