The UNION
operator can be used to combine the results from two separate queries. This operator is very useful to combine the results of two altogether different queries, which have no common match pattern among them. In this recipe, we will learn the use of the UNION
operator via examples.
To work through this recipe, you will need to create the nodes and relationships for which data has been provided with the code files.
We have divided this recipe into the following problem sets:
Getting the details: Let's get name of all the airports and flight numbers existing in our graph database:
MATCH (n:Airport) RETURN n.name AS name UNION ALL MATCH (n:Flight) RETURN n.flight_number AS name
The following screenshot shows the Neo4j console depicting the union result of two separate queries:
Removing duplicates from the query: For removing duplicates from the query,
UNION
is specified instead ofUNION ALL
:MATCH (n:Airport) RETURN n.name AS name UNION...