UltipaDocs
Try Playground
  • Introduction
  • Terminologies
    • Graphset
    • Schema
    • Property
    • Constraints
    • Insert
    • Overwrite or Insert
    • Upsert
    • Update
    • Delete
    • Find Nodes
    • Find Edges
      • AB
      • Autonet
      • Spread
      • Path Template
      • K-Hop
      • K-Hop Template
    • GROUP BY
    • ORDER BY
    • SKIP
    • LIMIT
    • WHERE
    • RETURN
    • WITH
    • UNCOLLECT
    • UNION
    • UNION ALL
    • CALL
    • All Functions
    • Path Functions
    • Aggregate Functions
    • Mathematical Functions
    • Trigonometric Functions
    • String Functions
    • List Functions
    • Datetime Functions
    • Spatial Functions
    • Table Functions
    • Null Functions
    • Type Conversion Functions
  • Operators
  • Expressions
    • Index
    • Full-text Index
    • Vector Index
    • Cache
    • Overview
    • Managing HDC Graphs
    • HDC Graph Queries
    • Process
    • Job
    • Execution Plan
    • Alias
    • Filter
    • Values and Types
    • Data Flow in Queries
    • Comments
    • Reserved Words
  • Access Control
  1. Docs
  2. /
  3. UQL
  4. /
  5. Querying

ORDER BY

Overview

The ORDER BY statement sorts records based on a set of keys. When multiple keys are provided, sorting is applied sequentially from left to right: first by the first key, then by the second key for records with identical first key values, and so on.

Syntax

Syntax
ORDER BY <key1> <desc_asc?>, <key2?> <desc_asc?>, ...

Details

  • <key>: The sorting key which references an alias declared in previous statements.
  • <desc_asc?>: The ordering specification, which is ASC (ascending) or DESC (descending). It's optional and ASC is applied by default.
  • The null values are excluded from the sorting process, and their corresponding records are placed at the end of the sorted results.

Example Graph

To create the graph, execute each of the following UQL queries sequentially in an empty graphset:

UQL
create().node_schema("student").node_schema("course").edge_schema("takes")
create().node_property(@*, "name").node_property(@student, "age", int32).node_property(@course, "credits", int32)
insert().into(@student).nodes([{_id:"S1", name:"Jason", age:25}, {_id:"S2", name:"Lina", age:23}, {_id:"S3", name:"Eric", age:24}, {_id:"S4", name:"Emma", age:26}, {_id:"S5", name:"Pepe", age:24}])
insert().into(@course).nodes([{_id:"C1", name:"French", credits:4}, {_id:"C2", name:"Math", credits:5}])
insert().into(@takes).edges([{_from:"S1", _to:"C1"}, {_from:"S2", _to:"C1"}, {_from:"S3", _to:"C1"}, {_from:"S2", _to:"C2"}, {_from:"S3", _to:"C2"}, {_from:"S4", _to:"C2"}, {_from:"S5", _to:"C2"}])

Ordering by Property

UQL
find().nodes({@student}) as stu
order by stu.age desc
return table(stu.name, stu.age)

Result:

stu.namestu.age
Emma26
Jason25
Pepe24
Eric24
Lina23

Ordering by Node/Edge Alias

When a node or edge alias is used as the sorting key, records are sorted by the _uuid of the corresponding nodes or edges.

UQL
find().nodes({@student}) as stu
order by stu
return table(stu.name, stu._uuid)

Result:

stu.namestu._uuid
Pepe5404321751867850754
Jason5908724910133346305
Eric9079259047802175489
Emma15924730481405329410
Lina16717364015822536705

Ordering by Expression

UQL
n({name == "Jason"}).e()[:3].n() as p
order by length(p)
return p{*}
Result: p
Jason -> French
Jason -> French <- Lina
Jason -> French <- Eric
Jason -> French <- Lina -> Math
Jason -> French <- Eric -> Math

Multi-level Ordering

UQL
n({@course} as crs).e().n({@student} as stu) as p
order by crs.credits, stu.age desc
return table(crs.name, crs.credits, stu.name, stu.age)

Result:

crs.namecrs.creditsstu.namestu.age
French4Jason25
French4Eric24
French4Lina23
Math5Emma26
Math5Eric24
Math5Pepe24
Math5Lina23

Grouping and Ordering

To count the number of students enrolled in each course and sort the results by the count:

UQL
n({@course} as crs).e().n({@student})
group by crs
with crs, count(crs) as stuCnt
order by stuCnt desc
return table(crs.name, stuCnt)

Result:

crs.namestuCnt
Math4
French3