In the RETURN part of your query, you define which parts of the pattern you want to output. Output can include agtype values, nodes, relationships, or properties.

Return nodes

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

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. MATCH (n {name: 'B'})
  4. RETURN n
  5. $$) as (n agtype);

The example will return the node.

Result

n
{id: 0; label: ‘’ properties: {name: ‘B’}}::vertex
(1 row)

Return edges

To return n‘s edges, just include it in the RETURN list.

Query

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

The relationship is returned by the example.

r
{id: 2; startid: 0; endid: 1; label: ‘KNOWS’ properties: {}}::edge
(1 row)

Return property

To return a property, use the dot separator, as follows:

Query

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

The value of the property name gets returned.

Result

name
‘A’
(1 row)

Return all elements

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

Query

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

This returns the two vertices, and the edge used in the query.

Result

a b r
{“id”: 281474976710659, “label”: “”, “properties”: {“age”: 55, “name”: “A”, “happy”: “Yes!”}}::vertex {“id”: 1125899906842625, “label”: “BLOCKS”, “end_id”: 281474976710660, “start_id”: 281474976710659, “properties”: {}}::edge {“id”: 281474976710660, “label”: “”, “properties”: {“name”: “B”}}::vertex
{“id”: 281474976710659, “label”: “”, “properties”: {“age”: 55, “name”: “A”, “happy”: “Yes!”}}::vertex {“id”: 1407374883553281, “label”: “KNOWS”, “end_id”: 281474976710660, “start_id”: 281474976710659, “properties”: {}}::edge {“id”: 281474976710660, “label”: “”, “properties”: {“name”: “B”}}::vertex
(2 rows)

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

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

The node with name “A” is returned.

Result

happy
“Yes!”
(1 row)

Aliasing a field

If the name of the field should be different from the expression used, you can rename it by changing the name in the column list definition.

Query

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

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

Result

objects_name
‘A’
(1 row)

Optional properties

If a property might or might not be there, it will be treated as null if it is missing.

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. MATCH (n)
  4. RETURN n.age
  5. $$) as (age agtype);

This query returns the property if it exists, or null if the property does not exist.

Result

age
55
NULL
(2 rows)

Other expressions

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

Query

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

Returns a predicate, a literal and function call with a pattern expression parameter.

Result

older_than_30 literal id
true ‘I’m a literal’ 1
(1 row)

Unique results

DISTINCT retrieves only unique records depending on the fields that have been selected to output.

Query

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

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

Result

b
{id: 1; label: ‘’ properties: {name: ‘B’}}::vertex
(1 row)