Skip to main content

Variable Binding in GQL

Retrieve all the teachers who liked a comment made by students who they know since 2021 or earlier:
MATCH path = (x:teacher)-[:likes ]->
(:comment)-[:author]->
(:student)-[y:knows WHERE y.since < 2021]->(x)
Here we are binding a :teacher-labelled node to the variable x and requiring there be a :likes-labelled edge to a node representing a comment. In GQL ASCII-art syntax, round brackets represent nodes, while square brackets represent edges. We then require this comment to have been made by a student (represented by an :author edge to a node with the label student). Finally, we require this student to have any edge to the original node bound to x, that this edge is labelled with :knows and that the value of its since attribute is greater than 2021

Shortest Path

Find shortest path path, from a user u1 to user u2 traversing :knows edges 1+ ({1,}) times.
MATCH path = ANY SHORTEST (u1:users {username: "saqib"})-
[:knows]->{1,}(t2:users {username: "uroosa"})
RETURN u1.username, u2.username, path_length(p) AS num_hops

Expressing complex queries using Multiple Paths

Find a pair of friends in the same city ((y.city = x.city)) who transfer money ((account_x)−[t1:transfer]−>(account_z)−[t2:transfer]−>(account_y)) to each other using a common friend who lives elsewhere ((x.city<>z.city)).
MATCH (x:account_holder)−[:friends]−>(y:account_holder)−
[:friends]−>(z:account_holder)−
[:friends]−>(x:account_holder)
, (x)−[:owns]−>(account_x)
, (y)−[:owns]−>(account_y)
, (z)−[:owns]−>(account_z)
, (account_x)−[t1:transfer]−>(account_z)−[t2:transfer]−>(account_y)
FILTER (y.city = x.city) AND (x.city<>z.city)
RETURN x.name AS name1, y.name AS name2
This pattern consists of five path patterns, separated by a commas. In GQL, the comma performs a logical join on the results of the two path patterns.