A variable is a unique name (identifier) assigned to represent a collection of data. Variables allow users to reference these data throughout a query, enabling data retrieval, manipulation, and further operations.
Graph Pattern Variables
Graph pattern variables include:
- Element Variable, including Node Variable and Edge Variable
- Path Variable
These variables can be declared at specific places within path patterns, allowing them to be bound to nodes, edges, or paths that match the pattern.
In this query, n
is a node variable bound to a list of nodes, e
is an edge variable bound to a list of edges, p
is a path variable that holds a path binding:
MATCH p = (:User {_id: "U01"})<-[e:Follows]-(n:User)
RETURN n, e, p
LET Variable Definition
The LET
statement allows you to define variables which effectively adds columns to the working table.
LET i = 2
RETURN i + 1
Unreferenced Variables
It is generally a good practice to remove any unreferenced variables from the query. For example,
MATCH (a)-[e]->(b)
RETURN e
If you don't need to reference the nodes bound to a
and b
, you can rewrite the query as:
MATCH ()-[e]->()
RETURN e
Unreferenced variables do not cause syntax errors but can lead to inefficiencies and reduced readability. It is best to avoid declaring variables you do not intend to use.