RETURN

Updated at:

The RETURN clause defines which parts of a Cypher pattern to include in the query output. You can return nodes, edges, properties, or any valid expression as agtype values.

Return nodes

List the node variable in the RETURN clause.

Example

SELECT *
FROM cypher('graph_name', $$
    MATCH (n {name: 'B'})
    RETURN n
$$) as (n agtype);

Result:

n
{id: 0; label: '' properties: {name: 'B'}}::vertex

Return edges

Include the edge variable in the RETURN clause.

Example

SELECT *
FROM cypher('graph_name', $$
    MATCH (n)-[r:KNOWS]->()
    WHERE n.name = 'A'
    RETURN r
$$) as (r agtype);

Result:

r
{id: 2; startid: 0; endid: 1; label: 'KNOWS' properties: {}}::edge

Return properties

Use the dot separator to return a specific property instead of the full node or edge.

Example

SELECT *
FROM cypher('graph_name', $$
    MATCH (n {name: 'A'})
    RETURN n.name
$$) as (name agtype);

Result:

name
'A'

Return all elements

Use * to return all vertices, edges, and paths found in the query. The SQL column list must enumerate each expected output variable with its type.

Example

SELECT *
FROM cypher('graph_name', $$
    MATCH (a {name: 'A'})-[r]->(b)
    RETURN *
$$) as (a agtype, b agtype, r agtype);

Result:

abr
{"id": 281474976710659, "label": "", "properties": {"age": 55, "name": "A", "happy": "Yes!"}}::vertex{"id": 281474976710660, "label": "", "properties": {"name": "B"}}::vertex{"id": 1125899906842625, "label": "BLOCKS", "end_id": 281474976710660, "start_id": 281474976710659, "properties": {}}::edge
{"id": 281474976710659, "label": "", "properties": {"age": 55, "name": "A", "happy": "Yes!"}}::vertex{"id": 281474976710660, "label": "", "properties": {"name": "B"}}::vertex{"id": 1407374883553281, "label": "KNOWS", "end_id": 281474976710660, "start_id": 281474976710659, "properties": {}}::edge

Variables with uncommon characters

To use a variable name that contains characters outside the English alphabet, enclose it in backticks (`).

Example

SELECT *
FROM cypher('graph_name', $$
    MATCH (`This isn\'t a common variable`)
    WHERE `This isn\'t a common variable`.name = 'A'
    RETURN `This isn\'t a common variable`.happy
$$) as (happy agtype);

Result:

happy
"Yes!"

Column aliases

Rename a return column by specifying a different name in the SQL column list. The alias applies at the SQL wrapper level, not inside the Cypher block.

Example

SELECT *
FROM cypher('graph_name', $$
    MATCH (n {name: 'A'})
    RETURN n.name
$$) as (objects_name agtype);

Result:

objects_name
"A"

Optional properties

If a property does not exist on a node, the query returns null for that row instead of omitting it.

Example

SELECT *
FROM cypher('graph_name', $$
    MATCH (n)
    RETURN n.age
$$) as (age agtype);

Result:

age
55
NULL

Other expressions

Any valid expression can be a return item, including literals, predicates, and functions.

Example

SELECT *
FROM cypher('graph_name', $$
    MATCH (a)
    RETURN a.age > 30, 'I''m a literal', id(a)
$$) as (older_than_30 agtype, literal agtype, id agtype);

Result:

older_than_30literalid
true'I'm a literal'1

Unique results

Add DISTINCT to deduplicate results based on the selected output fields.

Example

SELECT *
FROM cypher('graph_name', $$
    MATCH (a {name: 'A'})-[]->(b)
    RETURN DISTINCT b
$$) as (b agtype);

Result:

b
{id: 1; label: '' properties: {name: 'B'}}::vertex