Performs an operation on a graph in the FROM 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 the FROM clause of a SQL query and then
produces a table with the results.
With the GRAPH_TABLE operator, you can use the GQL syntax
to query a property graph. The result of this operation is produced as a table that
you 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 for
patterns. For more information, see Graph query language.
alias: An optional alias, which you can use to refer to the table
produced by the GRAPH_TABLE operator elsewhere in the query.
Examples
You can use the RETURN 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 the RETURN statement to produce output with graph pattern
variables. These variables can be referenced outside GRAPH_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 because id isn't
included in the RETURN statement, even though this property exists for
element n:
The following query produces an error because directly outputting the graph
element n is not supported. Convert n to its JSON representation using the
SAFE_TO_JSON for successful output.
[[["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 2025-08-28 UTC."],[],[],null,["# GQL within SQL\n\nGoogleSQL for Spanner supports the following syntax to use GQL\nwithin SQL queries.\n\nLanguage list\n-------------\n\n`GRAPH_TABLE` operator\n----------------------\n\n```\nFROM GRAPH_TABLE (\n property_graph_name\n multi_linear_query_statement\n) [ [ AS ] alias ]\n```\n\n#### Description\n\nPerforms an operation on a graph in the `FROM` clause of a SQL query and then\nproduces a table with the results.\n\nWith the `GRAPH_TABLE` operator, you can use the [GQL syntax](/spanner/docs/reference/standard-sql/graph-query-statements)\nto query a property graph. The result of this operation is produced as a table that\nyou can use in the rest of the query.\n\n#### Definitions\n\n- `property_graph_name`: The name of the property graph to query for patterns.\n- `multi_linear_query_statement`: You can use GQL to query a property graph for patterns. For more information, see [Graph query language](/spanner/docs/reference/standard-sql/graph-query-statements).\n- `alias`: An optional alias, which you can use to refer to the table produced by the `GRAPH_TABLE` operator elsewhere in the query.\n\n#### Examples\n\n| **Note:** The examples in this section reference a property graph called [`FinGraph`](/spanner/docs/reference/standard-sql/graph-schema-statements#fin_graph).\n\nYou can use the `RETURN` statement to return specific node and edge properties.\nFor example: \n\n SELECT name, id\n FROM GRAPH_TABLE(\n FinGraph\n MATCH (n:Person)\n RETURN n.name AS name, n.id AS id\n );\n\n /*-----------+\n | name | id |\n +-----------+\n | Alex | 1 |\n | Dana | 2 |\n | Lee | 3 |\n +-----------*/\n\nYou can use the `RETURN` statement to produce output with graph pattern\nvariables. These variables can be referenced outside `GRAPH_TABLE`. For example, \n\n SELECT n.name, n.id\n FROM GRAPH_TABLE(\n FinGraph\n MATCH (n:Person)\n RETURN n\n );\n\n /*-----------+\n | name | id |\n +-----------+\n | Alex | 1 |\n | Dana | 2 |\n | Lee | 3 |\n +-----------*/\n\nThe following query produces an error because `id` isn't\nincluded in the `RETURN` statement, even though this property exists for\nelement `n`: \n\n SELECT name, id\n FROM GRAPH_TABLE(\n FinGraph\n MATCH (n:Person)\n RETURN n.name\n );\n\nThe following query produces an error because directly outputting the graph\nelement `n` is not supported. Convert `n` to its JSON representation using the\n`SAFE_TO_JSON` for successful output. \n\n -- Error\n SELECT n\n FROM GRAPH_TABLE(\n FinGraph\n MATCH (n:Person)\n RETURN n\n );\n\n SELECT SAFE_TO_JSON(n) as json_node\n FROM GRAPH_TABLE(\n FinGraph\n MATCH (n:Person)\n RETURN n\n );\n\n /*---------------------------+\n | json_node |\n +---------------------------+\n | {\"identifier\":\"mUZpbk...} |\n | {\"identifier\":\"mUZpbk...} |\n | {\"identifier\":\"mUZpbk...} |\n +--------------------------*/"]]