- Neo4j Tutorial
- Neo4j - Home
- Neo4j - Overview
- Neo4j - Data Model
- Neo4j - Environment Setup
- Neo4j - Building Blocks
- Neo4j CQL Write Clauses
- Neo4j - Merge Command
- Neo4j - Set Clause
- Neo4j - Delete Clause
- Neo4j - Remove Clause
- Neo4j - Foreach Clause
- Neo4j CQL Read Clause
- Neo4j - Match Clause
- Neo4j - Optional Match Clause
- Neo4j - Where Clause
- Neo4j - Count Function
- Neo4j CQL General Clauses
- Neo4j - Return Clause
- Neo4j - Order By Clause
- Neo4j - Limit Clause
- Neo4j - Skip Clause
- Neo4j - With Clause
- Neo4j - Unwind Clause
- Neo4j CQL Functions
- Neo4j - String Functions
- Neo4j - Aggregation Function
- Neo4j CQL Admin
- Neo4j - Backup & Restore
- Neo4j - Index
- Neo4j - Create Unique Constraint
- Neo4j - Drop Unique
- Neo4j Useful Resources
- Neo4j - Quick Guide
- Neo4j - Useful Resources
- Neo4j - Discussion
Neo4j CQL - WHERE Clause
Like SQL, Neo4j CQL has provided WHERE clause in CQL MATCH command to filter the results of a MATCH Query.
Simple WHERE clause syntax
WHERE <condition>
Complex WHERE clause syntax
WHERE <condition> <boolean-operator> <condition>
We can put multiple conditions on same command by using Boolean Operators. Please refer next section for available Boolean operators in Neo4j CQL.
<condition> syntax:
<property-name> <comparison-operator> <value>
Syntax Description
S.No. | Syntax Element | Description |
---|---|---|
1. | WHERE | It is a Neo4j CQL keyword. |
2. | <property-name> | It is a property name of a Node or a Relationship. |
3. | <comparison-operator> | It is a one of the Neo4j CQL Comparison operators.Please refer next section for available Comparison operators in Neo4j CQL. |
4. | <value> | It is a literal value like number literal, string literal etc. |
Boolean operators in Neo4j CQL
Neo4j supports the following Boolean operators to use in Neo4j CQL WHERE clause to support multiple conditions.
S.No. | Boolean operators | Description |
---|---|---|
1. | AND | It is a Neo4j CQL keyword to support AND operation. It is like SQL AND operator. |
2. | OR | It is a Neo4j CQL keyword to support OR operation. It is like SQL AND operator. |
3. | NOT | It is a Neo4j CQL keyword to support NOT operation. It is like SQL AND operator. |
4. | XOR | It is a Neo4j CQL keyword to support XOR operation. It is like SQL AND operator. |
Comparison operators in Neo4j CQL
Neo4j supports the following Comparison operators to use in Neo4j CQL WHERE clause to support conditions.
S.No. | Boolean operators | Description |
---|---|---|
1. | = | It is a Neo4j CQL "Equal To" operator. |
2. | <> | It is a Neo4j CQL "Not Equal To" operator. |
3. | < | It is a Neo4j CQL "Less Than" operator. |
4. | > | It is a Neo4j CQL "Greater Than" operator. |
5. | <= | It is a Neo4j CQL "Less Than Or Equal To" operator. |
6. | >= | It is a Neo4j CQL "Greater Than Or Equal To" operator. |
Example
This example demonstrates how to use CQL WHERE clause in MATCH Command to retrieve an employee details based on employee name.
Step 1 - Open Neo4j Data Browser
It is Neo4j Data Browser Homepage
Step 2 - Type the below command on Data Browser
MATCH (emp:Employee) RETURN emp.empid,emp.name,emp.salary,emp.deptno
Step 3 - Click on "Execute" button and observe the results.
If we observe the results, it returns the 4 employee node details.
Step 4 - Type the below command on Data Browser
MATCH (emp:Employee) WHERE emp.name = 'Abc' RETURN emp
Step 5 - Click on "Execute" button and observe the results.
Use "Grid View" to see the node details.If we observe the results, it returns only one employee details whose name is "Abc".
Example
This example demonstrates how to use Multiple conditions with Boolean operator in CQL WHERE clause in MATCH Command to retrieve employee details based on employee name.
Step 1 - Open Neo4j Data Browser
Step 2 - Type the below command on Data Browser
MATCH (emp:Employee) RETURN emp.empid,emp.name,emp.salary,emp.deptno
Step 3 - Click on "Execute" button and observe the results.
If we observe the results, it returns the 4 employee node details.
Step 4 - Type the below command on Data Browser
MATCH (emp:Employee) WHERE emp.name = 'Abc' OR emp.name = 'Xyz' RETURN emp
Step 5 - Click on "Execute" button and observe the results.
Use "Grid View" to see the node details.If we observe the results, it returns only two employee details whose name is "Abc" or "Xyz".
Create Relationship with WHERE clause
In Neo4J CQL, We can create a Relationship between tow Nodes in different ways.
Create a Relationship between two existing nodes
Create a two Nodes and Relationship between them at a time
Create a Relationship between two existing nodes with WHERE clause
We have already discussed first two approaches in the previous chapters. Now we are going to discuss on "Create a Relationship between two existing nodes with WHERE clause" in this chapter
Syntax
MATCH (<node1-label-name>:<node1-name>),(<node2-label-name>:<node2-name>) WHERE <condition> CREATE (<node1-label-name>)-[<relationship-label-name>:<relationship-name> {<relationship-properties>}]->(<node2-label-name>)
Syntax Description
S.No. | Syntax Element | Description |
---|---|---|
1. | MATCH,WHERE,CREATE | They are Neo4J CQL keywords. |
2. | <node1-label-name> | It is a Node one label name used to create a Relationship. |
3. | <node1-name> | It is a Node one name used to create a Relationship. |
4. | <node2-label-name> | It is a Node one label name used to create a Relationship. |
5. | <node2-name> | It is a Node one name used to create a Relationship. |
6. | <condition> | It is a Neo4J CQL WHERE clause condition. It may be simple or complex. |
7. | <relationship-label-name> | It is a label name of newly creating Relationship between Node one and Node two. |
8. | <relationship-name> | It is a name of newly creating Relationship between Node one and Node two. |
9. | <relationship-properties> | It is a properties list(key-value pairs) of newly creating Relationship between Node one and Node two. |
Example
This example demonstrates how to Create a Relationship between two existing nodes with WHERE clause.
Step 1 - Open Neo4J Data Browser
Step 2 - Type the below command on Data Browser to verify our required Customer node is available in our Neo4J Database.
MATCH (cust:Customer) RETURN cust.id,cust.name,cust.dob
Step 3 - Click on "Execute" button and observe the results.
If we observe the results, it shows that our required Customer node is available in our Neo4J Database.
Step 4 - Type the below command on Data Browser to verify our required CreditCard node is available in our Neo4J Database.
MATCH (cc:CreditCard) RETURN cc.id,cc.number,cc.expiredate,cc.cvv
Step 5 - Click on "Execute" button and observe the results.
If we observe the results, it shows that our required CreditCard node is available in our Neo4J Database.
Step 6 - Type the below command on Data Browser to create a Relationship between Customer and CreditCard Nodes.
MATCH (cust:Customer),(cc:CreditCard) WHERE cust.id = "1001" AND cc.id= "5001" CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014",price:55000}]->(cc) RETURN r
Step 7 - Click on "Execute" button and observe the results.
Click on Relationship and observe its properties in a separate window
Now we have created a NEW Relationship between two existing Nodes by using Neo4J CQL WHERE Clause.
To Continue Learning Please Login
Login with Google