Cypher Query Language or CQL is the language to traverse and update graph structures in Neo4J. The language is similar to SQL and provides a declarative syntax for querying. However, it is worth mentioning that unlike SQL in which one needs to provide the complete procedure how to get records by defining joins etc. CQL’s language syntax suggests that one must only define what to get without specifying how to get it. Let’s revisit the schema example that i took in my last blog post to understand this better.
Consider the above schema, lets find the number of persons who visited a particular city. Th SQL query should look something like this.
SELECT name FROM Person LEFT JOIN VISITED_CITIES ON Person.Id = VISITED_CITIES.Person_Id LEFT JOIN City ON City.Id = VISITED_CITIES.City_Id WHERE CITY.name = “New Delhi”
However, the same thing can be achieved via CQL in Neo4j with only this :-
MATCH (p:Person)-[:VISITED]->(c:City) WHERE c.name = “New Delhi” RETURN p.name
It can be clearly observed that in SQL we have defined all the joins to let SQL database know that how to get the required data from different tables, but in CQL we have only defined what to get with a ‘PATTERN CLAUSE’
Terminology and syntax of CQL :-
CQL uses ASCII like characters for syntax :-
- Nodes are defined in round braces ‘(‘, ‘)‘ . In above example we have defined a node of type person with (:Person) syntax and also has assigned it to a variable ‘p’, thus the complete sub expression (p:Person) also their is a node of type CIty which is assigned to variable c. Note that by assigning these nodes to variables we were able to apply different property checks too like city name must be ‘New Delhi’ etc.
- Relationships are defined with arrows ‘->’ and when you want to apply conditions on those too (e.g type of relation etc.) you embed those arrows with square braces that contains all the conditions. In the above example we have defined a relationship between nodes of type person and City where the type of relation should be Visited.
So the complete PATTERN clause that we are looking at is, ‘Lets find a person who has visited a particular city’
The power of CQL lies in pattern search and that’s why we are not limited to just this kind of simple relationships, you can provide any patterns that you may think of will exist in your schema. For e.g if you want to design a system in which you want to provide friendship recommendation on the basis of city visited history of a person, you can do so by something like this
MATCH (p:Person)-[:VISITED]->(c:City)<-[:Lived_In]-(p2:Person) WHERE c.name = “New Delhi” RETURN p.name, p2.name
The above query will provide you with name of persons who have visited New Delhi with name of persons who have Lived_in new delhi, so that you can match the common interests on persons and show recommendations based on it.
Clauses in CQL :-
- Match :- It is similar to select clause of SQL
- Where :- Similar to where clause of SQL, used to filter out query results
- Create :- The ‘INSERT’ of C, will be discussing in my next blog.
- Delete :- Used to delete nodes from Neo4J.
- Set :- Update a property of node or relationship
- Remove :- Remove a property of node or relationship
- Return :- Used to filter out what is returned from the query.
Next Blog about :- CRUD operations in CQL.