YMatrix
Quick Start
Connecting
Benchmarks
Deployment
Data Usage
Manage Clusters
Upgrade
Global Maintenance
Expansion
Monitoring
Security
Best Practice
Technical Principles
Data Type
Storage Engine
Execution Engine
Streaming Engine(Domino)
MARS3 Index
Extension
Advanced Features
Advanced Query
Federal Query
Grafana
Backup and Restore
Disaster Recovery
Graph Database
Introduction
Clauses
Functions
Advanced
Guide
Performance Tuning
Troubleshooting
Tools
Configuration Parameters
SQL Reference
In the RETURN part of a query, you specify which parts of the matched pattern to output. Output may include agtype values, vertices, edges, or properties.
To return a vertex, list it in the RETURN clause.
SELECT *
FROM cypher('graph_name', $$
MATCH (n {name: 'B'})
RETURN n
$$) AS (n agtype);
Returns the vertex.
| n |
|---|
{id: 0; label: ''; properties: {name: 'B'}}::vertex |
| (1 row) |
To return an edge associated with n, include it directly in the RETURN list.
SELECT *
FROM cypher('graph_name', $$
MATCH (n)-[r:KNOWS]->()
WHERE n.name = 'A'
RETURN r
$$) AS (r agtype);
Returns the relationship.
| r |
|---|
{id: 2; startid: 0; endid: 1; label: 'KNOWS'; properties: {}}::edge |
| (1 row) |
To return a property, use dot notation.
SELECT *
FROM cypher('graph_name', $$
MATCH (n {name: 'A'})
RETURN n.name
$$) AS (name agtype);
Returns the value of the name property.
| name |
|---|
'A' |
| (1 row) |
Use * to return all vertices, edges, and paths found by the query.
SELECT *
FROM cypher('graph_name', $$
MATCH (a {name: 'A'})-[r]->(b)
RETURN *
$$) AS (a agtype, b agtype, r agtype);
Returns both vertices and the edge used in the pattern.
| 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) |
To use placeholders composed of non-alphanumeric characters, enclose the variable name in backticks (`).
SELECT *
FROM cypher('graph_name', $$
MATCH (`This isn't a common variable`)
WHERE `This isn't a common variable`.name = 'A'
RETURN `This isn't a common variable`.happy
$$) AS (happy agtype);
Returns the happy property of the vertex named "A".
| happy |
|---|
"Yes!" |
| (1 row) |
To assign a different name to a returned field than the expression used, rename it in the column definition list.
SELECT *
FROM cypher('graph_name', $$
MATCH (n {name: 'A'})
RETURN n.name
$$) AS (objects_name agtype);
Returns the name property of the vertex, but renames the output column.
| objects_name |
|---|
'A' |
| (1 row) |
If a property may be absent, its value is treated as NULL.
SELECT *
FROM cypher('graph_name', $$
MATCH (n)
RETURN n.age
$$) AS (age agtype);
Returns the age property if present; otherwise returns NULL.
| age |
|---|
55 |
NULL |
| (2 rows) |
Any expression — literals, predicates, properties, functions, or combinations — may appear in RETURN.
SELECT *
FROM cypher('graph_name', $$
MATCH (a)
RETURN a.age > 30, 'I''m a literal', id(a)
$$) AS (older_than_30 agtype, literal agtype, id agtype);
Returns a predicate, a literal string, and a function call with a pattern expression argument.
| older_than_30 | literal | id |
|---|---|---|
true |
'I''m a literal' |
1 |
| (1 row) |
DISTINCT retrieves only unique rows based on the selected output fields.
Note!
Although every vertex has a uniqueid,DISTINCTis most relevant when the same vertex is matched multiple times via different relationships.
For example:
(A)-[:KNOWS]->(B)and(A)-[:LIKES]->(B).
ExecutingMATCH (a {name: 'A'})-[]->(b) RETURN bmatchesBtwice — once viaKNOWS, once viaLIKES— returning two identical rows.
WithDISTINCT,Bappears only once.
SELECT *
FROM cypher('graph_name', $$
MATCH (a {name: 'A'})-[]->(b)
RETURN DISTINCT b
$$) AS (b agtype);
Returns the vertex named "B", but only once.
| b |
|---|
{id: 1; label: ''; properties: {name: 'B'}}::vertex |
| (1 row) |