GQL operators

Graph Query Language (GQL) supports all GoogleSQLoperators,including the following GQL-specific operators:

Graph operators list

NameSummary
Graph concatenation operator Combines multiple graph paths into one and preserves the original order of the nodes and edges.
Graph logical operators Tests for the truth of a condition in a graph and produces eitherTRUE orFALSE.
Graph predicates Tests for the truth of a condition for a graph element and producesTRUE,FALSE, orNULL.
ALL_DIFFERENT predicate In a graph, checks to see if the elements in a list are mutually distinct.
IS DESTINATION predicateIn a graph, checks to see if a node is or isn't the destination of an edge.
IS LABELED predicateIn a graph, checks to see if a node or edge label satisfies a label expression.
IS SOURCE predicateIn a graph, checks to see if a node is or isn't the source of an edge.
PROPERTY_EXISTS predicateIn a graph, checks to see if a property exists for an element.
SAME predicate In a graph, checks if all graph elements in a list bind to the same node or edge.

Graph concatenation operator

graph_path||graph_path[||...]

Description

Combines multiple graph paths into one and preserves the original order of thenodes and edges.

Arguments:

  • graph_path: AGRAPH_PATH value that represents a graph path toconcatenate.

Details

This operator produces an error if the last node in the first path isn't thesame as the first node in the second path.

-- This successfully produces the concatenated path called `full_path`.MATCHp=(src:Account)-[t1:Transfers]->(mid:Account),q=(mid)-[t2:Transfers]->(dst:Account)LETfull_path=p||q
-- This produces an error because the first node of the path to be concatenated-- (mid2) isn't equal to the last node of the previous path (mid1).MATCHp=(src:Account)-[t1:Transfers]->(mid1:Account),q=(mid2:Account)-[t2:Transfers]->(dst:Account)LETfull_path=p||q

The first node in each subsequent path is removed from theconcatenated path.

-- The concatenated path called `full_path` contains these elements:-- src, t1, mid, t2, dst.MATCHp=(src:Account)-[t1:Transfers]->(mid:Account),q=(mid)-[t2:Transfers]->(dst:Account)LETfull_path=p||q

If anygraph_path isNULL, producesNULL.

Example

In the following query, a path calledp andq are concatenated. Notice thatmid is used at the end of the first path and at the beginning of thesecond path. Also notice that the duplicatemid is removed from theconcatenated path calledfull_path:

GRAPHFinGraphMATCHp=(src:Account)-[t1:Transfers]->(mid:Account),q=(mid)-[t2:Transfers]->(dst:Account)LETfull_path=p||qRETURNJSON_QUERY(TO_JSON(full_path)[0],'$.labels')ASelement_a,JSON_QUERY(TO_JSON(full_path)[1],'$.labels')ASelement_b,JSON_QUERY(TO_JSON(full_path)[2],'$.labels')ASelement_c,JSON_QUERY(TO_JSON(full_path)[3],'$.labels')ASelement_d,JSON_QUERY(TO_JSON(full_path)[4],'$.labels')ASelement_e,JSON_QUERY(TO_JSON(full_path)[5],'$.labels')ASelement_f/*-------------------------------------------------------------------------------------+ | element_a   | element_b     | element_c   | element_d     | element_e   | element_f | +-------------------------------------------------------------------------------------+ | ["Account"] | ["Transfers"] | ["Account"] | ["Transfers"] | ["Account"] |           | | ...         | ...           | ...         | ...           | ...         | ...       | +-------------------------------------------------------------------------------------/*

The following query produces an error because the last node forp mustbe the first node forq:

-- Error: `mid1` and `mid2` aren't equal.GRAPHFinGraphMATCHp=(src:Account)-[t1:Transfers]->(mid1:Account),q=(mid2:Account)-[t2:Transfers]->(dst:Account)LETfull_path=p||qRETURNTO_JSON(full_path)ASresults

The following query produces an error because the path calledp isNULL:

-- Error: a graph path is NULL.GRAPHFinGraphMATCHp=NULL,q=(mid:Account)-[t2:Transfers]->(dst:Account)LETfull_path=p||qRETURNTO_JSON(full_path)ASresults

Graph logical operators

GoogleSQL supports the following logical operators inelement pattern label expressions:

NameSyntaxDescription
NOT!X ReturnsTRUE ifX isn't included, otherwise, returnsFALSE.
ORX | Y ReturnsTRUE if eitherX orY is included, otherwise, returnsFALSE.
ANDX & Y ReturnsTRUE if bothX andY are included, otherwise, returnsFALSE.

Graph predicates

GoogleSQL supports the following graph-specific predicates ingraph expressions. A predicate can produceTRUE,FALSE, orNULL.

ALL_DIFFERENT predicate

ALL_DIFFERENT(element,element[,...])

Description

In a graph, checks to see if the elements in a list are mutually distinct.ReturnsTRUE if the elements are distinct, otherwiseFALSE.

Definitions

  • element: The graph pattern variable for a node or edge element.

Details

Produces an error ifelement isNULL.

Return type

BOOL

Examples

GRAPHFinGraphMATCH(a1:Account)-[t1:Transfers]->(a2:Account)-[t2:Transfers]->(a3:Account)-[t3:Transfers]->(a4:Account)WHEREa1.id <a4.idRETURNALL_DIFFERENT(t1,t2,t3)ASresults/*---------+ | results | +---------+ | FALSE   | | TRUE    | | TRUE    | +---------*/

IS DESTINATION predicate

nodeIS[NOT]DESTINATION[OF]edge

Description

In a graph, checks to see if a node is or isn't the destination of an edge.Can produceTRUE,FALSE, orNULL.

Arguments:

  • node: The graph pattern variable for the node element.
  • edge: The graph pattern variable for the edge element.

Examples

GRAPHFinGraphMATCH(a:Account)-[transfer:Transfers]-(b:Account)WHEREaISDESTINATIONoftransferRETURNa.idASa_id,b.idASb_id/*-------------+ | a_id | b_id | +-------------+ | 16   | 7    | | 16   | 7    | | 20   | 16   | | 7    | 20   | | 16   | 20   | +-------------*/
GRAPHFinGraphMATCH(a:Account)-[transfer:Transfers]-(b:Account)WHEREbISDESTINATIONoftransferRETURNa.idASa_id,b.idASb_id/*-------------+ | a_id | b_id | +-------------+ | 7    | 16   | | 7    | 16   | | 16   | 20   | | 20   | 7    | | 20   | 16   | +-------------*/

IS LABELED predicate

elementIS[NOT]LABELEDlabel_expression

Description

In a graph, checks to see if a node or edge label satisfies a labelexpression. Can produceTRUE,FALSE, orNULL ifelement isNULL.

Arguments:

  • element: The graph pattern variable for a graph node or edge element.
  • label_expression: The label expression to verify. For more information, seeLabel expression definition.

Examples

GRAPHFinGraphMATCH(a)WHEREaISLABELEDAccount|PersonRETURNa.idASa_id,LABELS(a)ASlabels/*----------------+ | a_id | labels  | +----------------+ | 1    | Person  | | 2    | Person  | | 3    | Person  | | 7    | Account | | 16   | Account | | 20   | Account | +----------------*/
GRAPHFinGraphMATCH(a)-[e]-(b:Account)WHEREeISLABELEDTransfers|OwnsRETURNa.Idasa_id,Labels(e)ASlabels,b.Idasb_idORDERBYa_id,b_id/*------+-----------------------+------+ | a_id | labels                | b_id | +------+-----------------------+------+ |    1 | [owns]                |    7 | |    2 | [owns]                |   20 | |    3 | [owns]                |   16 | |    7 | [transfers]           |   16 | |    7 | [transfers]           |   16 | |    7 | [transfers]           |   20 | |   16 | [transfers]           |    7 | |   16 | [transfers]           |    7 | |   16 | [transfers]           |   20 | |   16 | [transfers]           |   20 | |   20 | [transfers]           |    7 | |   20 | [transfers]           |   16 | |   20 | [transfers]           |   16 | +------+-----------------------+------*/
GRAPHFinGraphMATCH(a:Account{Id:7})OPTIONALMATCH(a)-[:OWNS]->(b)RETURNa.IdASa_id,b.IdASb_id,bISLABELEDAccountASb_is_account/*------+-----------------------+ | a_id | b_id   | b_is_account | +------+-----------------------+ | 7    | NULL   | NULL         | +------+-----------------------+*/

IS SOURCE predicate

nodeIS[NOT]SOURCE[OF]edge

Description

In a graph, checks to see if a node is or isn't the source of an edge.Can produceTRUE,FALSE, orNULL.

Arguments:

  • node: The graph pattern variable for the node element.
  • edge: The graph pattern variable for the edge element.

Examples

GRAPHFinGraphMATCH(a:Account)-[transfer:Transfers]-(b:Account)WHEREaISSOURCEoftransferRETURNa.idASa_id,b.idASb_id/*-------------+ | a_id | b_id | +-------------+ | 20   | 7    | | 7    | 16   | | 7    | 16   | | 20   | 16   | | 16   | 20   | +-------------*/
GRAPHFinGraphMATCH(a:Account)-[transfer:Transfers]-(b:Account)WHEREbISSOURCEoftransferRETURNa.idASa_id,b.idASb_id/*-------------+ | a_id | b_id | +-------------+ | 7    | 20   | | 16   | 7    | | 16   | 7    | | 16   | 20   | | 20   | 16   | +-------------*/

PROPERTY_EXISTS predicate

PROPERTY_EXISTS(element,element_property)

Description

In a graph, checks to see if a property exists for an element.Can produceTRUE,FALSE, orNULL.

Arguments:

  • element: The graph pattern variable for a node or edge element.
  • element_property: The name of the property to look for inelement.The property name must refer to a property in the graph. If the propertydoesn't exist in the graph, an error is produced. The property name isresolved in a case-insensitive manner.

Example

GRAPHFinGraphMATCH(n:Person|AccountWHEREPROPERTY_EXISTS(n,name))RETURNn.name/*------+ | name | +------+ | Alex | | Dana | | Lee  | +------*/

SAME predicate

SAME(element,element[,...])

Description

In a graph, checks if all graph elements in a list bind to the same node oredge. ReturnsTRUE if the elements bind to the same node or edge, otherwiseFALSE.

Arguments:

  • element: The graph pattern variable for a node or edge element.

Details

Produces an error ifelement isNULL.

Example

The following query checks to see ifa andb aren't the same person.

GRAPHFinGraphMATCH(src:Account)<-[transfer:Transfers]-(dest:Account)WHERENOTSAME(src,dest)RETURNsrc.idASsource_id,dest.idASdestination_id/*----------------------------+ | source_id | destination_id | +----------------------------+ | 7         | 20             | | 16        | 7              | | 16        | 7              | | 16        | 20             | | 20        | 16             | +----------------------------*/

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-15 UTC.