RETURN
Introduction
The RETURN clause defines the parts of a pattern (nodes, relationships, and/or properties) to be included in the query result.
Example graph
The following graph is used for the examples below:
To recreate the graph, run the following query against an empty Neo4j database.
CREATE
(keanu:Person {name: 'Keanu Reeves', bornIn: 'Beirut', nationality: 'Canadian'}),
(taiChi:Movie {title: 'Man of Tai Chi', released: 2013}),
(keanu)-[:ACTED_IN]->(taiChi),
(keanu)-[:DIRECTED]->(taiChi)
Return nodes
To return a node, list it in the RETURN clause:
MATCH (p:Person {name: 'Keanu Reeves'})
RETURN p
| p |
|---|
|
Rows: 1 |
Return relationships
To return a relationship type, list it in the RETURN clause:
MATCH (p:Person {name: 'Keanu Reeves'})-[r:ACTED_IN]->(m)
RETURN type(r)
| type(r) |
|---|
|
Rows: 1 |
Return property
To return a specific property, use the dot separator:
MATCH (p:Person {name: 'Keanu Reeves'})
RETURN p.bornIn
| p.bornIn |
|---|
|
Rows: 1 |
| It is more performant to return properties rather than the full node or relationship. |
Return all elements
To return all nodes, relationships and paths found in a query, use the * symbol:
MATCH p = (keanu:Person {name: 'Keanu Reeves'})-[r]->(m)
RETURN *
This returns the two nodes, and the two possible paths between them.
| keanu | m | p | r |
|---|---|---|---|
|
|
|
|
|
|
|
|
Rows: 1 |
|||
Variable with uncommon characters
To introduce a variable made up of characters not contained in the English alphabet, use ` to enclose the variable:
MATCH (`/uncommon variable\`)
WHERE `/uncommon variable\`.name = 'Keanu Reeves'
RETURN `/uncommon variable\`.bornIn
The bornIn property of the node with the name property set to 'Keanu Reeves' is returned:
/uncommon variable\.bornIn |
|---|
|
Rows: 1 |
Column alias
Names of returned columns can be renamed using the AS operator:
MATCH (p:Person {name: 'Keanu Reeves'})
RETURN p.nationality AS citizenship
Returns the nationality property of 'Keanu Reeves', but the column is renamed to citizenship.
| citizenship |
|---|
|
Rows: 1 |
Optional properties
If the existence of a property is unknown, it can still be included in a RETURN clause.
It will be treated as null if it is missing.
MATCH (n)
RETURN n.bornIn
This example returns the bornIn properties for nodes that has that property, and null for those nodes missing the property.
| n.bornIn |
|---|
|
|
Rows: 2 |
Other expressions
Any expression can be used as a return item — literals, predicates, properties, functions, and so on.
MATCH (m:Movie {title: 'Man of Tai Chi'})
RETURN m.released < 2012, "I'm a literal",[p=(m)--() | p] AS `(m)--()`
Returns a predicate, a literal and function call with a pattern expression parameter:
| m.released < 2012 | "I’m a literal" | (m)--() |
|---|---|---|
|
|
|
Rows: 1 |
||
Unique results
DISTINCT retrieves only unique rows for the columns that have been selected for output.
MATCH (p:Person {name: 'Keanu Reeves'})-->(m)
RETURN DISTINCT m
The Movie node 'Man of Tai Chi' is returned by the query, but only once (without the DISTINCT operator it would have been returned twice because there are two relationships going to it from 'Keanu Reeves'):
| m |
|---|
|
Rows: 1 |