ORDER BY
Order by specifies that the output be sorted and how.
You can order by multiple properties by stating each variable in the ORDER BY clause.
Each property may specify its sort order with ASC/ASCENDING or DESC/DESCENDING. If no order is specified, it defaults to ascending.
The result will be sorted by the first variable listed.
For equal values, it will go to the next property in the ORDER BY clause, and so on.
ORDER BY <alias.property [ASC/DESC] list>
Below we sort our friends by height. For equal heights, weight is used to break ties.
ORDER BY friend.height, friend.weight DESC
Frequently Asked Questions 3
What is the default sort order in ORDER BY?
If no direction is specified, ORDER BY defaults to ascending (ASC) order.
Can I sort by multiple properties?
Yes. List multiple properties separated by commas. The result is sorted by the first property, then ties are broken by subsequent properties in order.
Does ORDER BY work with expressions and aliases?
Yes. You can order by any expression or alias that appears in the RETURN or WITH clause, including computed values and aggregations.