[ Web Proxy ]
URL:
Viewing: https://cloud.google.com/bigquery/docs/reference/standard-sql/graph-subqueries [Back]  [Original]

GQL subqueries  |  BigQuery  |  Google Cloud Documentation Skip to main content
Google Cloud Documentation [Google Cloud Documentation]
Send feedback

GQL subqueries Stay organized with collections Save and categorize content based on your preferences.

Preview

This product or feature is subject to the "Pre-GA Offerings Terms" in the General Service Terms section of the Service Specific Terms. Pre-GA products and features are available "as is" and might have limited support. For more information, see the launch stage descriptions.

Note: To provide feedback or request support for this feature, send an email to bq-graph-preview-support@google.com.

The following subqueries are supported in GQL query statements:

Subquery list

Name Summary
ARRAY subquery Subquery expression that produces an array.
EXISTS subquery Checks if a subquery produces at least one row.
IN subquery Checks if a subquery produces a specified value.
VALUE subquery Subquery expression that produces a scalar value.

ARRAY subquery

ARRAY { GRAPH graph_name gql_query_expr }

Description

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

Definitions

Return type

ARRAY<T>

Examples

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

In the following query, an array of transfer amounts is produced for each Account owned by each Person node:

GRAPH graph_db.FinGraph
MATCH (p:Person)-[:Owns]->(account:Account)
RETURN
 p.name, account.id AS account_id,
 ARRAY {
   GRAPH graph_db.FinGraph
   MATCH (a:Account)-[transfer:Transfers]->(:Account)
   WHERE a = account
   RETURN transfer.amount AS transfers
 } AS transfers;

/*-------------------------------+
 | name | account_id | transfers |
 +-------------------+-----------+
 | Alex | 7          | [300,100] |
 | Dana | 20         | [500,200] |
 | Lee  | 16         | [300]     |
 +-------------------------------*/

EXISTS subquery

EXISTS { GRAPH graph_name gql_query_expr }
EXISTS { match_statement }
EXISTS { graph_pattern }

Description

Checks if the subquery produces at least one row. Returns TRUE if at least one row is produced, otherwise returns FALSE. Never produces NULL. You can't use an EXISTS subquery in the WHERE clause of a path pattern. Instead, use a FILTER statement.

Definitions

Return type

BOOL

Examples

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

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

GRAPH graph_db.FinGraph
RETURN EXISTS {
  GRAPH graph_db.FinGraph
  MATCH (p:Person {Name: "Lee"})-[o:Owns]->(a:Account)
  RETURN p.Name
  LIMIT 1
} AS results;

/*---------+
 | results |
 +---------+
 | true    |
 +---------*/

You can include a MATCH statement or a graph pattern in an EXISTS subquery. The following examples include two ways to construct the subquery and produce similar results:

GRAPH graph_db.FinGraph
RETURN EXISTS {
  MATCH (p:Person {Name: "Lee"})-[o:Owns]->(a:Account)
} AS results;

/*---------+
 | results |
 +---------+
 | true    |
 +---------*/
GRAPH graph_db.FinGraph
RETURN EXISTS {
  (p:Person {Name: "Lee"})-[o:Owns]->(a:Account)
} AS results;

/*---------+
 | results |
 +---------+
 | true    |
 +---------*/

IN subquery

value [ NOT ] IN { GRAPH graph_name gql_query_expr }

Description

Checks if value is present in the subquery result. Returns TRUE if the result contains the value, otherwise returns FALSE.

Definitions

Details

The subquery result must have a single column and that column type must be comparable to the value type. If not, an error is returned. You can't use an IN subquery in the WHERE clause of a path pattern. Instead, use a FILTER statement.

Return type

BOOL

Examples

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

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

GRAPH graph_db.FinGraph
RETURN 'Dana' IN {
  GRAPH graph_db.FinGraph
  MATCH (p:Person)-[o:Owns]->(a:Account)
  RETURN p.name
} AS results;

/*---------+
 | results |
 +---------+
 | true    |
 +---------*/

VALUE subquery

VALUE { GRAPH graph_name gql_query_expr }

Description

A subquery expression that produces a scalar value.

Definitions

Details

The result of the subquery must have a single column. If the subquery returns more than one column, the query fails with an analysis error. The result type of the subquery expression is the produced column type. If the subquery produces exactly one row, that single value is the subquery expression result. If the subquery returns zero rows, the subquery expression result is NULL. If the subquery 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 called FinGraph.

The following query returns the name of any person whose country property is "Australia":

GRAPH graph_db.FinGraph
RETURN VALUE {
  GRAPH graph_db.FinGraph
  MATCH (p:Person {country: "Australia"})
  RETURN p.name
  LIMIT 1
} AS results;

/*---------+
 | results |
 +---------+
 | Alex    |
 +---------*/
Send feedback

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

Last updated 2026-08-11 UTC.

Need to tell us more? [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-08-11 UTC."],[],[]]

Web Proxy Viewer  |  New URL  |  Original Page