13.1. Return

13.1.1. Return nodes
13.1.2. Return relationships
13.1.3. Return property
13.1.4. Return all elements
13.1.5. Identifier with uncommon characters
13.1.6. Column alias
13.1.7. Optional properties
13.1.8. Other expressions
13.1.9. Unique results

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.

Figure 13.1. Graph


13.1.1. Return nodes

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

Query. 

MATCH n
WHERE n.name='B'
RETURN n

The example will return the node.

Result

n
1 row

Node[2]{name:"B"}


13.1.2. Return relationships

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

Query. 

MATCH (n)-[r:KNOWS]->(c)
WHERE n.name='A'
RETURN r

The relationship is returned by the example.

Result

r
1 row

:KNOWS[0]{}


13.1.3. Return property

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

Query. 

MATCH n
WHERE n.name='A'
RETURN n.name

The value of the property name gets returned.

Result

n.name
1 row

"A"


13.1.4. 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-[r]->b
WHERE a.name='A'
RETURN *

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

Result

abrp
2 rows

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

Node[2]{name:"B"}

:KNOWS[0]{}

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

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

Node[2]{name:"B"}

:BLOCKS[1]{}

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


13.1.5. Identifier with uncommon characters

To introduce a placeholder that is made up of characters that are outside of the english alphabet, you can use the ` to enclose the identifier, like this:

Query. 

MATCH `This isn't a common identifier`
WHERE `This isn't a common identifier`.name='A'
RETURN `This isn't a common identifier`.happy

The node indexed with name "A" is returned

Result

`This isn't a common identifier`.happy
1 row

"Yes!"


13.1.6. 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
WHERE a.name='A'
RETURN a.age AS SomethingTotallyDifferent

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

Result

SomethingTotallyDifferent
1 row

55


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

Result

n.age
2 rows

55

<null>


13.1.8. Other expressions

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

Query. 

MATCH a
WHERE 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.

Result

a.age > 30"I'm a literal"a-->()
1 row

true

"I'm a literal"

true


13.1.9. Unique results

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

Query. 

MATCH (a)-->(b)
WHERE a.name='A'
RETURN DISTINCT b

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

Result

b
1 row

Node[2]{name:"B"}