UltipaDocs
Products
Solutions
Resources
Company
Start Free Trial
UltipaDocs
Start Free Trial
  • Introduction
  • GQL vs Other Languages
    • Overview
    • Node and Edge Patterns
    • Path Patterns
    • Quantified Paths
    • Questioned Paths
    • Shortest Paths
    • Cheapest Paths
    • K-Hop Traversal
    • Graph Patterns
    • Overview
    • Open Graphs
    • Closed Graphs
    • Graphs with Edge ID
    • Graph Types
    • Constraints
    • Projections
    • Storage Maintenance
    • Unique Identifiers
    • INSERT
    • INSERT OVERWRITE
    • UPSERT
    • MERGE
    • SET
    • REMOVE
    • DELETE
    • FOREACH
    • Query Composition
    • Result Table and Visualization
    • MATCH
    • OPTIONAL MATCH
    • FILTER
    • LET
    • FOR
    • ORDER BY
    • LIMIT
    • SKIP
    • CALL
    • RETURN
    • Composite Query
    • NEXT
    • All Functions
    • Element Functions
    • Path Functions
    • Aggregate Functions
    • Mathematical Functions
    • Trigonometric Functions
    • String Functions
    • List Functions
    • Datetime Functions
    • Spatial Functions
    • Null Functions
    • Utility Functions
    • Type Conversion Functions
    • Table Functions
    • Database Functions
  • Operators
  • Predicates
    • CASE
    • LET Value Expression
    • Value Query Expression
    • List Expressions
    • Index
    • Full-text Index
    • Vector Index
  • Transactions
  • Triggers
  • Query Management
  • Execution Plan
  • Backup and Restore
    • Variables
    • Values and Types
    • Comments
    • Reserved Words
    • Naming Conventions
    • Syntactic Notation
  • GQL Conformance
  1. Docs
  2. /
  3. ISO GQL
  4. /
  5. Querying

NEXT

Overview

The NEXT statement chains multiple linear or composite query statements, where the result of one query is carried forward to the next. This allows for advanced linear composition, enabling more complex queries.

Syntax
<advanced linear query> ::= <query> <next statement> <query> ...

<next statement> ::= "NEXT" [ <yield clause> ]
          
<yield clause> ::= "YIELD" <yield item> [ { "," <yield item> }... ]

<yield item> ::= <column name> [ "AS" <binding variable> ]

Details

  • The conclusion of each <query> must contain a RETURN statement, specifying the variables that can be referenced in the <query> immediately after NEXT.
  • The NEXT statement supports the YIELD clause. See NEXT YIELD.

Example Graph

GQL
INSERT (rowlock:User {_id: 'U01', name: 'rowlock'}),
       (brainy:User {_id: 'U02', name: 'Brainy'}),
       (purplechalk:User {_id: 'U03', name: 'purplechalk'}),
       (mochaeach:User {_id: 'U04', name: 'mochaeach'}),
       (lionbower:User {_id: 'U05', name: 'lionbower'}),
       (c01:Club {_id: 'C01'}),
       (c02:Club {_id: 'C02'}),
       (rowlock)-[:Follows]->(brainy),
       (mochaeach)-[:Follows]->(brainy),
       (purplechalk)-[:Follows]->(mochaeach),
       (purplechalk)-[:Follows]->(lionbower),
       (brainy)-[:Joins {memberNo: 1}]->(c01),
       (lionbower)-[:Joins {memberNo: 2}]->(c01),
       (mochaeach)-[:Joins {memberNo: 9}]->(c02)

Basic Usage

This query returns users who are members of C01 while also followed by U03:

GQL
MATCH ({_id: "C01"})<-[:Joins]-(u1:User)
RETURN u1
NEXT
MATCH ({_id: "U03"})-[:Follows]->(u2:User) WHERE u2 = u1
RETURN u2

Result:

_idlabelname
U05Userlionbower

This query throws error since name is out of scope:

GQL - Error Occurs
LET name = "rowlock"
RETURN name
NEXT
MATCH ({_id: "C01"})<-[:Joins]-(u:User)
RETURN u
NEXT
RETURN name IN collect_list(u.name)

Using Grouped Result

This query returns the names of users who joined the club with the largest number of members:

GQL
MATCH (c:Club)<-[:Joins]-()
RETURN c, count(c) AS cnt GROUP BY c
ORDER BY cnt DESC LIMIT 1
NEXT
MATCH (c)<-[:Joins]-(u)
RETURN c._id, collect_list(u.name)

Result:

c._idcollect_list(u.name)
C01["Brainy","lionbower"]

Using Aggregated Result

This query inserts a new Joins edge from U01 to C01 and sets the memberNo property to the next highest value:

GQL
MATCH ({_id: "C01"})<-[e1:Joins]-()
RETURN max(e1.memberNo) AS maxNo
NEXT
MATCH (u {_id: "U01"}), (c {_id: "C01"})
INSERT (c)<-[e2:Joins {memberNo: maxNo + 1}]-(u)
RETURN e2

Result:

_id_from_tolabelmemberNo
e:9U01C01Joins3

NEXT YIELD

The YIELD clause can be used to select specific variables from the previous query statement and rename them if necessary, making them accessible for reference in the next query statement. Variables not selected with YIELD will no longer be available. If the YIELD clause is omitted, all variables bound in the previous query statement are passed through by default.

This query finds clubs joined by users followed by purplechalk:

GQL
LET name = "purplechalk"
MATCH (:User {name: name})-[:Follows]->(u:User)
RETURN *
NEXT YIELD u
MATCH (u)-[:Joins]->(c:Club)
RETURN u.name, c._id

Result:

u.namec._id
mochaeachC02
lionbowerC01