Order By
Order by is the clause where you define the order in which you want the query results to be displayed
or sort a set of tuples you computed up to a point in your query. Kùzu currently supports two
sorting orders: ASC
, DESC
. By default if no sorting order is specified, Kùzu sorts
rows in ascending order and NULLs are placed first.
We will use the example database for demonstration, whose schema and data import commands are given here.
Basic example
The following query returns the user’s name and age ordered by user’s age using the default ordering(asc order):
Output:
Similarly, the following returns the users’ names who lives in Waterloo ordered by user’s age, but in descending order.
Query:
Output:
Ordering using multiple properties or expressions
You can also order using multiple properties or expressions that are in scope in your query.
The meaning is that the first expression forms the primary order, then ties are broken
according to the second order, then further ties are broken according third order, etc.
Each order can be ascending or descending independent of the others.
For example, the following query sorts the results of 1-hop (a:User)-[:Follows]->(b:User)
queries first by b.age
and then by a.name
both in descending order.
Output:
Noe that there is tie for b.age in first two rows and the order is
decided based on a.name (in descending order). If you removed the
last DESC
in the above query, the first two tuples in the result
would be swapped.