In Salesforce, retrieving data in the correct order is very important for reports, UI display, and business logic. This is where the ORDER BY clause in SOQL plays a key role.
As a Salesforce Developer, you want to retrieve records in a particular order to analyze them using SOQL in Apex. For this, we have different clauses that provide functionality to fetch records in various patterns, as required.
In this article, we will learn everything about the ORDER BY Clause in SOQL (Salesforce Object Query Language).
We will cover syntax, examples, real-time use cases, best practices, and functionality of the ORDER BY clause, and how to use it in SOQL queries with different examples.
What is ORDER BY in SOQL?
The ORDER BY clause in SOQL is used to sort the query results. It can be sorted in ascending (ASC) or descending (DESC) order and can use multiple fields to sort the result.
This clause is useful for ranking or organizing data based on specific fields. Using ORDER BY in SOQL, we can sort records based on fields such as date, name, or other relevant criteria.
It helps you:
- Sort records in ascending order
- Sort records in descending order
- Display data in a meaningful way
Syntax: Declaring ORDER BY Clause in SOQL
SELECT field1, field2, ... FROM ObjectName WHERE condition ORDER BY field [ ASC/DESC ] [ NULLS FIRST/NULLS LAST ]- SELECT: The SELECT clause specifies the fields to retrieve in the query results.
- FROM: The FROM clause defines the Sobject from which to retrieve data.
- WHERE (Optional): The WHERE clause filters the query results based on specific conditions, as you specify.
- ORDER BY: Using the ORDER BY clause, we can sort results in ascending or descending order or in null-first or null-last order.
Example 1: Display Result in Ascending Order Using SOQL
When we want to display results in ascending order, we don’t need to add the ASC keyword; the results are displayed in ascending order by default.
Here is the SOQL query to display account records in ascending order.
SELECT Name, Industry FROM Account ORDER BY NameAs you execute the above query, even though I didn’t add the ASC keyword, the results are displayed in ascending order.

Example 2: Display Result in Descending Order Using SOQL
In the query below, I want to display account record results in descending order by name. So here, we need to use the DESC keyword.
SELECT Name, Industry FROM Account ORDER BY Name DESCWhen I execute the above query, the results show that these time records are displayed in descending order.
If you add the LIMIT clause in the query, Apex will display only specific records in the result based on the number you provided.

Example 3: Use Multiple Fields in ORDER BY Clause in SOQL
Now, let’s understand how to use multiple fields in the ORDER BY clause in SOQL and see how records are sorted when multiple fields are used.
We can use multiple fields in the ORDER BY clause to sort results based on multiple criteria. When we include multiple fields, the results will be sorted by the first field, then by the second, and so on.
For example, if you want to fetch the account records in such an order, the rating should be in ascending order, and the industry field should be in descending order.
Therefore, we need to use an ORDER BY clause with the rating field set to ASC and the industry field set to DESC.
In the SOQL query below, we have used multiple fields in the order by clause.
SELECT Name, Rating, Industry FROM Account ORDER BY Rating ASC, Industry DESCAs I execute the above query, you can see the result: the first rating field is sorted in ascending order, and within that field, the industry field is sorted in descending order. If we have more fields, it will go on.
After this, the industry values for the other set of rating values will be sorted in descending order. In this way, the multiple field words are sorted by clause in SOQL.

Example 4: Display Null Values First in SOQL Result
To display the null values first in the SOQL result in the ORDER BY clause, we need to add NULL FIRST so that the records that don’t contain a value for any particular field will display first.
For example, when you want to display contact records that are not associated with any account, we can use this SOQL query to retrieve those records.
SELECT FirstName, LastName, Account.Name FROM Contact ORDER BY Account.Name NULLS FIRST
Example 5: Display Null Values Last in SOQL Result
To display null values last in the SOQL result using the ORDER BY clause, we need to add ‘NULL Last’, so that records without a value for any particular field will display last.
Let’s take another example of a lead object. We only want to display the lead records whose lead source values are not null, and if the lead source field value is null, those records display last in the search result.
SELECT Name, LeadSource, Status FROM Lead ORDER BY LeadSource DESC NULLS LASTAfter executing the ORDER BY NULL LAST SOQL query, you can see the result: the null values of the lead source field are displayed last in the result list.

Example 6: Display the Latest Created Record Using the ORDER BY Clause in SOQL
When you need to display the newly created record using SOQL in Apex, you can use an ORDER BY DESC clause to retrieve the data.
If you want to retrieve limited records, you can use the LIMIT clause and provide a number so that only that number of records will display in the query result.
SELECT Id, Name, CreatedDate FROM Account ORDER BY CreatedDate DESC LIMIT 5
Example 7: Use the SOQL Query ORDER BY Clause in Apex
In the Apex program below, I explain how to use an ORDER BY clause in an SOQL query to fetch records from the Salesforce database in a specific order.
For example, we want to display account records without a rating value and display annual revenue in descending order. To achieve this, let’s create an Apex code to display the records.
public class ListCollection {
public void SOQL() {
List<Account> accList = [
SELECT Id, Name, Rating, AnnualRevenue
FROM Account
ORDER BY Rating DESC NULLS LAST, AnnualRevenue DESC ];
for (Account acc : accList) {
System.debug ('Account Name : ' + acc.Name +
', Ratings : ' + acc.Rating +
', Revenue: ' + acc.AnnualRevenue);
}
}
}As you execute the above Aex code, you will see the rating field values displayed in descending order with the annual revenue field.

Frequently Asked Questions
Q1: What is the ORDER BY clause in SOQL?
Used to sort records
Q2: Can we sort by multiple fields?
Yes
Q3: What is default sorting?
Ascending
Q4: Can ORDER BY affect performance?
Yes, if used on large data
Conclusion
The ORDER BY clause in SOQL is a powerful feature that helps you organize and display data effectively.
By using ascending and descending sorting, multiple fields, and combining with LIMIT and WHERE, you can build efficient and optimized queries.
I hope you have an idea about the ORDER BY Clause in SOQL (Salesforce Object Query Language). I have explained the syntax and the different SOQL queries used in the ORDER BY clause, with examples and explanations.
You may like to read:
- Salesforce Object Query Language (SOQL) in Apex
- GROUP BY Date Clause in SOQL
- GROUP BY Clause in SOQL
- DATE Clause in SOQL
- LIKE Clause in SOQL
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.