RETURN is similar to the SELECT clause of SQL. RETURN is where the final results of the
query are specified, which can be listed as a list of expressions, e.g., variables that have
bound to nodes or relationships, aliases, or more complex expressions. RETURN can also be used
for performing group-by and aggregations as well as duplication removing (using the DISTINCT clause).
We discuss several common expressions used in RETURN.
We will use the example database for demonstration, whose schema and data import commands are given here
Returning node and relationship variables
Returning variables in the query that are bound to node and relationships in the query
is a syntactic sugar to return all properties of those variables. For example:
Output:
Returning all variables
Returning all variables in the query can be written as RETURN * as a syntactic sugar. Below query returns a and b, relationship is omitted because no variable binds to it.
Output:
Returning node and relationship properties
You can also return properties of variables by explicitly specifying properties in the RETURN clause.
Output:
As a syntactic sugar, Kùzu supports returning all properties of node or rel with *.
Output:
Output:
Using distinct for duplicate elimination
You can use RETURN DISTINCT to do duplicate elimination of the returned tuples.
For example, if we instead wrote RETURN DISTINCT in the above query, we would
eliminate one of the 2 (Adam, 30, 2020) tuples above:
Output:
Group by and aggregations
You can group by one or more expression and perform one or more aggregations
in a RETURN clause. For example:
Output:
The semantics is exactly the same as SQL’s semantics, which is a 3-step process:
for each tuple t in the previous part of the query, i.e., before the RETURN clause,
group t according to (one or more) group by key expressions into a group. Let us refer
to the result of these expressions as t’s keys.
For each group G, compute the (or or more) aggregations in the query.
Output for each group G, G’s key(s) and the result of the aggregations.
You can find the list of aggregation functions supported in Kùzu here.