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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, and System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.
Happy Learning!