Like SQL, Neo4j CQL has furnished WHERE clause in CQL MATCH command to filter out the outcomes of a MATCH Query.
Syntax
Following is the syntax of the WHERE clause.
MATCH (label)
WHERE label.country = "property"
RETURN label
Example
Before intending with the example, create five nodes within the database as shown below.
CREATE(Dhawan:player{name:"shikar Dhawan", YOB: 1985, runs:363, country: "India"}
CREATE(Jonathan:player{name:"Jonathan Trott", YOB:1981, runs:229, country:"South Africa"}
CREATE(Sangakkara:player{name:"Kumar Sangakkara", YOB:1977, runs:222,
country:"Srilanka"})
CREATE(Rohit:player{name:"Rohit Sharma", YOB: 1987, runs:177, country:"India"})
CREATE(Virat:player{name:"Virat Kohli", YOB: 1988, runs:176, country:"India"})
CREATE(Ind:Country {name: "India", result: "Winners"})
Following is a pattern Cypher Query which returns all of the players (nodes) that belongs to the usa India the usage of WHERE clause.
MATCH (player)
WHERE player.country = "India"
RETURN player
To execute the above question, perform the following steps −
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as proven in the following screenshot.
Step 2 − Copy and paste the favored question inside the greenback prompt and press the play button (to execute the question) highlighted in the following screenshot.
Result
On executing, you will get the following end result.
WHERE Clause with Multiple Conditions
You also can use the WHERE clause to verify multiple situations.
Syntax
Following is the syntax to use WHERE clause in Neo4j with a couple of conditions.
MATCH (emp:Employee)
WHERE emp.name = 'Abc' AND emp.name = 'Xyz'
RETURN emp
Example
Following is a sample Cypher Query which filters the nodes within the Neo4j database using situations.
MATCH (player)
WHERE player.country = "India" AND player.runs >=175
RETURN player
To execute the above query, perform the following steps −
Step 1 − Open the Neo4j computer App and start the Neo4j Server. Open the built-in browser app of Neo4j the usage of the URL http://localhost:7474/ as shown inside the following screenshot.
Step 2 − Copy and paste the favored query within the dollar prompt and press the play button (to execute the query) highlighted inside the following screenshot.
Result
On executing, you will get the subsequent end result.
Using Relationship with Where Clause
You also can use Where clause to clear out the nodes using the relationships.
Example
Assume we've got the following graph within the database.
Following is a sample Cypher Query to retrieve the pinnacle scorer of India the usage of WHERE clause as shown below.
MATCH (n)
WHERE (n)-[: TOP_SCORER_OF]->( {name: "India", result: "Winners"})
RETURN n
To execute the above question, perform the subsequent steps −
Step 1 − Open the Neo4j computing device App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown inside the following screenshot.
Step 2 − Copy and paste the preferred question within the dollar activate and press the play button (to execute the query) highlighted within the following screenshot.
Result
On executing, you'll get the subsequent end result. Here you may study that Neo4j returned the node, which has the relation TOP_SCORER_OF to the usa with the node having the name India.