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 ElementDescription
1.WHEREIt 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 operatorsDescription
1.ANDIt is a Neo4j CQL keyword to support AND operation. It is like SQL AND operator.
2.ORIt is a Neo4j CQL keyword to support OR operation. It is like SQL AND operator.
3.NOTIt is a Neo4j CQL keyword to support NOT operation. It is like SQL AND operator.
4.XORIt 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 operatorsDescription
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

Neo4j CQL Tutorial

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
Neo4j CQL Tutorial

Step 3 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

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
Neo4j CQL Tutorial

Step 5 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

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

Neo4j CQL Tutorial

Step 2 - Type the below command on Data Browser

MATCH (emp:Employee)
RETURN emp.empid,emp.name,emp.salary,emp.deptno
Neo4j CQL Tutorial

Step 3 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

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
Neo4j CQL Tutorial

Step 5 - Click on "Execute" button and observe the results.

Neo4j CQL Tutorial

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 ElementDescription
1.MATCH,WHERE,CREATEThey 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
Neo4J CQL Tutorial

Step 3 - Click on "Execute" button and observe the results.

Neo4J CQL Tutorial

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
Neo4J CQL Tutorial

Step 5 - Click on "Execute" button and observe the results.

Neo4J CQL Tutorial

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
Neo4J CQL Tutorial

Step 7 - Click on "Execute" button and observe the results.

Neo4J CQL Tutorial

Click on Relationship and observe its properties in a separate window

Neo4J CQL Tutorial

Now we have created a NEW Relationship between two existing Nodes by using Neo4J CQL WHERE Clause.

Advertisements