Linear query statement¶
The linear query statement consists of a linear composition of simple query statements, which are executed sequentially to produce a single result set.
Syntax¶
- linear_query_statement
- simple_query_statement
linear_query_statement ::=
((use_graph_clause simple_query_statement+)+)? use_graph_clause simple_query_statement+ primitive_result_statement
| use_graph_clause primitive_result_statement
| ( simple_query_statement+)? primitive_result_statement
| use_graph_clause? '{' procedure_body '}'
simple_query_statement ::=
match_statement
| let_statement
| for_statement
| filter_statement
| paging_statement
| call_procedure_statement
Parameters¶
use_graph_clause¶
Declares the working graph of the query. This clause is required only if the query involves a graph and no home graph is set in the current session. For how to set the home graph, see SESSION SET.
For more information about the clause, see USE clause.
simple_query_statement¶
Performs a simple query. A simple query statement can be one of the following:
match_statementlet_statementfor_statementfilter_statementpaging_statementcall_procedure_statement
match_statement¶
Retrieves data from the working graph by pattern matching. For more information, see MATCH.
let_statement¶
Declares variables that can be used in subsequent statements. For more information, see LET.
for_statement¶
Iterates over a list of elements. For more information, see FOR.
filter_statement¶
Selects a subset of the results produced by the preceding statement. For more information, see FILTER.
paging_statement¶
Divides a set of results into discrete subsets or pages. For more information, see Paging.
call_procedure_statement¶
Calls a procedure. For more information, see Call inline procedures and Call named procedures.
primitive_result_statement¶
Returns the result of the query. For more information, see Primitive result.
procedure_body¶
The body of a procedure. It cannot contain any DML or DDL statements. For more information, see Procedure body.
Examples¶
The following examples are based on the example dataset.
Perform a linear query with the USE clause¶
'''
USE movie
MATCH (v@`User`{id: 123})-[e:Watch]->(m:Movie)
FILTER WHERE e.rate > 3
RETURN m.name AS movie, e.rate AS rate
ORDER BY rate DESC
'''
The preceding statement retrieves the movies watched by the person with an ID of 123 and filters the movies with a rating greater than 3. It then returns the movie names and their ratings in descending order.
Return:
+-----------------------+------+
| movie | rate |
+-----------------------+------+
| Star Wars | 4.0 |
| Star Wars: A New Hope | 4.0 |
| City of God | 3.5 |
+-----------------------+------+
Perform a linear query without the USE clause¶
'''
LET nums = LIST[1, 2, 3, 4, 5]
FOR i IN nums
FILTER i % 2 = 1
RETURN i AS odd
ORDER BY odd DESC
'''
Return:
+-----+
| odd |
+-----+
| 5 |
| 3 |
| 1 |
+-----+
Chain two linear segments with NEXT¶
'''
FOR i IN RANGE(1, 8)
RETURN i AS num
NEXT
FILTER num >= 5
RETURN num
ORDER BY num
'''
Return:
+-----+
| num |
+-----+
| 5 |
| 6 |
| 7 |
| 8 |
+-----+