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 statement> ] [ <limit statement> ] [ <skip statement> ]
Details
- Each linear query must conclude with a
<result statement>
, which includes aRETURN
statement and may optionally includes theORDER BY
,LIMIT
, andSKIP
statements.
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 and binds data with variables for subsequent references. | WHERE , YIELD |
FILTER | Selects a subset of records from the working table. | |
LET | Defines variables and adds corresponding columns to the working table. | |
FOR | Unnest a list into individual rows by expanding the working table. | |
ORDER BY | Sorts records in the working table. | |
LIMIT | Restricts the number of records to be retained in the working table. | |
SKIP | Discards a specified number of records from the beginning of the working table. | |
CALL | Invokes a system-defined procedure or inline subquery. | YIELD |
RETURN | Specifies the columns to include in the output table. | GROUP BY |
Composite Query
A composite query combines a list of linear queries with query conjunctions.
For example, this is a composite query that uses UNION ALL
to combine the results of two linear queries:
MATCH (n:Club) RETURN n
UNION ALL
MATCH (n:User) RETURN n
Learn more about Composite Query →
Advanced Linear Composition with NEXT
The NEXT
statement can be used to chain multiple linear or composite query statements together, enabling more complex queries.
Learn more about NEXT →