A Gentle Introduction to Cypher Queries in Neo4j

Graph databases are very powerful tools to store entities and its relations. Those data stores also offer to reveal some complex relations that human beings cannot see with eyes. They actually do not offer anything you cannot do with relational databases and sql but they offer you to do same functionalities with simplified way. In this post, we are going to understand and run some cypher queries for a pre-built graph in neo4j.

Graph of Thrones!
Vlog

You can either follow this tutorial or watch the following video. They both cover the cypher queries in neo4j graph database on a pre-built game of thrones graph.


🙋‍♂️ You may consider to enroll my top-rated machine learning course on Udemy

Decision Trees for Machine Learning

PS: please like the video and do not forget to subscribe to the channel if you enjoy the video

Neo4j sandbox

Sandbox is the cloud tool of neo4j. You don’t have to install neo4j server on your environment and also download any datasets. If you want to use your own neo4j server, then you can skip to next section.

Please visit sandbox.neo4j.com. Then, select the graph data science under pre built data and click launch project.

Launching a project

Then, you will see your graph data science project in the console when launch is over.

Listing existing projects

Click the green button to Open the graph data science project in the browser.

Initial graph

If you are going to click the all badge in the relationship types, a summary of the graph will be shown.

Graph summary

Yes, it is the graph of game of thrones!

Running some queries

Notice that nodes are represented with parentheses and edges are represented with bracket. We can specify the type of node or edge in the brackets.





House, person, battle and many others are represented as nodes in the graph.

Let’s find out who are the members of Stark household. House is a node. That’s why, we will write it as parentheses. Type of the node is house. So, we will mention the node type as House in the parentheses. Besides, houses and persons are nodes and they are connected with BELONGS_TO labels. So, edges will be written with brackets and its type will be BELONGS_TO.


MATCH p=(h1:House {name: "Stark"})-[r1:BELONGS_TO]-()
RETURN p

Members of the House Stark

Eddark Stark, Arya Stark, Jon Snow and many others seem to be the member of Stark House.

Now, Let’s learn the battles Starks defended. The both houses and battles are nodes. We will write them in the parentheses. Types of nodes will be house and battle respectively. Those nodes will be connected with edges having DEFENDER labels.


MATCH p=(h1:House {name: "Stark"})-[r1:DEFENDER]-(b1:Battle)
RETURN p

Battles that Starks defend

Starks defended their house in 8 battles.

What if I ask who is the attackers of the battles that Starks defend. We will add and extra node for the house node. It will be connected to the battle and its edge should have an ATTACKER label.


MATCH p=(h1:House {name: "Stark"})-[r1:DEFENDER]-(b1:Battle)-[r2:ATTACKER]-(h2:House) 
RETURN p

Attackers of the battle that Starks defend

Notice that we can store those relations in the regular relational databases but querying it with sql requires nested and very complex scripts.

It seems that Greyjoys attacked the most against Starks. There are 6 battles between those houses. On the other hand, Lannisters attacked Starks just once.

We can get the battles between houses in tabular format.


MATCH p=(h1:House {name: "Stark"})-[r1:DEFENDER]-(b1:Battle)-[r2:ATTACKER]-(h2:House)
RETURN h1.name as defender, h2.name as attacker, count(b1) as count

Battles between houses in tabular form
Creating a graph from scracth

We have used a pre-built graph in this post. If you wonder how to create a graph from scratch, this post might attract your attention: Deep Face Recognition with Neo4j.





Conclusion

So, we have mentioned cypher queries for a pre-built game of thrones graph in neo4j. I hope using a funny data set makes it understandable.

Cypher in neo4j actually does not offer anything you cannot do with sql but it avoids you to write very complex and nested queries. Simplified queries make it more understandable you won’t do any mistakes.


Like this blog? Support me on Patreon

Buy me a coffee


1 Comment

Comments are closed.