A variable is a unique name (identifier) assigned to represent a collection of records. Variables allow users to reference these data throughout a query, enabling data retrieval, manipulation, and further operations.
Graph pattern variables include:
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:
GQLMATCH p = (:User {_id: "U01"})<-[e:Follows]-(n:User) RETURN n, e, p
The LET statement allows you to define variables which effectively adds columns to the intermediate result table.
GQLLET i = 2 RETURN i + 1
It is generally a good practice to remove any unreferenced variables from the query. For example,
GQLMATCH (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:
GQLMATCH ()-[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.
A query parameter is a placeholder of the form $name that is bound to a value supplied by the driver at execution time, rather than written as a literal in the query text. Parameters can be referenced anywhere a value expression is allowed.
GQLMATCH (p:Person) WHERE p.age > $minAge AND p.city = $city RETURN p.name
Parameter values are not declared inside the query; they are passed in through the driver's parameter map. Using parameters instead of string-concatenated literals avoids injection risks, keeps the query text reusable across different inputs, and lets the planner reuse cached plans.