GQL within SQL

GoogleSQL for Spanner supports the following syntax to use GQLwithin SQL queries.

Language list

NameSummary
GRAPH_TABLE operator Performs an operation on a graph in theFROM clause of a SQL query and then produces a table with the results.

GRAPH_TABLE operator

FROM GRAPH_TABLE (property_graph_namemulti_linear_query_statement) [ [ AS ]alias ]

Description

Performs an operation on a graph in theFROM clause of a SQL query and thenproduces a table with the results.

With theGRAPH_TABLE operator, you can use theGQL syntaxto query a property graph. The result of this operation is produced as a table thatyou can use in the rest of the query.

Definitions

  • property_graph_name: The name of the property graph to query for patterns.
  • multi_linear_query_statement: You can use GQL to query a property graph forpatterns. For more information, seeGraph query language.
  • alias: An optional alias, which you can use to refer to the tableproduced by theGRAPH_TABLE operator elsewhere in the query.

Examples

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

You can use theRETURN statement to return specific node and edge properties.For example:

SELECTname,idFROMGRAPH_TABLE(FinGraphMATCH(n:Person)RETURNn.nameASname,n.idASid);/*-----------+ | name | id | +-----------+ | Alex | 1  | | Dana | 2  | | Lee  | 3  | +-----------*/

You can use theRETURN statement to produce output with graph patternvariables. These variables can be referenced outsideGRAPH_TABLE. For example,

SELECTn.name,n.idFROMGRAPH_TABLE(FinGraphMATCH(n:Person)RETURNn);/*-----------+ | name | id | +-----------+ | Alex | 1  | | Dana | 2  | | Lee  | 3  | +-----------*/

The following query produces an error becauseid isn'tincluded in theRETURN statement, even though this property exists forelementn:

SELECTname,idFROMGRAPH_TABLE(FinGraphMATCH(n:Person)RETURNn.name);

The following query produces an error because directly outputting the graphelementn is not supported. Convertn to its JSON representation using theSAFE_TO_JSON for successful output.

-- ErrorSELECTnFROMGRAPH_TABLE(FinGraphMATCH(n:Person)RETURNn);
SELECTSAFE_TO_JSON(n)asjson_nodeFROMGRAPH_TABLE(FinGraphMATCH(n:Person)RETURNn);/*---------------------------+ | json_node                 | +---------------------------+ | {"identifier":"mUZpbk...} | | {"identifier":"mUZpbk...} | | {"identifier":"mUZpbk...} | +--------------------------*/

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.