In GQL, a query is composed of one or more statements. Each statement is a self-contained unit that can be executed by the database. A typical GQL query begins with the MATCH
statement, which is used to retrieve data from the graph.
Furthermore, a clause is a component of a statement that performs a specific function, such as WHERE
for filtering. A clause on its own is not a complete instruction, but must be part of a statement.
Linear Query
A linear query refers to a query that is executed sequentially, where each statement is processed one after another without any branching or conditional logic. The result is returned in a straightforward progression.
<linear query statement> ::=
[ <query statement>... ] <result statement>
<query statement> ::=
<match statement>
| <filter statement>
| <let statement>
| <for statement>
| <order by statement>
| <limit statement>
| <skip statemenmt>
| <call statement>
<result statement> ::=
<return statement> [ <order by and page statement> ]
<order by and page statement> ::=
<order by statement>
| <order by statement> <skip statement>
| <order by statement> <limit statement>
| <order by statement> <skip statement> <limit statement>
| <skip statement>
| <skip statement> <limit clause>
| <limit clause>
Details
- Each linear query must conclude with a
<result statement>
, which includes a<return statement>
and may optionally includes an<order by and page statement>
.- The
<order by and page statement>
consists of standaloneORDER BY
,SKIP
, andLIMIT
statements or their combinations, which must appear in this order when combined.
- The
For example, this is a linear query where the MATCH
, FILTER
and RETURN
statements processed in a linear order:
MATCH (:User {_id: "U01"})-[:Follows]->(u:User)
FILTER u.city = "New York"
RETURN u
Query and Result Statements
Statement |
Description | Clauses |
---|---|---|
MATCH | Retrieves data from the graph using patterns. | WHERE , YIELD |
FILTER | Discards records in the intermediate result table that do not satisfy the specified conditions. | |
LET | Defines variables and adds corresponding columns to the intermediate result table. | |
FOR | Unnests a list into individual rows. | |
ORDER BY | Sorts records in the intermediate result or output table. | |
LIMIT | Restricts the number of records to be retained in the intermediate result or output table. | |
SKIP | Discards a specified number of records from the beginning of the intermediate result or output table. | |
CALL | Invokes an inline procedure or named procedure. | YIELD |
RETURN | Specifies the columns to include in the output table. | GROUP BY |
Composite Query
A composite query combines the result sets of multiple linear queries with query conjunctions (UNION
, EXCEPT
, INTERSECT
, and OTHERWISE
).
<composite query statement> ::=
<linear query statement> <query conjunction> <linear query statement>
[ { <query conjunction> <linear query statement> }... ]
<query conjunction> ::=
"UNION" [ "DISTINCT" | "ALL" ]
| "EXCEPT" [ "DISTINCT" | "ALL" ]
| "INTERSECT" [ "DISTINCT" | "ALL" ]
| "OTHERWISE"
For example, this is a composite query that uses UNION ALL
to combine the result sets of two linear queries:
MATCH (n:Club) RETURN n
UNION
MATCH (n:User) RETURN n
Learn more about Composite Query →
Advanced Linear Composition with NEXT
The NEXT
statement chains multiple linear or composite query statements, enabling more complex queries.
Learn more about NEXT →