RETURN
Introduction
In the RETURN
part of your query, you define which parts of the pattern you are interested in.
It can be nodes, relationships, or properties on these.
If what you actually want is the value of a property, make sure to not return the full node/relationship. This will improve performance. |
N0 [ label = "happy = \'Yes!\'\lname = \'A\'\lage = 55\l" ] N0 -> N1 [ color = "#2e3436" fontcolor = "#2e3436" label = "BLOCKS\n" ] N0 -> N1 [ color = "#4e9a06" fontcolor = "#4e9a06" label = "KNOWS\n" ] N1 [ label = "name = \'B\'\l" ]
Return nodes
To return a node, list it in the RETURN
statement.
MATCH (n {name: 'B'})
RETURN n
The example will return the node.
n |
---|
|
Rows: 1 |
Return relationships
To return a relationship, just include it in the RETURN
list.
MATCH (n {name: 'A'})-[r:KNOWS]->(c)
RETURN r
The relationship is returned by the example.
r |
---|
|
Rows: 1 |
Return property
To return a property, use the dot separator, like this:
MATCH (n {name: 'A'})
RETURN n.name
The value of the property name
gets returned.
n.name |
---|
|
Rows: 1 |
Return all elements
When you want to return all nodes, relationships and paths found in a query, you can use the *
symbol.
MATCH p = (a {name: 'A'})-[r]->(b)
RETURN *
This returns the two nodes, the relationship and the path used in the query.
a | b | p | r |
---|---|---|---|
|
|
|
|
|
|
|
|
Rows: 2 |
Variable with uncommon characters
To introduce a placeholder that is made up of characters that are not contained in the English alphabet, you can use the `
to enclose the variable, like this:
MATCH (`This isn\'t a common variable`)
WHERE `This isn\'t a common variable`.name = 'A'
RETURN `This isn\'t a common variable`.happy
The node with name "A" is returned.
`This isn\'t a common variable`.happy |
---|
|
Rows: 1 |
Column alias
If the name of the column should be different from the expression used, you can rename it by using AS
<new name>.
MATCH (a {name: 'A'})
RETURN a.age AS SomethingTotallyDifferent
Returns the age property of a node, but renames the column.
SomethingTotallyDifferent |
---|
|
Rows: 1 |
Optional properties
If a property might or might not be there, you can still select it as usual.
It will be treated as null
if it is missing.
MATCH (n)
RETURN n.age
This example returns the age when the node has that property, or null
if the property is not there.
n.age |
---|
|
|
Rows: 2 |
Other expressions
Any expression can be used as a return item — literals, predicates, properties, functions, and everything else.
MATCH (a {name: 'A'})
RETURN a.age > 30, "I'm a literal", (a)-->()
Returns a predicate, a literal and function call with a pattern expression parameter.
a.age > 30 | "I'm a literal" | (a)-->() |
---|---|---|
|
|
|
Rows: 1 |