The RETURN statement allows you to specify items to include in the query result. Each item is defined by an expression that can include variables, properties, functions, constants, etc.
Syntax<return statement> ::= "RETURN" [ "DISTINCT" | "ALL" ] { <"*"> | <return items> } [ <group by clause> ] <return items> ::= <return item> [ { "," <return item> }... ] <return item> ::= <value expression> [ "AS" <identifier> ] <group by clause> ::= "GROUP BY" <grouping key> [ { "," <grouping key> }... ]
Details
* returns all columns in the intermediate result table. See Returning All.AS can be used to rename a return item. See Return Item Alias.RETURN statement supports the GROUP BY clause. See Returning with Grouping.DISTINCT operator can be used to deduplicate records. If neither DISTINCT nor ALL is specified, ALL is implicitly applied. See Returning Distinct Records.
CREATE GRAPH myGraph { NODE Student ({name string, gender string}), NODE Course ({name string, credit uint32}), EDGE Take ()-[{year uint32, term string}]->() } PARTITION BY HASH(Crc32) SHARDS [1]
A variable bound to nodes returns all information about each node.
GQLMATCH (n:Course) RETURN n
Result: n
| _id | _uuid | schema | values |
|---|---|---|---|
| c1 | Sys-gen | Course | {name: "Art", credit: 13} |
| c2 | Sys-gen | Course | {name: "Literature", credit: 15} |
A variable bound to edges returns all information about each edge.
GQLMATCH ()-[e]->() RETURN e
Result: e
_uuid | _from | _to | _from_uuid | _to_uuid | schema | values |
|---|---|---|---|---|---|---|
| Sys-gen | s2 | c1 | UUID of s2 | UUID of c1 | Take | {year: 2023, term: "Fall"} |
| Sys-gen | s2 | c2 | UUID of s2 | UUID of c2 | Take | {year: 2023, term: "Spring"} |
| Sys-gen | s1 | c1 | UUID of s1 | UUID of c1 | Take | {year: 2024, term: "Spring"} |
A variable bound to paths returns all information about the nodes and edges included in each path.
GQLMATCH p = ()-[:Take {term: "Spring"}]->() RETURN p
Result: p

The function labels() can be used to return the labels of nodes and edges.
GQLMATCH ({_id: "s2"})-[e]->(n) RETURN labels(e), labels(n)
Result:
| labels(e) | labels(n) |
|---|---|
| Take | Course |
| Take | Course |
The period operator . can be used to extract the value of a specified property from a variable bound to nodes or edges. The null value will be returned if the specified property is not found on the nodes or edges.
GQLMATCH (:Student {name:"Susan"})-[]->(c:Course) RETURN c.name, c.credit, c.type
Result:
| c.name | c.credit | c.type |
|---|---|---|
| Literature | 15 | null |
| Art | 13 | null |
The asterisk * returns all columns in the intermediate result table. Note that the RETURN statement cannot include the GROUP BY clause when using *.
GQLMATCH (s:Student {name:"Susan"})-[]->(c:Course) RETURN *
Result:
s
| _id | _uuid | schema | values |
|---|---|---|---|
| s2 | Sys-gen | Student | {name: "Susan", gender: "female"} |
| s2 | Sys-gen | Student | {name: "Susan", gender: "female"} |
c
| _id | _uuid | schema | values |
|---|---|---|---|
| c1 | Sys-gen | Course | {name: "Art", credit: 13} |
| c2 | Sys-gen | Course | {name: "Literature", credit: 15} |
The AS keyword allows you to assign an alias to a return item.
GQLMATCH (s:Student)-[t:Take]->(c:Course) RETURN s.name AS Student, c.name AS Course, t.year AS TakenIn
Result:
| Student | Course | TakenIn |
|---|---|---|
| Alex | Art | 2024 |
| Susan | Art | 2023 |
| Susan | Literature | 2023 |
Aggregation functions, such as sum() and max(), can be directly applied in the RETURN statement.
GQLMATCH (:Student {name:"Susan"})-[]->(c:Course) RETURN sum(c.credit)
Result:
| sum(c.credit) |
|---|
| 28 |
Due to the use of the aggregate function, the c returned by this query contains only one record, as expected:
GQLMATCH (:Student {name:"Susan"})-[]->(c:Course) RETURN c, sum(c.credit)
Result:
c
| _id | _uuid | schema | values |
|---|---|---|---|
| c1 | Sys-gen | Course | {name: "Art", credit: 13} |
sum(c.credit)
| sum(c.credit) |
|---|
| 28 |
GQLMATCH (n:Course) RETURN n.name, CASE WHEN n.credit > 14 THEN "Y" ELSE "N" END AS Recommended
Result:
| n.name | Recommended |
|---|---|
| Art | N |
| Literature | Y |
The LIMIT statement can be used to restrict the number of records retained for each return item.
GQLMATCH (n:Course) RETURN n.name LIMIT 1
Result:
| n.name |
|---|
| Art |
The ORDER BY statement can be used to sort the records.
GQLMATCH (n:Course) RETURN n ORDER BY n.credit DESC
Result: n
| _id | _uuid | schema | values |
|---|---|---|---|
| c2 | Sys-gen | Course | {name: "Literature", credit: 15} |
| c1 | Sys-gen | Course | {name: "Art", credit: 13} |
The GROUP BY clause allows you to specify the keys to group the query result. After grouping, each group will keep only one record.
GQLMATCH ()-[e:Take]->() RETURN e.term GROUP BY e.term
Result:
| e.term |
|---|
| Spring |
| Fall |
In the GQL standard, the grouping key must be a direct variable reference, where this query must be written as RETURN e.term AS <varName> GROUP BY <varName>. Ultipa simplifies this by allowing direct grouping on expressions, removing the need to introduce intermediate variables.
GQLMATCH ()<-[e:Take]-() RETURN e.year, e.term GROUP BY e.year, e.term
Result:
| e.year | e.term |
|---|---|
| 2023 | Spring |
| 2024 | Spring |
| 2023 | Fall |
When grouping is applied, any aggregation operation in the RETURN statement is performed on each group.
This query counts the number of Take edges for each Term:
GQLMATCH ()-[e:Take]->() RETURN e.term, count(e) GROUP BY e.term
Result:
| e.term | count(e) |
|---|---|
| Spring | 2 |
| Fall | 1 |
The DISTINCT operator deduplicates records for all return items. When DISTINCT is specified, each return item is implicly an operand of a grouping operation.
GQLMATCH ()-[e]->() RETURN DISTINCT e.year
This is equivalent to:
GQLMATCH ()-[e]->() RETURN e.year GROUP BY e.year
Result:
| e.year |
|---|
| 2023 |
| 2024 |
GQLMATCH ()-[e]->() RETURN DISTINCT e.year, e.term
This is equivalent to:
GQLMATCH ()-[e]->() RETURN e.year, e.term GROUP BY e.year, e.term
Result:
| e.year | e.term |
|---|---|
| 2023 | Fall |
| 2023 | Spring |
| 2024 | Spring |