ORDER BY

The ORDER BY clause follows WITH. It specifies how the output should be sorted.

Introduction

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.

  • If the projection is aggregating or DISTINCT, only variables present in the projection are available.
  • If the projection preserves cardinality (i.e., is neither aggregating nor DISTINCT), variables available before the projection are also accessible.
  • When a projection introduces a variable with the same name as an existing one, only the newly introduced variable is visible.

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 BY does not guarantee globally ordered results. Therefore, ORDER BY + LIMIT does not guarantee retrieval of the globally top-N rows.
To ensure global ordering, precede ORDER BY with a WITH clause:

  • 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: The WITH triggers data motion (a Motion node), enabling global sorting before applying LIMIT.

Sorting Vertices by Property

Use ORDER BY to sort the output.

Query

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.

Result

name age
"A" 34
"B" 34
"C" 32
(1 row)

Sorting Vertices by Multiple Properties

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.

Query

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.

Result

name age
"C" 32
"A" 34
"B" 34
(1 row)

Sorting Vertices in Descending Order

Append DESC or DESCENDING after a sort variable to reverse the sort order.

Query

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.

Result

name age
"C" 32
"B" 34
"A" 34
(3 rows)

Null Value Ordering

When sorting a result set:

  • In ascending order, NULL values always appear last.
  • In descending order, NULL values always appear first.

Query

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.

Result

name age
"A" 34 170
"C" 32 185
"B" 34 <NULL>
(3 rows)