Source: https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/dql/linear-query/ Documentation version: v5.3.2 # 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 diagramsource 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](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/sessions/set/). For more information about the clause, see [USE clause](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/dql/clauses/use-graph/). ### `simple_query_statement` Performs a simple query. A simple query statement can be one of the following: - `match_statement` - `let_statement` - `for_statement` - `filter_statement` - `paging_statement` - `call_procedure_statement` #### `match_statement` Retrieves data from the working graph by pattern matching. For more information, see [MATCH](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/dql/match/). #### `let_statement` Declares variables that can be used in subsequent statements. For more information, see [LET](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/dql/let/). #### `for_statement` Iterates over a list of elements. For more information, see [FOR](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/dql/for/). #### `filter_statement` Selects a subset of the results produced by the preceding statement. For more information, see [FILTER](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/dql/filter/). #### `paging_statement` Divides a set of results into discrete subsets or pages. For more information, see [Paging](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/dql/order-by-page/). #### `call_procedure_statement` Calls a procedure. For more information, see [Call inline procedures](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/dql/call/call-inline-procedure/) and [Call named procedures](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/dql/call/call-procedure/). ### `primitive_result_statement` Returns the result of the query. For more information, see [Primitive result](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/dql/primitive-result/). ### `procedure_body` The body of a procedure. It cannot contain any DML or DDL statements. For more information, see [Procedure body](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/procedures/create/#procedure_body). ## Examples The following examples are based on the [example dataset](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/overview/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 | +-----+ ````