03. Neo4j Cypher Query
2 min readJan 16, 2021
We are aware of SQL “Structured Query Language“ which we usually use in SQL databases. Here we are using CQL “Cypher Query Language“.
Neo4j is based on node and relations. Node can be considered as tables in SQL and relations can be considered as foreign keys in there.
What is CQL?
- Is a query language for Neo4j Graph Database.
- Is a declarative pattern-matching language.
- Follows SQL like syntax.
- The syntax is very simple and in human-readable format.
- Neo4j CQL has commands to perform database operations.
- Neo4j CQL supports many clauses such as WHERE, ORDER BY, etc., to write very complex queries in an easy manner.
- Neo4j CQL supports some functions such as String, Aggregation. In addition to them, it also supports some Relationship Functions.
Lets understand pattern of CQL. After understanding pattern then it will be just matter of practice.
MATCH (n:Person)-[:KNOWS]->(m:Person) WHERE n.name = 'Alice' RETURN m
- MATCH (n: Person) where n.name= ‘Alice’ it will find a Person with name ‘Alice’.
- -[:KNOWS] : it signifies the relation between two nodes or entities. We can keep some properties with relations also that time we have to filter it out with name then we can write like [r : KNOWS]. Here r is relation alias and we can use “r.date_of_last_meet” kind of property in where clause.
- -[:RELATION]-> : here we can see
- “-[]-> “ : arrows . which signifies directed relation. Here in our case direction from Alice to other person.
- “(a)<-[]-(b)“ : which states that relation from b to a.
- “(a)-[]-(b)“ : if we want to keep relation bidirectional then use this.
- RUTURN m — is just returning data fetched from database.
- WHERE n.name = ‘Alice’ — Here we have used WHERE clause which we are already aware of in SQL also.
There are multiple clauses we can use like we have used in above query. Here are few:-
Read More..
All blogs..