You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: modules/ROOT/pages/clauses/order-by.adoc
+52-14Lines changed: 52 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,6 @@
1
-
:description: Information about Cypher's `ORDER BY` subclause.
2
-
3
-
[[query-order]]
4
1
= ORDER BY
2
+
:description: Information about Cypher's `ORDER BY` subclause.
3
+
:table-caption!:
5
4
6
5
`ORDER BY` is a subclause that determines how the results of a xref:clauses/return.adoc[`RETURN`] or xref:clauses/with.adoc[`WITH`] clause are ordered.
7
6
As of Neo4j 5.24, it can also be used as a standalone clause, either on its own or in combination with `SKIP`/`OFFSET` or `LIMIT`.
`ORDER BY` can be used to sort the result by property values.
57
59
@@ -82,8 +84,10 @@ The nodes are returned, sorted by the value of the `total` properties in an asce
82
84
2+d|Rows: 5
83
85
|===
84
86
85
-
[[order-by-multiple-properties]]
86
-
== Order by multiple property values
87
+
=====
88
+
89
+
.Order by multiple property values
90
+
=====
87
91
88
92
Order by multiple property values by listing two or more properties in the `ORDER BY` subclause.
89
93
Cypher sorts by the first property, and if values are equal, it moves to the next property, and so on.
@@ -116,9 +120,10 @@ This returns the nodes, sorted first by their `total` property, and then, for eq
116
120
3+d|Rows: 5
117
121
|===
118
122
123
+
=====
119
124
120
-
[[order-by-id]]
121
-
== Order by ID
125
+
.Order by ID
126
+
=====
122
127
123
128
`ORDER BY` can be used to sort nodes or relationships by their ID (retrieved by either the xref:functions/scalar.adoc#functions-elementid[`elementId()`] or xref:functions/scalar.adoc#functions-id[`id()`] functions).
124
129
@@ -150,8 +155,10 @@ Neo4j reuses its internal IDs when nodes and relationships are deleted.
150
155
Applications relying on internal Neo4j IDs are, as a result, brittle and can be inaccurate.
151
156
It is recommended to use application-generated IDs instead.
152
157
153
-
[[order-by-expression]]
154
-
== Order by expressions
158
+
=====
159
+
160
+
.Order by expressions
161
+
=====
155
162
156
163
`ORDER BY` can be used to sort according to the results of an xref:expressions/index.adoc[expression].
157
164
The below query calculates a 10% discount on each order's `total` property value, and then orders the results by the discounted total.
@@ -179,7 +186,7 @@ RETURN o.id AS order,
179
186
2+d|Rows: 5
180
187
|===
181
188
182
-
This next example xref:subqueries/count.adoc[counts] the number of items contained in each order and then orders the results by the item count.
189
+
This next query xref:subqueries/count.adoc[counts] the number of items contained in each order and then orders the results by the item count.
183
190
184
191
.Order by an expression result
185
192
[source, cypher]
@@ -204,6 +211,36 @@ RETURN o.id AS order,
204
211
2+d|Rows: 5
205
212
|===
206
213
214
+
=====
215
+
216
+
[[order-by-values-not-in-result]]
217
+
== Order by values not in the result
218
+
219
+
`ORDER BY` can sort by values that are not included in the result set.
220
+
That is, the sort key does not need to be part of the preceding `RETURN` or `WITH` clause.
221
+
For example, the query below sorts orders based on how many items they contain, even though that count is not returned.
`ORDER BY` can be used to sort results before continuing with additional pattern matching.
267
304
In the example below, it is combined with the xref:clauses/limit.adoc[`LIMIT`] subclause to first sort `Order` nodes by their `orderDate` property values, limit the result to the most recent `Order`, and then match any connected `Item` nodes.
305
+
Also note that `ORDER BY` and `LIMIT` are used as xref:clauses/order-by.adoc#order-standalone-clause[standalone clauses] and not as subclauses in this example.
268
306
269
307
.Find the items contained in the most recently placed order
270
308
[source, cypher]
271
309
----
272
310
MATCH (o:Order)
273
-
ORDER BY o.orderDate DESC
274
-
LIMIT 1
311
+
ORDER BY o.orderDate DESC
312
+
LIMIT 1
275
313
MATCH (o)-[:CONTAINS]->(i:Item)
276
314
RETURN o.id AS order,
277
315
o.total,
@@ -321,7 +359,7 @@ RETURN o.id AS order,
321
359
== ORDER BY and the WITH clause
322
360
323
361
When `ORDER BY` is present on a `WITH` clause, the immediately following clause will receive records in the specified order.
324
-
The ordering guarantee can be useful to exploit by operations which depend on the order in which they consume values.
362
+
This guaranteed order is useful for operations that rely on the sequence in which values are processed.
325
363
For example, appending `ORDER BY` to a `WITH` clause can be used to control the order of items in the list produced by the xref:functions/aggregating.adoc#functions-collect[`collect()`] aggregating function.
326
364
The xref:clauses/merge.adoc[`MERGE`] and xref:clauses/set.adoc[`SET`] clauses also have ordering dependencies which can be controlled this way.
0 commit comments