UltipaDocs
Products
Solutions
Resources
Company
Start Free Trial
UltipaDocs
Start Free Trial
  • Introduction
  • GQL vs Other Languages
    • Overview
    • Node and Edge Patterns
    • Path Patterns
    • Quantified Paths
    • Questioned Paths
    • Shortest Paths
    • Cheapest Paths
    • K-Hop Traversal
    • Graph Patterns
    • Overview
    • Open Graphs
    • Closed Graphs
    • Graphs with Edge ID
    • Graph Types
    • Constraints
    • Projections
    • Storage Maintenance
    • Unique Identifiers
    • INSERT
    • INSERT OVERWRITE
    • UPSERT
    • MERGE
    • SET
    • REMOVE
    • DELETE
    • FOREACH
    • Query Composition
    • Result Table and Visualization
    • MATCH
    • OPTIONAL MATCH
    • FILTER
    • LET
    • FOR
    • ORDER BY
    • LIMIT
    • SKIP
    • CALL
    • RETURN
    • Composite Query
    • NEXT
    • All Functions
    • Element Functions
    • Path Functions
    • Aggregate Functions
    • Mathematical Functions
    • Trigonometric Functions
    • String Functions
    • List Functions
    • Datetime Functions
    • Spatial Functions
    • Null Functions
    • Utility Functions
    • Type Conversion Functions
    • Table Functions
    • Database Functions
  • Operators
  • Predicates
    • CASE
    • LET Value Expression
    • Value Query Expression
    • List Expressions
    • Index
    • Full-text Index
    • Vector Index
  • Transactions
  • Triggers
  • Query Management
  • Execution Plan
  • Backup and Restore
    • Variables
    • Values and Types
    • Comments
    • Reserved Words
    • Naming Conventions
    • 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 ConjunctionDescription
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.
  • 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

GQL
INSERT (rowlock:User {_id:'U01', name:'rowlock'}),
       (brainy:User {_id:'U02', name:'Brainy'}),
       (purplechalk:User {_id:'U03', name:'purplechalk'}),
       (mochaeach:User {_id:'U04', name:'mochaeach'}),
       (lionbower:User {_id:'U05', name:'lionbower'}),
       (c01:Club {_id:'C01'}),
       (c02:Club {_id:'C02'}),
       (rowlock)-[:Follows]->(brainy),
       (brainy)-[:Follows]->(rowlock),
       (mochaeach)-[:Follows]->(brainy),
       (brainy)-[:Follows]->(purplechalk),
       (purplechalk)-[:Follows]->(brainy),
       (brainy)-[:Joins]->(c01),
       (lionbower)-[:Joins]->(c01),
       (mochaeach)-[:Joins]->(c02)

UNION

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

Result:

JSON
[
  {"id": "C02", "labels": ["Club"], "properties": {}},
  {"id": "C01", "labels": ["Club"], "properties": {}},
  {"id": "U01", "labels": ["User"], "properties": {"name": "rowlock"}},
  {"id": "U02", "labels": ["User"], "properties": {"name": "Brainy"}},
  {"id": "U03", "labels": ["User"], "properties": {"name": "purplechalk"}},
  {"id": "U04", "labels": ["User"], "properties": {"name": "mochaeach"}},
  {"id": "U05", "labels": ["User"], "properties": {"name": "lionbower"}}
]

UNION ALL

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

Result:

JSON
[
  {"id": "C02", "labels": ["Club"], "properties": {}},
  {"id": "C01", "labels": ["Club"], "properties": {}},
  {"id": "U01", "labels": ["User"], "properties": {"name": "rowlock"}},
  {"id": "U02", "labels": ["User"], "properties": {"name": "Brainy"}},
  {"id": "U03", "labels": ["User"], "properties": {"name": "purplechalk"}},
  {"id": "U04", "labels": ["User"], "properties": {"name": "mochaeach"}},
  {"id": "U05", "labels": ["User"], "properties": {"name": "lionbower"}},
  {"id": "C02", "labels": ["Club"], "properties": {}},
  {"id": "C01", "labels": ["Club"], "properties": {}}
]

EXCEPT

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

Result:

JSON
[
  {"id": "U04", "labels": ["User"], "properties": {"name": "mochaeach"}},
  {"id": "U03", "labels": ["User"], "properties": {"name": "purplechalk"}},
  {"id": "U01", "labels": ["User"], "properties": {"name": "rowlock"}}
]

EXCEPT ALL

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

Result:

JSON
[
  {"id": "U01", "labels": ["User"], "properties": {"name": "rowlock"}},
  {"id": "U03", "labels": ["User"], "properties": {"name": "purplechalk"}},
  {"id": "U04", "labels": ["User"], "properties": {"name": "mochaeach"}},
  {"id": "U03", "labels": ["User"], "properties": {"name": "purplechalk"}},
  {"id": "U01", "labels": ["User"], "properties": {"name": "rowlock"}}
]

INTERSECT

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

Result:

JSON
[
  {"id": "U02", "labels": ["User"], "properties": {"name": "Brainy"}}
]

INTERSECT ALL

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

Result:

JSON
[
  {"id": "U02", "labels": ["User"], "properties": {"name": "Brainy"}},
  {"id": "U02", "labels": ["User"], "properties": {"name": "Brainy"}}
]

OTHERWISE

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

Result:

JSON
[
  {"id": "U01", "labels": ["User"], "properties": {"name": "rowlock"}},
  {"id": "U03", "labels": ["User"], "properties": {"name": "purplechalk"}},
  {"id": "U04", "labels": ["User"], "properties": {"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