Skip to content

Commit cba2863

Browse files
update
1 parent 35f2931 commit cba2863

File tree

1 file changed

+52
-14
lines changed

1 file changed

+52
-14
lines changed

modules/ROOT/pages/clauses/order-by.adoc

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
:description: Information about Cypher's `ORDER BY` subclause.
2-
3-
[[query-order]]
41
= ORDER BY
2+
:description: Information about Cypher's `ORDER BY` subclause.
3+
:table-caption!:
54

65
`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.
76
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`.
@@ -51,7 +50,10 @@ CREATE (o1:Order {id: 'ORD-001', orderDate: datetime('2024-05-01T10:00:00'), tot
5150

5251

5352
[[order-by-property]]
54-
== Order by property values
53+
== Basic examples
54+
55+
.Order by property values
56+
=====
5557
5658
`ORDER BY` can be used to sort the result by property values.
5759
@@ -82,8 +84,10 @@ The nodes are returned, sorted by the value of the `total` properties in an asce
8284
2+d|Rows: 5
8385
|===
8486
85-
[[order-by-multiple-properties]]
86-
== Order by multiple property values
87+
=====
88+
89+
.Order by multiple property values
90+
=====
8791
8892
Order by multiple property values by listing two or more properties in the `ORDER BY` subclause.
8993
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
116120
3+d|Rows: 5
117121
|===
118122
123+
=====
119124

120-
[[order-by-id]]
121-
== Order by ID
125+
.Order by ID
126+
=====
122127
123128
`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).
124129
@@ -150,8 +155,10 @@ Neo4j reuses its internal IDs when nodes and relationships are deleted.
150155
Applications relying on internal Neo4j IDs are, as a result, brittle and can be inaccurate.
151156
It is recommended to use application-generated IDs instead.
152157
153-
[[order-by-expression]]
154-
== Order by expressions
158+
=====
159+
160+
.Order by expressions
161+
=====
155162
156163
`ORDER BY` can be used to sort according to the results of an xref:expressions/index.adoc[expression].
157164
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,
179186
2+d|Rows: 5
180187
|===
181188
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.
183190
184191
.Order by an expression result
185192
[source, cypher]
@@ -204,6 +211,36 @@ RETURN o.id AS order,
204211
2+d|Rows: 5
205212
|===
206213
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.
222+
223+
.Order by values not in the returned results
224+
[source, cypher]
225+
----
226+
MATCH (o:Order)
227+
RETURN o.id AS order
228+
ORDER BY COUNT { (o)-[:CONTAINS]->(:Item) }
229+
----
230+
231+
.Result
232+
[role="queryresult",options="header,footer",cols="1*<m"]
233+
|===
234+
| order
235+
236+
| "ORD-002"
237+
| "ORD-004"
238+
| "ORD-001"
239+
| "ORD-003"
240+
| "ORD-005"
241+
242+
1+d|Rows: 5
243+
|===
207244

208245
[[ascending-descending-order]]
209246
== Ascending and descending order
@@ -265,13 +302,14 @@ RETURN o.id AS order,
265302

266303
`ORDER BY` can be used to sort results before continuing with additional pattern matching.
267304
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.
268306

269307
.Find the items contained in the most recently placed order
270308
[source, cypher]
271309
----
272310
MATCH (o:Order)
273-
ORDER BY o.orderDate DESC
274-
LIMIT 1
311+
ORDER BY o.orderDate DESC
312+
LIMIT 1
275313
MATCH (o)-[:CONTAINS]->(i:Item)
276314
RETURN o.id AS order,
277315
o.total,
@@ -321,7 +359,7 @@ RETURN o.id AS order,
321359
== ORDER BY and the WITH clause
322360

323361
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.
325363
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.
326364
The xref:clauses/merge.adoc[`MERGE`] and xref:clauses/set.adoc[`SET`] clauses also have ordering dependencies which can be controlled this way.
327365

0 commit comments

Comments
 (0)