YMatrix
Quick Start
Connecting
Benchmarks
Deployment
Data Usage
Manage Clusters
Upgrade
Global Maintenance
Expansion
Monitoring
Security
Best Practice
Technical Principles
Data Type
Storage Engine
Execution Engine
Streaming Engine(Domino)
MARS3 Index
Extension
Advanced Features
Advanced Query
Federal Query
Grafana
Backup and Restore
Disaster Recovery
Graph Database
Introduction
Clauses
Functions
Advanced
Guide
Performance Tuning
Troubleshooting
Tools
Configuration Parameters
SQL Reference
The ORDER BY clause follows WITH. It specifies how the output should be sorted.
Note that you cannot sort vertices or edges directly — sorting must be performed on properties. ORDER BY relies on value comparison to order results. See Sorting and Comparison of Values for details.
Regarding variable scope, ORDER BY follows special scoping rules depending on whether the preceding RETURN or WITH projection is aggregating or uses DISTINCT.
DISTINCT, only variables present in the projection are available. DISTINCT), variables available before the projection are also accessible. Finally, aggregate expressions not explicitly listed in the projection clause may not appear in ORDER BY. This rule ensures ORDER BY affects only result ordering — never result composition.
Note!
In YMatrix AGE,ORDER BYdoes not guarantee globally ordered results. Therefore,ORDER BY+LIMITdoes not guarantee retrieval of the globally top-N rows.
To ensure global ordering, precedeORDER BYwith aWITHclause:
RETURN p ORDER BY p.age LIMIT 3: Each segment sorts locally and returns its top 3; final output is not guaranteed to be the global top 3.WITH p ORDER BY p.age LIMIT 3 RETURN p: TheWITHtriggers data motion (a Motion node), enabling global sorting before applyingLIMIT.
Use ORDER BY to sort the output.
SELECT *
FROM cypher('graph_name', $$
MATCH (n)
WITH n.name AS name, n.age AS age
ORDER BY n.name
RETURN name, age
$$) AS (name agtype, age agtype);
Returns vertices sorted by name.
| name | age |
|---|---|
"A" |
34 |
"B" |
34 |
"C" |
32 |
| (1 row) |
List multiple variables in the ORDER BY clause to sort by several properties. Cypher sorts first by the leftmost variable; for ties, it proceeds to the next variable in the list, and so on.
SELECT *
FROM cypher('graph_name', $$
MATCH (n)
WITH n.name AS name, n.age AS age
ORDER BY n.age, n.name
RETURN name, age
$$) AS (name agtype, age agtype);
Returns vertices sorted first by age, then by name.
| name | age |
|---|---|
"C" |
32 |
"A" |
34 |
"B" |
34 |
| (1 row) |
Append DESC or DESCENDING after a sort variable to reverse the sort order.
SELECT *
FROM cypher('graph_name', $$
MATCH (n)
WITH n.name AS name, n.age AS age
ORDER BY n.name DESC
RETURN name, age
$$) AS (name agtype, age agtype);
Returns vertices sorted by name in descending order.
| name | age |
|---|---|
"C" |
32 |
"B" |
34 |
"A" |
34 |
| (3 rows) |
When sorting a result set:
NULL values always appear last. NULL values always appear first.SELECT *
FROM cypher('graph_name', $$
MATCH (n)
WITH n.name AS name, n.age AS age, n.height
ORDER BY n.height
RETURN name, age, height
$$) AS (name agtype, age agtype, height agtype);
Returns vertices sorted by the height property; vertices lacking this property appear last.
| name | age | |
|---|---|---|
"A" |
34 |
170 |
"C" |
32 |
185 |
"B" |
34 |
<NULL> |
| (3 rows) |