UNION

Introduction

UNION combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union.

The number and the names of the columns must be identical in all queries combined by using UNION.

To keep all the result rows, use UNION ALL. Using just UNION will combine and remove duplicates from the result set.

Graph
  N0 [
    label = "{Actor|name = \'Anthony Hopkins\'\l}"
  ]
  N0 -> N3 [
    color = "#2e3436"
    fontcolor = "#2e3436"
    label = "ACTS_IN\n"
  ]
  N0 -> N1 [
    color = "#4e9a06"
    fontcolor = "#4e9a06"
    label = "KNOWS\n"
  ]
  N1 [
    label = "{Actor|name = \'Helen Mirren\'\l}"
  ]
  N1 -> N3 [
    color = "#2e3436"
    fontcolor = "#2e3436"
    label = "ACTS_IN\n"
  ]
  N2 [
    label = "{Actor|name = \'Hitchcock\'\l}"
  ]
  N3 [
    label = "{Movie|title = \'Hitchcock\'\l}"
  ]

Combine two queries and retain duplicates

Combining the results from two queries is done using UNION ALL.

Query
MATCH (n:Actor)
RETURN n.name AS name
UNION ALL MATCH (n:Movie)
RETURN n.title AS name

The combined result is returned, including duplicates.

Table 1. Result
name

"Anthony Hopkins"

"Helen Mirren"

"Hitchcock"

"Hitchcock"

4 rows

Combine two queries and remove duplicates

By not including ALL in the UNION, duplicates are removed from the combined result set

Query
MATCH (n:Actor)
RETURN n.name AS name
UNION
MATCH (n:Movie)
RETURN n.title AS name

The combined result is returned, without duplicates.

Table 2. Result
name

"Anthony Hopkins"

"Helen Mirren"

"Hitchcock"

3 rows