Diving into Neo4j and Its Query Language - Cypher - Digital Solutions, IT Services & Consulting - Payoda

Diving into Neo4j and Its Query Language - Cypher

We strongly encourage you to know why one needs graph databases and how they can benefit you. Would you please read the previous article — What and Why of Graph Databases to know more about graph databases?

Hopping back to this article, we will learn about the popular Neo4j graph database and its query language — Cypher.



Neo4j is probably the most common graph database that you’re going to run into. Neo4j supports a query language called Cypher.

A Neo4j graph has four components :

  • Nodes: Nodes represent the object or entities. These can be labeled.
  • Relationships: Nodes can have multiple relationships.
  • Labels: Labels are used to categorize the nodes. A relationship is a connection between any two nodes. These must have a type and a direction. Types are similar to labels since these provide a general category for each relationship.
  • Properties: Properties give specific information for each node and relationship.

In the above example,

  • Nodes : 3
  • Labels: 2 (Person and Book)
  • Properties for Label Person: 2 ( Name and age)
  • Properties for Label Book: 2 ( Title and Author)
  • Relationships: 2 (“IS_FRIENDS_WITH” and “HAS_READ”). These relationships have properties that give us extra information.

Notice that the relationship is unidirectional. It means that John is friends with Sally, but Sally isn’t friends with John. Similarly, John has read Graph Databases, but Graph Databases hasn’t read John.

That’s a good thing. Because relationships are specific, each node can have many relationships with other nodes to capture their context fully.

There are four ways to get started with Neo4j:

  • Neo4j Sandbox for learning and exploring
  • Neo4j Desktop for local development
  • Neo4j Aura for development and production (secure cloud service)
  • Neo4j Server for development and production (self-hosted on chosen infrastructure)


Cypher is a robust, simple graph-optimized query language that identifies and utilizes data relationships. It’s simple to use and understand, and it explores relationships in every direction to discover previously unseen links and clusters. In addition, Cypher is based on ASCII art, so the syntax is easily understood and makes queries more self-explanatory.


This will create a node with the label “Person.” In Cypher, a node is represented with a set of parentheses. If we want to refer to the node later, we can store it in a variable. In this case, the variable is n. Then, we can name the variable as we like and reference them as needed.

This creates two nodes (a) and (b) with two labels each.

(a) has “Person” and “Actor”, whereas (b) has “Person” and “Director.”

We can also add properties that provide additional details to our nodes and relationships. These are name-value pairs, so we use curly braces to represent these.

This creates a “Person” node with two properties, “name” and “age”.


Like how we use SELECT to view the data in relational models, we use MATCH here.

This will match and return all the nodes in our database. RETURN specifies what result we want from our query. It could be nodes, relationships, or properties. So it’s only required during reading procedures. Note that the variable has to be specified here, else the query won’t return anything.

This is going to return all the nodes labeled “Person” with the name Brad. It is always recommended to use a label when using the MATCH clause since retrieval will be faster.

Instead of returning the entire node, this will return only the name and born value of the nodes that match the query.


To utilize the power of a graph database, we also need to express the relationships between our nodes.

Relationships are represented in Cypher using an arrow → or ← between two nodes.

CALL db.schema.visualization() will give us all the relationship types between the nodes in the graph.

There are multiple ways to create a relationship between two nodes.

This creates the same relationship but with extra information in the form of a property.

Next is to query the relationships and utilize graphs efficiently finally.

This creates the same relationship but with extra information in the form of a property.

Next is to query the relationships and utilize graphs efficiently finally.

“rel” here is a variable that represents the relationship for the relationship type, ACTED_IN. So this query is going to return us a graph with all the “Person” who have “ACTED_IN” “The Matrix”.

This is going to return all the movies that “Tom Hanks” has either “DIRECTED” or “ACTED_IN.” Notice how the direction of the relationship has been changed.

Remember that we must insert a direction into the database and match it with an undirected relationship. Cypher will then ignore any particular direction and retrieve all the relationships and connected nodes.

Undirected relationships are represented with no arrow and just two dashes —. It means that this can traverse the relationship in either direction.

Moreover, “ — “ means related to, ignoring, the type or direction of the relationship. So this will give us a graph with all “Person” nodes related to “The Matrix” in any form.

There are a couple of in-built functions in Cypher like type(), shortestPath() etc . We’re going to use type() in the next example. This returns the type of relationship.

This will give us all the “Person” related to “The Matrix” and, more importantly, how they’re related to it.

We can also filter by the properties of relationships.

This returns all the “Person” nodes that have the “ACTED_IN” relationship to the “Movie” node with the title “The Matrix.” So, in simple words, show me all the actors that have acted in The Matrix.

One more way is to use the WHERE clause to filter queries. The benefit of using a WHERE clause is that we can specify potentially complex conditions for the query.


We have covered the basics of Neo4j and Cypher today. However, there is a lot more to read to know them inside out. To sum it up, if you require a database that manages large volumes of data and generates some insight from the existing data, then a graph database is the way to go.

Leave a Reply

Your email address will not be published. Required fields are marked *

2 + 6 =