Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Graph Data Processing with Cypher

You're reading from   Graph Data Processing with Cypher A practical guide to building graph traversal queries using the Cypher syntax on Neo4j

Arrow left icon
Product type Paperback
Published in Dec 2022
Publisher Packt
ISBN-13 9781804611074
Length 332 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Ravindranatha Anthapu Ravindranatha Anthapu
Author Profile Icon Ravindranatha Anthapu
Ravindranatha Anthapu
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Part 1: Cypher Introduction
2. Chapter 1: Introduction to Neo4j and Cypher FREE CHAPTER 3. Chapter 2: Components of Cypher 4. Part 2: Working with Cypher
5. Chapter 3: Loading Data with Cypher 6. Chapter 4: Querying Graph 7. Chapter 5: Filtering, Sorting, and Aggregations 8. Chapter 6: List Expressions, UNION, and Subqueries 9. Part 3: Advanced Cypher Concepts
10. Chapter 7: Working with Lists and Maps 11. Chapter 8: Advanced Query Patterns 12. Chapter 9: Query Tuning 13. Chapter 10: Using APOC Utilities 14. Chapter 11: Cypher Ecosystem 15. Chapter 12: Tips and Tricks 16. Index 17. Other Books You May Enjoy

Working with UNION in Cypher

The UNION clause combines the results of two or more queries and returns the results. It works pretty similarly to how it works in SQL queries. Normally, we use the UNION clause when we want to combine the results of multiple, disparate queries returning similar datasets.

Let’s look at an example usage of the UNION clause:

MATCH (p:Patient)-[:HAS_ENCOUNTER]->()-[:HAS_DIAGNOSIS]->(d)
WHERE p.id='f237e253-9052-a038-7c9e-dbd9a1d7da32'
RETURN d.code as drug
UNION
MATCH (p:Patient)-[:HAS_ENCOUNTER]->()-[:HAS_DIAGNOSIS]->(d)
WHERE p.id='ffa580de-08e5-9a47-b12a-db312ad6825b'
RETURN d.code as drug

This query returns the diagnosis codes used among two patients, as shown in the following figure:

Figure 6.13 – Usage of the UNION clause

We can see there are five records returned. The UNION clause eliminates duplicate records in the results if there are any.

We could have written the...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime