GQL subqueries

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 { 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

  • gql_query_expr: A GQL query expression.

Return type

ARRAY<T>

Examples

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

GRAPH FinGraph
MATCH (p:Person)-[:Owns]->(account:Account)
RETURN
 p.name, account.id AS account_id,
 ARRAY {
   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 { 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.

Definitions

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

Return type

BOOL

Examples

The following query checks whether any Person whose name starts with the letter 'D' owns an Account. The subquery contains a graph query expression.

GRAPH FinGraph
RETURN EXISTS {
  MATCH (p:Person)-[o:Owns]->(a:Account)
  WHERE p.Name LIKE 'D%'
  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 FinGraph
RETURN EXISTS {
  MATCH (p:Person)-[o:Owns]->(a:Account)
  WHERE p.Name LIKE 'D%'
} AS results;

/*---------+
 | results |
 +---------+
 | true    |
 +---------*/
GRAPH FinGraph
RETURN EXISTS {
  (p:Person)-[o:Owns]->(a:Account) WHERE p.Name LIKE 'D%'
} AS results;

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

IN subquery

value [ NOT ] IN { 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

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

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.

Return type

BOOL

Examples

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

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

/*---------+
 | 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 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

The following query returns a the name of any Person whose name contains the character 'e':

GRAPH FinGraph
RETURN VALUE {
  MATCH (p:Person)
  WHERE p.name LIKE '%e%'
  RETURN p.name
  LIMIT 1
} AS results;

/*---------+
 | results |
 +---------+
 | [Alex]  |
 +---------*/