Saturday, 28 August 2021

Neo4j: Comparison Operators

Neo4j support following comparison operators.

 

a.   = : Equal to operator

b.   <> : Not Equal to

c.    < : Less than

d.   > : Greater than

e.   <= : Less than or equal to

f.     >= " Greater than or equal to

 

For example, below query return all the persons whose age is 32.

MATCH(n:Person) WHERE n.age=32 RETURN n

 

Let’s see some more examples.

 

Step 1: Open neo4j browser by hitting following url in browser.

http://localhost:7474/browser/

 


Step 2: Insert data by executing below query.

CREATE (p1:Person{name: "Rama Krishna", age: 32, city: "Bangalore", male: true, designation: "CEO"}),
(p2:Person{name: "Murali", age: 32, city: "Hyderabad", male: true, designation: "Vice President"}),
(p3:Person{name: "Gopi", age: 34, city: "Chennai", male: true, designation: "Vice President"}),
(p4:Person{name: "Kiran", age: 28, city: "Bangalore", male: true, designation: "Director"}),
(p5:Person{name: "Siva", age: 45, city: "Hyderabad", male: true, designation: "Director"}),
(p6:Person{name: "Kiran", age: 36, city: "Hyderabad", male: true, designation: "Manager"}),
(p7:Person{name: "Ritweek", age: 23, city: "Bangalore", male: true, designation: "Manager"}),
(p8:Person{name: "Sudheer", age: 24, city: "Chennai", male: true, designation: "Manager"}),
(p9:Person{name: "Naveen", age: 37, city: "Bangalore", male: true, designation: "Employee"}),
(p10:Person{name: "Sailaja", age: 31, city: "Hyderabad", female: true, designation: "Employee"}),
(p11:Person{name: "Harika", age: 36, city: "Bangalore", female: true, designation: "Employee"}),
(p12:Person{name: "Brahmam", age: 41, city: "Bangalore", male: true, designation: "Employee"}),
(office1:Location{name: "Pluto", city: "Bangalore"}),
(office2:Location{name: "Mars", city: "Chennai"})
MERGE (p2)-[:REPORT_TO{from: 2014, description: "reports to"}]->(p1)
MERGE (p3)-[:REPORT_TO{from: 2016, description: "reports to"}]->(p1)
MERGE (p4)-[:REPORT_TO{from: 1997, description: "reports to"}]->(p2)
MERGE (p5)-[:REPORT_TO{from: 1997, description: "reports to"}]->(p3)
MERGE (p6)-[:REPORT_TO{from: 2001, description: "reports to"}]->(p4)
MERGE (p7)-[:REPORT_TO{from: 2009, description: "reports to"}]->(p4)
MERGE (p8)-[:REPORT_TO{from: 1998, description: "reports to"}]->(p5)
MERGE (p9)-[:REPORT_TO{from: 2003, description: "reports to"}]->(p6)
MERGE (p10)-[:REPORT_TO{from: 2004, description: "reports to"}]->(p6)
MERGE (p11)-[:REPORT_TO{from: 2008, description: "reports to"}]->(p7)
MERGE (p12)-[:REPORT_TO{from: 2019, description: "reports to"}]->(p8)
MERGE (p2)-[:FRIEND_TO{from: 2014, description: "friend to"}]->(p1)
MERGE (p10)-[:FRIEND_TO{from: 2014, description: "friend to"}]->(p8)
MERGE (p7)-[:FRIEND_TO{from: 2014, description: "friend to"}]->(p9)
MERGE (p3)-[:RELATIVE_TO{from: 2014, description: "relative to"}]->(p4)
RETURN p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, office1, office2

 


 

Get all the persons whose age is equal to 32

MATCH(n:Person) WHERE n.age=32 RETURN n




Get all the persons whose age is not equal to 32.

 

 


 


Get all the persons whose age is < 32.

MATCH(n:Person) WHERE n.age<32 RETURN n

 

 


Get all the persons whose age is > 32

MATCH(n:Person) WHERE n.age>32 RETURN n

 


 

Similarly you can perform <=, >= queries.

Get all the persons whose age is <= 32.

MATCH(n:Person) WHERE n.age<=32 RETURN n

 

Get all the persons whose age is >= 32.

MATCH(n:Person) WHERE n.age>=32 RETURN n

 

One important thing to note is that comparison operators can be used to compare text (strings).

 

For example, below query return all the persons whose name starts with R and greater than R .

MATCH(n:Person) WHERE n.name>='R' RETURN n




MATCH(n:Person) WHERE n.name>="R" AND n.name < "Sb" RETURN n



Previous                                                    Next                                                    Home

No comments:

Post a Comment