Overview
The ORDER BY
statement allows you to sort the current working table based on a list of specifications.
<order by statement> ::=
"ORDER BY" <sort specification> [ { "," <sort specification> }... ]
<sort specification> ::=
<value expression> [ <ordering specification> ] [ <null ordering> ]
<ordering specification> ::= "ASC" | "DESC"
<null ordering> ::= "NULLS FIRST" | "NULLS LAST"
Details
- When no ordering specification is provided,
ASC
(ascending) is applied by default. To reverse the order, you can explicitly use theDESC
(descending) keyword. NULLS FIRST
andNULLS LAST
can be used to control whethernull
values appear before or after non-null values when ordering the result. When null ordering is not explicitly specified:NULLS LAST
is applied by default when effectively ordering inASC
order.NULLS FIRST
is applied by default when effectively ordering inDESC
order.
Example Graph
The following examples run against this graph:
To create this graph, run the following query against an empty graph:
INSERT (p1:Paper {_id:'P1', title:'Efficient Graph Search', score:6, author:'Alex', publisher:'PulsePress'}),
(p2:Paper {_id:'P2', title:'Optimizing Queries', score:9, author:'Alex'}),
(p3:Paper {_id:'P3', title:'Path Patterns', score:7, author:'Zack', publisher:'BrightLeaf'}),
(p1)-[:Cites {weight:2}]->(p2),
(p2)-[:Cites {weight:1}]->(p3)
Ordering by Property
The table can be sorted by the values of a specified property.
MATCH (n:Paper)
ORDER BY n.score
RETURN n.title, n.score
Result:
n.title | n.score |
---|---|
Efficient Graph Search | 6 |
Path Patterns | 7 |
Optimizing Queries | 9 |
Ordering by Element Variable
When an element variable is specified, the table is sorted on the _uuid
of the nodes or edges.
MATCH (n:Paper)
RETURN n.title, element_id(n) ORDER BY n
Result:
n.title | element_id(n) |
---|---|
Optimizing Queries | 8718971077612535810 |
Efficient Graph Search | 8791028671650463745 |
Path Patterns | 12033620403357220867 |
Ordering by Expression
The table can be sorted by the results produced by an expression.
MATCH p = (:Paper)->{1,2}(:Paper)
RETURN p, path_length(p) AS length ORDER BY length DESC
Result:
p | length |
---|---|
(:Paper {_id: "P1", score: 6, title: "Efficient Graph Search", author: "Alex", publisher: "PulsePress"})-[:Cites {weight: 2}]->(:Paper {_id: "P2", score: 9, title: "Optimizing Queries", author: "Alex", publisher: null})-[:Cites {weight: 1]->(:Paper {_id: "P3", score: 7, title: "Path Patterns", author: "Zack", publisher: "BrightLeaf"}) | 2 |
(:Paper {_id: "P1", score: 6, title: "Efficient Graph Search", author: "Alex", publisher: "PulsePress"})-[:Cites {weight: 2]->(:Paper {_id: "P2", score: 9, title: "Optimizing Queries", author: "Alex", publisher: null}) | 1 |
(:Paper {_id: "P2", score: 9, title: "Optimizing Queries", author: "Alex", publisher: null})-[:Cites {weight: 1]->(:Paper {_id: "P3", score: 7, title: "Path Patterns", author: "Zack", publisher: "BrightLeaf"}) | 1 |
Multi-level Ordering
When there are multiple specifications, the table is sorted by the first specification listed, and for equals values, go to the next specification, and so on.
MATCH (n:Paper)
RETURN n.title, n.author, n.score
ORDER BY n.author DESC, n.score
Result:
n.title | n.author | n.score |
---|---|---|
Path Patterns | Zack | 7 |
Efficient Graph Search | Alex | 6 |
Optimizing Queries | Alex | 9 |
Discarding and Retaining Records After Ordering
You may use the SKIP
and LIMIT
statements in combination with the ORDER BY
statement to skip a specified number of records from the beginning of the table and to limit the number of records retained.
This query returns the titles of the two papers with the second and third highest scores:
MATCH (n:Paper)
RETURN n.title, n.score
ORDER BY n.score DESC SKIP 1 LIMIT 2
Result:
n.title | n.score |
---|---|
Path Patterns | 7 |
Efficient Graph Search | 6 |
Null Ordering
NULLS FIRST
and NULLS LAST
can be used to control whether null
values appear before or after non-null values when ordering the result.
This query returns the titles of the two papers with the second and third highest scores:
MATCH (n:Paper)
RETURN n.title, n.publisher
ORDER BY n.publisher NULLS FIRST
Result:
n.title | n.score |
---|---|
Optimizing Queries | null |
Path Patterns | BrightLeaf |
Efficient Graph Search | PulsePress |