UltipaDocs
Products
Solutions
Resources
Company
Start Free Trial
UltipaDocs
Start Free Trial
  • Introduction
  • GQL vs Other Languages
    • Overview
    • Node and Edge Patterns
    • Path Patterns
    • Quantified Paths
    • Questioned Paths
    • Shortest Paths
    • Graph Patterns
    • Overview
    • Open Graph
    • Closed Graph
    • Constraints
    • Unique Identifiers
    • INSERT
    • INSERT OVERWRITE
    • 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
    • Element Functions
    • Path Functions
    • Aggregate Functions
    • Mathematical Functions
    • Trigonometric Functions
    • String Functions
    • List Functions
    • Datetime Functions
    • Spatial Functions
    • Null Functions
    • Utility Functions
    • Type Conversion Functions
    • Table Functions
    • AI & Vector Functions
    • Database Functions
  • Operators
  • Predicates
    • CASE
    • LET Value Expression
    • Value Query Expression
    • List Comprehension
    • Index
    • Full-text Index
    • Vector Index
  • Transactions
  • Triggers
  • Query Management
  • Backup and Restore
    • 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 table or output table based on the specified items.

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

GQL
INSERT (p1:Paper {_id:'P1', title:'Efficient Graph Search', score:6, author:'Alex', publisher:'PulsePress'}),
       (p2:Paper {_id:'P2', title:'Optimizing Queries', score:9, author:'Alex'}),
       (p3:Paper {_id:'P3', title:'Path Patterns', score:7, author:'Zack', publisher:'BrightLeaf'}),
       (p1)-[:Cites {weight:2}]->(p2),
       (p2)-[:Cites {weight:1}]->(p3)

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 _id of the nodes or edges.

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

Result:

n.titleelement_id(n)
Efficient Graph SearchP1
Optimizing QueriesP2
Path PatternsP3

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 ordering 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 ORDER BY to skip a specified number of rows from the top, or to limit the number of rows retained.

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

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.publisher
Optimizing Queriesnull
Path PatternsBrightLeaf
Efficient Graph SearchPulsePress