3.3.4. RETURN

The RETURN clause defines what to include in the query result set.

3.3.4.1. 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.

Figure 3.11. Graph
alt

3.3.4.2. Return nodes

To return a node, list it in the RETURN statement.

Query. 

MATCH (n { name: 'B' })
RETURN n

The example will return the node.

Table 3.62. Result
n

1 row

Node[1]\{name:"B"\}

Try this query live.  CREATE (a {name: 'A', happy: 'Yes!', age: 55}), (b {name: 'B'}), (a)-[:KNOWS]->(b), (a)-[:BLOCKS]->(b) MATCH (n {name: 'B'}) RETURN n

3.3.4.3. Return relationships

To return a relationship, just include it in the RETURN list.

Query. 

MATCH (n { name: 'A' })-[r:KNOWS]->(c)
RETURN r

The relationship is returned by the example.

Table 3.63. Result
r

1 row

:KNOWS[0]\{\}

Try this query live.  CREATE (a {name: 'A', happy: 'Yes!', age: 55}), (b {name: 'B'}), (a)-[:KNOWS]->(b), (a)-[:BLOCKS]->(b) MATCH (n {name: 'A'})-[r:KNOWS]->(c) RETURN r

3.3.4.4. Return property

To return a property, use the dot separator, like this:

Query. 

MATCH (n { name: 'A' })
RETURN n.name

The value of the property name gets returned.

Table 3.64. Result
n.name

1 row

"A"

Try this query live.  CREATE (a {name: 'A', happy: 'Yes!', age: 55}), (b {name: 'B'}), (a)-[:KNOWS]->(b), (a)-[:BLOCKS]->(b) MATCH (n {name: 'A'}) RETURN n.name

3.3.4.5. Return all elements

When you want to return all nodes, relationships and paths found in a query, you can use the * symbol.

Query. 

MATCH p =(a { name: 'A' })-[r]->(b)
RETURN *

This returns the two nodes, the relationship and the path used in the query.

Table 3.65. Result
a b p r

2 rows

Node[0]\{name:"A",happy:"Yes!",age:55\}

Node[1]\{name:"B"\}

[Node[0]\{name:"A",happy:"Yes!",age:55\},:BLOCKS[1]\{\},Node[1]\{name:"B"\}]

:BLOCKS[1]\{\}

Node[0]\{name:"A",happy:"Yes!",age:55\}

Node[1]\{name:"B"\}

[Node[0]\{name:"A",happy:"Yes!",age:55\},:KNOWS[0]\{\},Node[1]\{name:"B"\}]

:KNOWS[0]\{\}

Try this query live.  CREATE (a {name: 'A', happy: 'Yes!', age: 55}), (b {name: 'B'}), (a)-[:KNOWS]->(b), (a)-[:BLOCKS]->(b) MATCH p = (a {name: 'A'})-[r]->(b) RETURN *

3.3.4.6. 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:

Query. 

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.

Table 3.66. Result
This isn't a common variable.happy

1 row

"Yes!"

Try this query live.  CREATE (a {name: 'A', happy: 'Yes!', age: 55}), (b {name: 'B'}), (a)-[:KNOWS]->(b), (a)-[:BLOCKS]->(b) MATCH (`This isn\'t a common variable`) WHERE `This isn\'t a common variable`.name = 'A' RETURN `This isn\'t a common variable`.happy

3.3.4.7. Column alias

If the name of the column should be different from the expression used, you can rename it by using AS <new name>.

Query. 

MATCH (a { name: 'A' })
RETURN a.age AS SomethingTotallyDifferent

Returns the age property of a node, but renames the column.

Table 3.67. Result
SomethingTotallyDifferent

1 row

55

Try this query live.  CREATE (a {name: 'A', happy: 'Yes!', age: 55}), (b {name: 'B'}), (a)-[:KNOWS]->(b), (a)-[:BLOCKS]->(b) MATCH (a {name: 'A'}) RETURN a.age AS SomethingTotallyDifferent

3.3.4.8. 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.

Query. 

MATCH (n)
RETURN n.age

This example returns the age when the node has that property, or null if the property is not there.

Table 3.68. Result
n.age

2 rows

55

<null>

Try this query live.  CREATE (a {name: 'A', happy: 'Yes!', age: 55}), (b {name: 'B'}), (a)-[:KNOWS]->(b), (a)-[:BLOCKS]->(b) MATCH (n) RETURN n.age

3.3.4.9. Other expressions

Any expression can be used as a return item — literals, predicates, properties, functions, and everything else.

Query. 

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.

Table 3.69. Result
a.age > 30 "I’m a literal" (a)-→()

1 row

true

"I’m a literal"

[[Node[0]\{name:"A",happy:"Yes!",age:55\},:BLOCKS[1]\{\},Node[1]\{name:"B"\}],[Node[0]\{name:"A",happy:"Yes!",age:55\},:KNOWS[0]\{\},Node[1]\{name:"B"\}]]

Try this query live.  CREATE (a {name: 'A', happy: 'Yes!', age: 55}), (b {name: 'B'}), (a)-[:KNOWS]->(b), (a)-[:BLOCKS]->(b) MATCH (a {name: 'A'}) RETURN a.age > 30, "I'm a literal", (a)-->()

3.3.4.10. Unique results

DISTINCT retrieves only unique rows depending on the columns that have been selected to output.

Query. 

MATCH (a { name: 'A' })-->(b)
RETURN DISTINCT b

The node named "B" is returned by the query, but only once.

Table 3.70. Result
b

1 row

Node[1]\{name:"B"\}

Try this query live.  CREATE (a {name: 'A', happy: 'Yes!', age: 55}), (b {name: 'B'}), (a)-[:KNOWS]->(b), (a)-[:BLOCKS]->(b) MATCH (a {name: 'A'})-->(b) RETURN DISTINCT b