RETURN¶
The RETURN
clause defines the output of an nGQL query. To return multiple fields, separate them with commas.
RETURN
can lead a clause or a statement:
- A
RETURN
clause can work in openCypher statements in nGQL, such asMATCH
orUNWIND
.
- A
RETURN
statement can work independently to output the result of an expression.
OpenCypher compatibility¶
This topic applies to the openCypher syntax in nGQL only. For native nGQL, use YIELD
.
RETURN
does not support the following openCypher features yet.
-
Return variables with uncommon characters, for example:
MATCH (`non-english_characters`:player) \ RETURN `non-english_characters`;
-
Set a pattern in the
RETURN
clause and return all elements that this pattern matches, for example:MATCH (v:player) \ RETURN (v)-[e]->(v2);
Legacy version compatibility¶
- In nGQL 1.x,
RETURN
works with native nGQL with theRETURN <var_ref> IF <var_ref> IS NOT NULL
syntax.
- In nGQL 2.0,
RETURN
does not work with native nGQL.
Map order description¶
When RETURN
returns the map data structure, the order of key-value pairs is undefined.
nebula> RETURN {age: 32, name: "Marco Belinelli"};
+------------------------------------+
| {age:32,name:"Marco Belinelli"} |
+------------------------------------+
| {age: 32, name: "Marco Belinelli"} |
+------------------------------------+
nebula> RETURN {zage: 32, name: "Marco Belinelli"};
+-------------------------------------+
| {zage:32,name:"Marco Belinelli"} |
+-------------------------------------+
| {name: "Marco Belinelli", zage: 32} |
+-------------------------------------+
Return vertices¶
nebula> MATCH (v:player) \
RETURN v;
+---------------------------------------------------------------+
| v |
+---------------------------------------------------------------+
| ("player104" :player{age: 32, name: "Marco Belinelli"}) |
| ("player107" :player{age: 32, name: "Aron Baynes"}) |
| ("player116" :player{age: 34, name: "LeBron James"}) |
| ("player120" :player{age: 29, name: "James Harden"}) |
| ("player125" :player{age: 41, name: "Manu Ginobili"}) |
+---------------------------------------------------------------+
...
Return edges¶
nebula> MATCH (v:player)-[e]->() \
RETURN e;
+------------------------------------------------------------------------------+
| e |
+------------------------------------------------------------------------------+
| [:follow "player104"->"player100" @0 {degree: 55}] |
| [:follow "player104"->"player101" @0 {degree: 50}] |
| [:follow "player104"->"player105" @0 {degree: 60}] |
| [:serve "player104"->"team200" @0 {end_year: 2009, start_year: 2007}] |
| [:serve "player104"->"team208" @0 {end_year: 2016, start_year: 2015}] |
+------------------------------------------------------------------------------+
...
Return properties¶
To return a vertex or edge property, use the {<vertex_name>|<edge_name>}.<property>
syntax.
nebula> MATCH (v:player) \
RETURN v.name, v.age \
LIMIT 3;
+-------------------+-------+
| v.name | v.age |
+-------------------+-------+
| "Rajon Rondo" | 33 |
| "Rudy Gay" | 32 |
| "Dejounte Murray" | 29 |
+-------------------+-------+
Return all elements¶
To return all the elements that this pattern matches, use an asterisk (*).
nebula> MATCH (v:player{name:"Tim Duncan"}) \
RETURN *;
+----------------------------------------------------+
| v |
+----------------------------------------------------+
| ("player100" :player{age: 42, name: "Tim Duncan"}) |
+----------------------------------------------------+
nebula> MATCH (v:player{name:"Tim Duncan"})-[e]->(v2) \
RETURN *;
+----------------------------------------------------+-----------------------------------------------------------------------+-------------------------------------------------------+
| v | e | v2 |
+----------------------------------------------------+-----------------------------------------------------------------------+-------------------------------------------------------+
| ("player100" :player{age: 42, name: "Tim Duncan"}) | [:follow "player100"->"player101" @0 {degree: 95}] | ("player101" :player{age: 36, name: "Tony Parker"}) |
| ("player100" :player{age: 42, name: "Tim Duncan"}) | [:follow "player100"->"player125" @0 {degree: 95}] | ("player125" :player{age: 41, name: "Manu Ginobili"}) |
| ("player100" :player{age: 42, name: "Tim Duncan"}) | [:serve "player100"->"team204" @0 {end_year: 2016, start_year: 1997}] | ("team204" :team{name: "Spurs"}) |
+----------------------------------------------------+-----------------------------------------------------------------------+-------------------------------------------------------+
Rename a field¶
Use the AS <alias>
syntax to rename a field in the output.
nebula> MATCH (v:player{name:"Tim Duncan"})-[:serve]->(v2) \
RETURN v2.name AS Team;
+---------+
| Team |
+---------+
| "Spurs" |
+---------+
nebula> RETURN "Amber" AS Name;
+---------+
| Name |
+---------+
| "Amber" |
+---------+
Return a non-existing property¶
If a property matched does not exist, NULL
is returned.
nebula> MATCH (v:player{name:"Tim Duncan"})-[e]->(v2) \
RETURN v2.name, type(e), v2.age;
+-----------------+----------+--------------+
| v2.name | type(e) | v2.age |
+-----------------+----------+--------------+
| "Tony Parker" | "follow" | 36 |
| "Manu Ginobili" | "follow" | 41 |
| "Spurs" | "serve" | UNKNOWN_PROP |
+-----------------+----------+--------------+
Return expression results¶
To return the results of expressions such as literals, functions, or predicates, set them in a RETURN
clause.
nebula> MATCH (v:player{name:"Tony Parker"})-->(v2:player) \
RETURN DISTINCT v2.name, "Hello"+" graphs!", v2.age > 35;
+---------------------+------------------+-------------+
| v2.name | (Hello+ graphs!) | (v2.age>35) |
+---------------------+------------------+-------------+
| "Tim Duncan" | "Hello graphs!" | true |
| "LaMarcus Aldridge" | "Hello graphs!" | false |
| "Manu Ginobili" | "Hello graphs!" | true |
+---------------------+------------------+-------------+
nebula> RETURN 1+1;
+-------+
| (1+1) |
+-------+
| 2 |
+-------+
nebula> RETURN 3 > 1;
+-------+
| (3>1) |
+-------+
| true |
+-------+
nebula> RETURN 1+1, rand32(1, 5);
+-------+-------------+
| (1+1) | rand32(1,5) |
+-------+-------------+
| 2 | 1 |
+-------+-------------+
Return unique fields¶
Use DISTINCT
to remove duplicate fields in the result set.
# Before using DISTINCT.
nebula> MATCH (v:player{name:"Tony Parker"})--(v2:player) \
RETURN v2.name, v2.age;
+---------------------+--------+
| v2.name | v2.age |
+---------------------+--------+
| "Tim Duncan" | 42 |
| "LaMarcus Aldridge" | 33 |
| "Marco Belinelli" | 32 |
| "Boris Diaw" | 36 |
| "Dejounte Murray" | 29 |
| "Tim Duncan" | 42 |
| "LaMarcus Aldridge" | 33 |
| "Manu Ginobili" | 41 |
+---------------------+--------+
# After using DISTINCT.
nebula> MATCH (v:player{name:"Tony Parker"})--(v2:player) \
RETURN DISTINCT v2.name, v2.age;
+---------------------+--------+
| v2.name | v2.age |
+---------------------+--------+
| "Tim Duncan" | 42 |
| "LaMarcus Aldridge" | 33 |
| "Marco Belinelli" | 32 |
| "Boris Diaw" | 36 |
| "Dejounte Murray" | 29 |
| "Manu Ginobili" | 41 |
+---------------------+--------+