Skip to main content

Documentation Index

Fetch the complete documentation index at: https://gql.ch/llms.txt

Use this file to discover all available pages before exploring further.

A GQL query typically begins with the MATCH clause, which identifies a graph pattern to search for in the data. It is the foundation for querying nodes, relationships, and their properties. The MATCH clause specifies the structural shape of the subgraph being queried.
MATCH (u: User)−[:PERFORMED]−>(e: Event {activity: 'Submit'})
In this example, the pattern searches for users who performed an event labeled “Submit”. The nodes (u, e) are labeled with semantic roles, and the relationship [:PERFORMED] describes the interaction between them. Patterns can include multiple relationships and variable-length traversals using the {m,n} quantifier. For example, [:FOLLOWS]->{,3} matches up to 3 levels of consecutive FOLLOWS relationships. The MATCH clause is frequently used in combination with WHERE, RETURN, and WITH clauses to build complex queries.

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.