Skip to content

ORDER BY

The ORDER BY clause specifies the order of the rows in the output.

  • Native nGQL: You must use a pipe (|) and an ORDER BY clause after YIELD clause.
  • OpenCypher style: No pipes are permitted. The ORDER BY clause follows a RETURN clause.

There are two order options:

  • ASC: Ascending. ASC is the default order.
  • DESC: Descending.

Native nGQL Syntax

<YIELD clause>
ORDER BY <expression> [ASC | DESC] [, <expression> [ASC | DESC] ...];

Compatibility

In the native nGQL syntax, $-. must be used after ORDER BY. But it is not required in releases prior to 2.5.0.

Examples

nebula> FETCH PROP ON player "player100", "player101", "player102", "player103" \
        YIELD player.age AS age, player.name AS name \
        | ORDER BY $-.age ASC, $-.name DESC;
+-------------+-----+---------------------+
| VertexID    | age | name                |
+-------------+-----+---------------------+
| "player103" | 32  | "Rudy Gay"          |
| "player102" | 33  | "LaMarcus Aldridge" |
| "player101" | 36  | "Tony Parker"       |
| "player100" | 42  | "Tim Duncan"        |
+-------------+-----+---------------------+

nebula> $var = GO FROM "player100" OVER follow \
        YIELD dst(edge) AS dst; \
        ORDER BY $var.dst DESC;
+-------------+
| dst         |
+-------------+
| "player125" |
| "player101" |
+-------------+

OpenCypher Syntax

<RETURN clause>
ORDER BY <expression> [ASC | DESC] [, <expression> [ASC | DESC] ...];

Examples

nebula> MATCH (v:player) RETURN v.name AS Name, v.age AS Age  \
        ORDER BY Name DESC;
+-----------------+-----+
| Name            | Age |
+-----------------+-----+
| "Yao Ming"      | 38  |
| "Vince Carter"  | 42  |
| "Tracy McGrady" | 39  |
| "Tony Parker"   | 36  |
| "Tim Duncan"    | 42  |
+-----------------+-----+
...

# In the following example, nGQL sorts the rows by age first. If multiple people are of the same age, nGQL will then sort them by name.
nebula> MATCH (v:player) RETURN v.age AS Age, v.name AS Name  \
        ORDER BY Age DESC, Name ASC;
+-----+-------------------+
| Age | Name              |
+-----+-------------------+
| 47  | "Shaquille O'Neal" |
| 46  | "Grant Hill"      |
| 45  | "Jason Kidd"      |
| 45  | "Steve Nash"      |
+-----+-------------------+
...

Order of NULL values

nGQL lists NULL values at the end of the output for ascending sorting, and at the start for descending sorting.

nebula> MATCH (v:player{name:"Tim Duncan"}) --> (v2) \
        RETURN v2.name AS Name, v2.age AS Age  \
        ORDER BY Age;
+-----------------+--------------+
| Name            | Age          |
+-----------------+--------------+
| "Tony Parker"   | 36           |
| "Manu Ginobili" | 41           |
| "Spurs"         | UNKNOWN_PROP |
+-----------------+--------------+

nebula> MATCH (v:player{name:"Tim Duncan"}) --> (v2) \
        RETURN v2.name AS Name, v2.age AS Age  \
        ORDER BY Age DESC;
+-----------------+--------------+
| Name            | Age          |
+-----------------+--------------+
| "Spurs"         | UNKNOWN_PROP |
| "Manu Ginobili" | 41           |
| "Tony Parker"   | 36           |
+-----------------+--------------+

Last update: November 1, 2021
Back to top