SOQL ORDER BY clause in Salesforce Object Query Language (SOQL) is used to sort the query results either in ascending (ASC) order or descending (DESC). This clause helps to retrieve and aggregate a similar type of data from the Salesforce objects like Account or contact or opportunity
The basic usage of the ORDER BY clause in SOQL only lets you order query results either by ascending (ASC) or descending (DESC), expanding this knowledge You can gain a lot when manipulating how your data is retrieved.
Ordering by Multiple Fields
Multiple field sorting allows you to sort the data in a more exact order with SOQL. When multiple fields are specified, then they are sorted sequentially in order of which the different fields appear.
Syntax
SELECT [fields]
FROM [object]
WHERE [conditions]
ORDER BY [field1] [ASC|DESC], [field2] [ASC|DESC], ...
Example:-
SELECT Id, Name, CreatedDate, Priority
FROM Case
WHERE Status = 'Open'
ORDER BY Priority DESC, CreatedDate ASC
- First, cases are sorted by
Priority
in descending order. - Within each priority level, cases are further sorted by
CreatedDate
in ascending order
Using ORDER BY with LIMIT
Using the LIMIT clause with ORDER BY will return only top records according to sorting. The example is useful when we need to get the most recent or higher priority records.
SELECT Id, Name, CreatedDate
FROM Opportunity
WHERE StageName = 'Prospecting'
ORDER BY CreatedDate DESC
LIMIT 5
This query retrieves the five most recently created Opportunities in the ‘Prospecting’ stage.
How SOQL ORDER BY works ?
ORDER BY ASC
While sorting in ascending order, the query output will be sorted from smallest to largest value. Values are sorted from A — Z of the field it with contains strings, and sortation will be smallest to largest if it consists numbers. If you do not specify the order, it sorts asc ► Sort (Sorting Ascending)
Example:-
SELECT Name FROM Account ORDER BY Name ASC
ORDER BY DESC
Basically descending order arranges the records from largest to smallest value. In the case of string values, it will be in reverse alphabetical order (Z to A) and for numbers they come from largest to smallest.
example:-
SELECT Name FROM Account ORDER BY Name DESC
Sorting Multiple fields
Multiple field sorting is also possible. For instance, if we want to sort records by one field and then further by another field, the ORDER BY clause of SOQL allows us do this in a chained manner.
SELECT Name, Industry FROM Account ORDER BY Industry ASC, Name DESC
In the above example above, results are ordered by Industry ascending. The sort logic will be: If two accounts have the same industry then by Name desc.
Null Handling in SOQL Sorting
NULLs are handled in the sorting process, placing them with either type of ordering at a beginning or end of result set. By default:
1.ASC: Null values come first.
2.DESC—it places null values at the end of a count.
SOQL ORDER BY Clause Realtime example
ORDER BY Clause is used to retrieve the data in “Ascending” or “Descending” order by the condition given in SOQL Statement.
In salesforce ORDER BY clause are two types. They are
- ASC.
- DESC.
Here ASC means ascending and DESC means descending order. In SOQL by default the data will be retrieved in ascending order only.
SOQL ORDER BY Clause ASC
- SELECT name, Amount FROM opportunity ORDER BY name ASC.
Here we have used ORDER BY Clause called ASC. From above SOQL statement name and Amount values will be retrieved from Opportunity and records will be arranged in Ascending order.
SOQL ORDER BY Clause DESC.
- SELECT name, Amount FROM Opportunity ORDER BY name DESC.
Here we have used SOQL ORDER BY clause called “DESC”. From above SOQL statement name and Amount values will be retrieved from opportunity in descending order.
Conclusion
In SOQL, the ORDER BY clause is a great way to organize your query results and conceptually make it easier for logical analyses of data output. Sorting records alphabetical and numeric using ASC, DESC will let you manage how your listing data would viewed. This also allows you to concatenate fields or handle nulls in Salesforce results manipulation.