Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Cypher Language
3.
Query Structure
4.
Filtering and Aggregation
4.1.
Filtering Data
4.2.
Aggregating Data
5.
Using Cypher Language
6.
Frequently Asked Questions
6.1.
What is the Cypher Language?
6.2.
How to filtering and aggregate the nodes in Cypher?
6.3.
How does Cypher differ from SQL?
7.
Conclusion
Last Updated: Mar 27, 2024

Neo4j Query Cypher Language

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Welcome to Coding Ninjas; Today, In the world of query languages, we are going to discuss a topic called ‘Neo4j Query Cypher Language’. In this article, we will discuss what the cypher language is, its query structure, pattern, filtering, and aggregation.

In cypher language, the nodes are connected to each other for the graph, so if you want to know what a graph database is. I will recommend you to read this article on ‘Neo4j Graph Database’.

Neo4j is a very popular graph database management system, and it is designed to store, manage, and query graph-structured information in the database. Neo4j can be used as a powerful tool for applications that deal with highly connected data.

Neo4j Query Cypher Language

Cypher Language

Cypher Language is a query language introduced by Neo4j that was designed for the graph database. This query language can be used for both relational and non-relational databases. In SQL, we store the data in the form of a table with rows and columns. In Cypher Language, the data is represented as nodes.

Here is an example of a cypher language query:

MATCH (N:Ninjas {name: “Ninja1”})
RETURN N

 

The query matches a node labeled as "Ninjas" with the property "name" equal to "Ninja1" and returns that node. It looks for a specific ninja with the name "Ninja1" in the graph and retrieves that node as the query result.

There are several features that make cypher language different from other languages are following:

  • It can be used for creating, reading, updating, and deleting the data from the graph database.
     
  • Extracting and Sorting can be done on nodes or relationship properties.
     
  • Filtering and Aggregation can be done on the data.
     
  • Path Matching can be done on the graph to find the paths.
     
  • Cypher Language also supports querying and filtering the nodes and relationships based on their properties.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Query Structure

Here are some examples of using cypher queries:

1. Creating Node with properties:

CREATE (n:Label { property1: value1, property2: value2 })
RETURN n


The query creates a new node with the label "Label" and sets its properties, "property1" and "property2," to the specified values. It then returns the newly created node as the query result. It allows for the creation of a node with specific properties and captures the created node for further use or confirmation.

2. Creating the relationship between nodes:

MATCH (n1:Label1), (n2:Label2)
WHERE n1.property = value1 AND n2.property = value2
CREATE (n1)-[:RELATIONSHIP]->(n2)
RETURN n1, n2


The query matches two nodes, one labeled as "Label1" and the other as "Label2," based on specific property conditions. It then creates a directed relationship of type "RELATIONSHIP" from the first node to the second node. Finally, it returns both nodes involved in the relationship. The query establishes a connection between nodes that meet the property criteria and captures the connected nodes as the query result.

3. Retrieving specific properties of nodes:

MATCH (n:Label)
RETURN n.property1, n.property2


The query matches all nodes labeled as "Label" and returns the values of their "property1" and "property2" properties. It retrieves the specified properties of all nodes with the given label and presents them as the query result.

4. Retrieving all nodes of a specific label:

MATCH (n:Label)
RETURN n


The query matches all nodes labeled as "Label" and returns the nodes themselves. It retrieves all nodes that have been assigned the specified label and presents them as the query result.

5. Filtering nodes based on the property value or data:

MATCH (n:Label)
WHERE n.property = value
RETURN n


The query matches all nodes labeled as "Label" that have a property equal to the specified value. It filters the nodes based on the property condition and returns the filtered nodes as the query result.

Filtering and Aggregation

In the cypher language, there are capabilities of filtering and aggregation that can be used to refine the query results, and the calculations can be performed on the graph data. Let’s understand these features in detail with the example:

Filtering Data

WHERE is a clause in cypher language used to filter the data based on some specific conditions. This WHERE clause supports several comparison and logical operators such as =, <, >, <=, >=, AND, OR, NOT to filter the nodes in a graph database.

Here’s an example of filtering data:


MATCH (n:Person)
WHERE n.age > 30 AND n.country = 'USA'
RETURN n.name


The query matches nodes labeled as "Person" and filters them based on two conditions: age greater than 30 and country equal to 'USA'. It then returns the names of the matching nodes. The query retrieves the names of people who are older than 30 and from the USA as the query result.

Aggregating Data

There are some aggregate functions in cypher language that can be used to perform the aggregating operations on data. Examples of aggregate functions such as COUNT, SUM, AVG, MIN, and MAX.

Here’s an example of aggregating data:

MATCH (n:Product)
RETURN COUNT(n) AS totalProducts, AVG(n.price) AS averagePrice

 

The query matches nodes labeled as "Product" and calculates the total count of matching nodes. It also calculates the average value of the "price" property across those nodes. The query then returns the computed values as "totalProducts" and "averagePrice" respectively. The output will provide the total number of products and the average price of those products based on the data in the graph database.

Using Cypher Language

Here, in this example, where will create three nodes with properties, connect them in a graph, and then lastly delete them using cypher language:

To create three nodes with properties ‘name’ and ‘rollNumber’:

CREATE (n1:Student {name: 'Ninja1', rollNumber: 1}),
       (n2:Student {name: 'Ninja2', rollNumber: 2}),
       (n3:Student {name: 'Ninja3', rollNumber: 3})

 

This query creates three nodes labeled as ‘Student’ and assigns them properties ‘name’ and ‘rollNumber’ with their respective values.

To create the nodes with relationships:

MATCH (n1:Student {name: 'John'}), (n2:Student {name: 'Alice'}), (n3:Student {name: 'Bob'})
CREATE (n1)-[:FRIEND]->(n2),
       (n2)-[:FRIEND]->(n3),
       (n3)-[:FRIEND]->(n1)

 

This query matches the previously created nodes and connects them with a relationship type ‘FRIEND’. It establishes a friendship relationship between the nodes.
To delete the nodes and relationships:

MATCH (n:Student)
DETACH DELETE n

 

This query matches with all created nodes labeled as ‘Student’ and deletes them, along with any relationships connected to them. The ‘DETACH’ keyword ensures that the relationships are also deleted before removing the nodes.

Frequently Asked Questions

What is the Cypher Language?

Cypher Language is a query language introduced by Neo4j that was designed for the graph database. This query language can be used for both relational and non-relational databases. In Cypher Language, the data is represented as nodes.

How to filtering and aggregate the nodes in Cypher?

Comparison and Logical Operators such as =, <, >, <=, >=, AND, OR, and NOT can be used for filtering the nodes. Aggregating can be done by using aggregating functions such as COUNT, SUM, AVG, MIN, and MAX.

How does Cypher differ from SQL?

In SQL, we store the data in the form of a table with rows and columns. In Cypher Language, the data is represented as nodes. Cypher Language can be used for both relational and non-relational databases.

Conclusion

Cypher Language is a query language introduced by Neo4j where It deals with the nodes, and the data is present in the graph format. ASCII-Art is used to create patterns in cypher language. This article was based on a topic called ‘Neo4j Query Cypher language’, where we discussed what the cypher language is, the structure of the query, filtering, aggregation, and implementation of the cypher language.

I will recommend this article on what are the ‘neo4j graph database’.

You can refer to our guided paths on the Coding Ninjas Studio platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMS, and System Design, etc. as well as some Contests, Test SeriesInterview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Happy Learning!

Previous article
The Random Oracle Model and its Algorithms in Cryptography
Next article
Comparison of Security Criteria in Hash
Live masterclass