GQL subqueries

The following subqueries are supported in GQL query statements:

Subquery list

NameSummary
ARRAY subquerySubquery expression that produces an array.
EXISTS subqueryChecks if a subquery produces at least one row.
IN subqueryChecks if a subquery produces a specified value.
VALUE subquerySubquery expression that produces a scalar value.

ARRAY subquery

ARRAY{gql_query_expr}

Description

Subquery expression that produces an array. If the subquery produces zero rows,an empty array is produced. Never produces aNULL array. This can be usedwherever a query expression is supported in a GQL query statement.

Definitions

  • gql_query_expr: A GQL query expression.

Return type

ARRAY<T>

Examples

Note: The examples in this section reference a property graph calledFinGraph.

In the following query, an array of transfer amounts is produced for eachAccount owned by eachPerson node:

GRAPHFinGraphMATCH(p:Person)-[:Owns]->(account:Account)RETURNp.name,account.idASaccount_id,ARRAY{MATCH(a:Account)-[transfer:Transfers]->(:Account)WHEREa=accountRETURNtransfer.amountAStransfers}AStransfers;/*-------------------------------+ | name | account_id | transfers | +-------------------+-----------+ | Alex | 7          | [300,100] | | Dana | 20         | [500,200] | | Lee  | 16         | [300]     | +-------------------------------*/

EXISTS subquery

EXISTS{gql_query_expr}
EXISTS{match_statement}
EXISTS{graph_pattern}

Description

Checks if the subquery produces at least one row. ReturnsTRUE ifat least one row is produced, otherwise returnsFALSE. Never producesNULL.

Definitions

  • gql_query_expr: A GQL query expression.
  • match_statement: A pattern matching operation to perform on a graph. For more information, seeMATCH statement.
  • graph_pattern: A pattern to match in a graph. For more information, seegraph pattern definition.

Return type

BOOL

Examples

Note: The examples in this section reference a property graph calledFinGraph.

The following query checks whether any person named"Lee" owns anaccount. The subquery contains a graph query expression.

GRAPHFinGraphRETURNEXISTS{MATCH(p:Person{Name:"Lee"})-[o:Owns]->(a:Account)RETURNp.NameLIMIT1}ASresults;/*---------+ | results | +---------+ | true    | +---------*/

You can include aMATCH statement or a graph pattern in anEXISTSsubquery. The following examples include two ways to construct the subqueryand produce similar results:

GRAPHFinGraphRETURNEXISTS{MATCH(p:Person{Name:"Lee"})-[o:Owns]->(a:Account)}ASresults;/*---------+ | results | +---------+ | true    | +---------*/
GRAPHFinGraphRETURNEXISTS{(p:Person{Name:"Lee"})-[o:Owns]->(a:Account)}ASresults;/*---------+ | results | +---------+ | true    | +---------*/

IN subquery

value [ NOT ] IN{gql_query_expr}

Description

Checks ifvalue is present in the subquery result. ReturnsTRUE if theresult contains thevalue, otherwise returnsFALSE.

Definitions

  • value: The value look for in the subquery result.
  • IN:TRUE if the value is in the subquery result, otherwiseFALSE.
  • NOT IN:FALSE if the value is in the subquery result,otherwiseTRUE.
  • gql_query_expr: A GQL query expression.

Details

The subquery result must have a single column and that column type mustbe comparable to thevalue type. If not, an error is returned.

Return type

BOOL

Examples

Note: The examples in this section reference a property graph calledFinGraph.

The following query checks if'Dana' is a name of a person who owns anaccount.

GRAPHFinGraphRETURN'Dana'IN{MATCH(p:Person)-[o:Owns]->(a:Account)RETURNp.name}ASresults;/*---------+ | results | +---------+ | true    | +---------*/

VALUE subquery

VALUE{gql_query_expr}

Description

A subquery expression that produces a scalar value.

Definitions

  • gql_query_expr: A GQL query expression.

Details

The result of the subquery must have a single column. If the subquery returnsmore than one column, the query fails with an analysis error. The result type ofthe subquery expression is the produced column type. If the subquery producesexactly one row, that single value is the subquery expression result. If thesubquery returns zero rows, the subquery expression result isNULL. If thesubquery returns more than one row, the query fails with a runtime error.

Return type

The same as the column type in the subquery result.

Examples

Note: The examples in this section reference a property graph calledFinGraph.

The following query returns the name of any person whosecountry propertyis"Australia":

GRAPHFinGraphRETURNVALUE{MATCH(p:Person{country:"Australia"})RETURNp.nameLIMIT1}ASresults;/*---------+ | results | +---------+ | Alex    | +---------*/

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.