Book Image

Neo4j Cookbook

By : Ankur goel, Ankur Goel
Book Image

Neo4j Cookbook

By: Ankur goel, Ankur Goel

Overview of this book

Table of Contents (17 chapters)
Neo4j Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Combining results with Cypher


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.

Getting ready

To work through this recipe, you will need to create the nodes and relationships for which data has been provided with the code files.

How to do it...

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 of UNION ALL:

    MATCH (n:Airport) RETURN n.name AS name UNION...