UltipaDocs
Try Playground
  • Introduction
  • GQL vs Other Languages
    • Overview
    • Node and Edge Patterns
    • Path Patterns
    • Quantified Paths
    • Shortest Paths
    • Graph Patterns
    • Overview
    • Closed Graph
    • Open Graph
    • Graph Sharding and Storage
    • Constraints
    • Unique Identifiers
    • INSERT
    • INSERT OVERWRITE
    • UPSERT
    • SET
    • REMOVE
    • DELETE
    • Query Composition
    • Result Table and Visualization
    • MATCH
    • OPTIONAL MATCH
    • FILTER
    • LET
    • FOR
    • ORDER BY
    • LIMIT
    • SKIP
    • CALL
    • RETURN
    • Composite Query
    • NEXT
    • All Functions
    • Scalar Functions
    • Path Functions
    • Aggregate Functions
    • Mathematical Functions
    • Trigonometric Functions
    • String Functions
    • List Functions
    • Datetime Functions
    • Spatial Functions
    • Type Conversion Functions
    • Table Functions
    • AI & Vector Functions
    • Database Functions
  • Operators
  • Predicates
  • Expressions
    • Index
    • Full-text Index
    • Vector Index
  • Transactions
  • Triggers
  • Query Management
    • Variables
    • Values and Types
    • Comments
    • Reserved Words
    • Syntactic Notation
  • GQL Conformance
  1. Docs
  2. /
  3. ISO GQL
  4. /
  5. Querying

ORDER BY

Overview

The ORDER BY statement allows you to sort the intermediate result or output table based on the specified columns.

Syntax
<order by statement> ::= 
  "ORDER BY" <sort specification> [ { "," <sort specification> }... ]

<sort specification> ::=
  <value expression> [ "ASC" | "DESC" ] [ "NULLS FIRST" | "NULLS LAST" ]

Details

  • ASC (ascending) is applied by default. To reverse the order, you can explicitly use the DESC (descending) keyword.
  • NULLS FIRST and NULLS LAST can be used to control whether null values appear before or after non-null values. When null ordering is not explicitly specified:
    • NULLS LAST is applied by default when ordering in the ASC order.
    • NULLS FIRST is applied by default when ordering in the DESC order.

Example Graph

CREATE GRAPH myGraph { 
  NODE Paper ({title string, score uint32, author string, publisher string}),
  EDGE Cites ()-[{weight uint32}]->()
} PARTITION BY HASH(Crc32) SHARDS [1]

Ordering by Property

GQL
MATCH (n:Paper)
ORDER BY n.score
RETURN n.title, n.score

Result:

n.titlen.score
Efficient Graph Search6
Path Patterns7
Optimizing Queries9

Ordering by Node or Edge Variable

When a node or edge variable is specified, it is sorted on the _uuid of the nodes or edges.

GQL
MATCH (n:Paper)
RETURN n.title, element_id(n) ORDER BY n

Result:

n.titleelement_id(n)
Optimizing Queries8718971077612535810
Efficient Graph Search8791028671650463745
Path Patterns12033620403357220867

Ordering by Expression

GQL
MATCH p = (:Paper)->{1,2}(:Paper)
RETURN p, path_length(p) AS length ORDER BY length DESC

Result:

plength
2
1
1

Multi-level Ordering

When there are multiple specifications, it is sorted by the first specification listed, and for equals values, go to the next specification, and so on.

GQL
MATCH (n:Paper)
RETURN n.title, n.author, n.score 
ORDER BY n.author DESC, n.score

Result:

n.titlen.authorn.score
Path PatternsZack7
Efficient Graph SearchAlex6
Optimizing QueriesAlex9

Discarding and Retaining Records After Ordering

You may use the SKIP or LIMIT statement after the ORDER BY statement to skip a specified number of records from the top, or to limit the number of records retained.

To return titles of the two papers with the second and third highest scores:

GQL
MATCH (n:Paper)
RETURN n.title, n.score
ORDER BY n.score DESC SKIP 1 LIMIT 2

Result:

n.titlen.score
Path Patterns7
Efficient Graph Search6

Null Ordering

To return titles of the two papers with the second and third highest scores, ensuring null values appear at the front if applicable:

GQL
MATCH (n:Paper)
RETURN n.title, n.publisher
ORDER BY n.publisher NULLS FIRST

Result:

n.titlen.score
Optimizing Queriesnull
Path PatternsBrightLeaf
Efficient Graph SearchPulsePress