UltipaDocs
Try Playground
  • Introduction
  • GQL vs Other Languages
    • Overview
    • Node and Edge Patterns
    • Path Patterns
    • Quantified Paths
    • Shortest Paths
    • Graph Patterns
    • Overview
    • Closed Graph
    • Open Graph
    • Graph Sharding and Storage
    • Constraints
    • Unique Identifiers
    • INSERT
    • INSERT OVERWRITE
    • UPSERT
    • SET
    • REMOVE
    • DELETE
    • Query Composition
    • Result Table and Visualization
    • MATCH
    • OPTIONAL MATCH
    • FILTER
    • LET
    • FOR
    • ORDER BY
    • LIMIT
    • SKIP
    • CALL
    • RETURN
    • Composite Query
    • NEXT
    • All Functions
    • Scalar Functions
    • Path Functions
    • Aggregate Functions
    • Mathematical Functions
    • Trigonometric Functions
    • String Functions
    • List Functions
    • Datetime Functions
    • Spatial Functions
    • Type Conversion Functions
    • Table Functions
    • AI & Vector Functions
    • Database Functions
  • Operators
  • Predicates
  • Expressions
    • Index
    • Full-text Index
    • Vector Index
  • Transactions
  • Triggers
  • Query Management
    • Variables
    • Values and Types
    • Comments
    • Reserved Words
    • Syntactic Notation
  • GQL Conformance
  1. Docs
  2. /
  3. ISO GQL
  4. /
  5. Querying

Composite Query

Overview

A composite query combines the result sets of multiple linear queries using the following query conjunctions:

Query Conjunction
Description
UNIONReturns distinct records from all result sets.
UNION ALLReturns all records from all result sets.
EXCEPTReturns distinct records in the 1st result set that do not appear in others.
EXCEPT ALLReturns all records in the 1st result set that do not appear in others.
INTERSECTReturns distinct records that appear in all result sets.
INTERSECT ALLReturns all records that appear in all result sets.
OTHERWISEReturns the first non-empty result set, in order of appearance.

Details

  • UNION, EXCEPT, and INTERSECT perform deduplication on the final result set by default. They are equivalent to UNION DISTINCT, EXCEPT DISTINCT, and INTERSECT DISTINCT.
  • Different query conjunctions can be used within a composite query statement.

To combine the result sets of multiple linear queries, the RETURN statements in all linear queries include the same number of return items, in the same order and with the same names. Each return item with the same name must also have the same type.

Example Graph

CREATE GRAPH myGraph { 
  NODE User ({name string}),
  NODE Club (),
  EDGE Follows ()-[{}]->(),
  EDGE Joins ()-[{}]->()
} PARTITION BY HASH(Crc32) SHARDS [1]

UNION

GQL
MATCH (n:Club) RETURN n
UNION
MATCH (n) RETURN n

Result: n

_id_uuidschema
values
C02Sys-genClub
C01Sys-genClub
U05Sys-genUser{name: "lionbower"}
U04Sys-genUser{name: "mochaeach"}
U03Sys-genUser{name: "purplechalk"}
U02Sys-genUser{name: "Brainy"}
U01Sys-genUser{name: "rowlock"}

UNION ALL

GQL
MATCH (n:Club) RETURN n
UNION ALL
MATCH (n) RETURN n

Result: n

_id_uuidschema
values
C02Sys-genClub
C01Sys-genClub
U05Sys-genUser{name: "lionbower"}
U04Sys-genUser{name: "mochaeach"}
U03Sys-genUser{name: "purplechalk"}
U02Sys-genUser{name: "Brainy"}
U01Sys-genUser{name: "rowlock"}
C02Sys-genClub
C01Sys-genClub

EXCEPT

GQL
MATCH ({_id: "U02"})-(n) RETURN n
EXCEPT
MATCH ({_id: "U05"})-(n) RETURN n

Result: n

_id_uuidschema
values
U04Sys-genUser{name: "mochaeach"}
U03Sys-genUser{name: "purplechalk"}
U01Sys-genUser{name: "rowlock"}

EXCEPT ALL

GQL
MATCH ({_id: "U02"})-(n) RETURN n
EXCEPT ALL
MATCH ({_id: "U05"})-(n) RETURN n

Result: n

_id_uuidschema
values
U01Sys-genUser{name: "rowlock"}
U03Sys-genUser{name: "purplechalk"}
U04Sys-genUser{name: "mochaeach"}
U03Sys-genUser{name: "purplechalk"}
U01Sys-genUser{name: "rowlock"}

INTERSECT

GQL
MATCH ({_id: "U01"})-(u:User) RETURN u
INTERSECT
MATCH ({_id: "U03"})-(u:User) RETURN u

Result: u

_id_uuidschema
values
U02Sys-genUser{name: "Brainy"}

INTERSECT ALL

GQL
MATCH ({_id: "U01"})-(u:User) RETURN u
INTERSECT ALL
MATCH ({_id: "U03"})-(u:User) RETURN u

Result: u

_id_uuidschema
values
U02Sys-genUser{name: "Brainy"}
U02Sys-genUser{name: "Brainy"}

OTHERWISE

GQL
MATCH ({_id: "U04"})<-[]-(u:User) RETURN u
OTHERWISE
MATCH ({_id: "U02"})<-[]-(u:User) RETURN u

Result: u

_id_uuidschema
values
U01Sys-genUser{name: "rowlock"}
U03Sys-genUser{name: "purplechalk"}
U04Sys-genUser{name: "mochaeach"}

In this example, the result set of the first linear query returns a null value due to the usage of OPTIONAL:

GQL
OPTIONAL MATCH ({_id: "U04"})<-[]-(u:User) RETURN u
OTHERWISE
MATCH ({_id: "U02"})<-[]-(u:User) RETURN u

Result:

u
null

Renaming Return Items

You may use the AS keyword to rename return items to ensure that the results of linear queries can be combined.

GQL
MATCH ({_id: "C01"})<-(u) RETURN u.name, 1 AS Club
UNION
MATCH ({_id: "C02"})<-(u) RETURN u.name, 2 AS Club

Result:

u.nameClub
Brainy1
lionbower1
mochaeach2

Using Different Query Conjunctions

GQL
MATCH (n:Club) RETURN n._id
OTHERWISE
MATCH (n) RETURN n._id
UNION ALL
MATCH (n)-[]->(:Club) RETURN n._id

Result:

n._id
C01
C02
U05
U04
U02

Deduplicating Multiple Return Items

When the RETURN statements contain multiple return items, DISTINCT removes duplicate records based on the combined values of all return items.

GQL
MATCH (u1 {name: "rowlock"})-(u2:User) RETURN u1.name, u2.name
UNION DISTINCT
MATCH (u1 {name: "purplechalk"})-(u2:User) RETURN u1.name, u2.name

Result:

u1.nameu2.name
rowlockBrainy
purplechalkBrainy

You may compare the results returned by UNION ALL:

GQL
MATCH (u1 {name: "rowlock"})-(u2:User) RETURN u1.name, u2.name
UNION ALL
MATCH (u1 {name: "purplechalk"})-(u2:User) RETURN u1.name, u2.name

Result:

u1.nameu2.name
rowlockBrainy
rowlockBrainy
purplechalkBrainy
purplechalkBrainy