14.24. Union

14.24.1. Union two queries
14.24.2. Combine two queries and removing duplicates

Combining results from multiple queries is done through the UNION operator.

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.

Figure 14.11. Graph


14.24.1. Union two queries

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.

Result

1 ms

(empty result)


14.24.2. Combine two queries and removing duplicates

By not uncluding 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.

Result

1 ms

(empty result)