Skip to main content

WHERE clause

WHERE isn't usually considered a standalone clause but rather a part of the MATCH, OPTIONAL MATCH and WITH clauses.

The difference when using WHERE with these clauses is that it only filter the results in the case of the WITH clause, while it adds constraints to the patterns described in the case of MATCH and OPTIONAL MATCH.

WHERE is part of the directly preceding MATCH or OPTIONAL MATCH clause and should always be used like that to avoid problems with performance or results.

  1. Basic usage
    1. Boolean Operators
    2. Inequality Operators Operators
    3. Filter with node labels
    4. Filter with node properties
    5. Filter with relationship properties
    6. Check if property is not null
  2. String matching
  3. Regular Expressions

Data Set#

The following examples are executed with this data set. You can create this data set locally by executing the queries at the end of the page: Data Set.

Data set

1. Basic Usage#

1.1 Boolean Operators#

Standard boolean operators like NOT, AND, OR and XOR can be used.

MATCH (c:Country)WHERE c.language = 'English' AND  c.continent = 'Europe'RETURN;


+----------------+|         |+----------------+| United Kingdom |+----------------+

1.2 Inequality Operators Operators#

Standard inequality operators like <, <=, > and >= can be used.

MATCH (c:Country)WHERE (c.population > 80000000)RETURN;


+---------+|  |+---------+| Germany |+---------+

1.3 Filter with node labels#

Nodes can be filtered by their label using the WHERE clause instead of specifying it directly in the MATCH clause.



+----------------+|         |+----------------+| Germany        || France         || United Kingdom |+----------------+

1.4 Filter with node properties#

Just as labels, node properties can be used in the WHERE clause to filter nodes.

MATCH (c:Country)WHERE c.population < 70000000RETURN;


+----------------+|         |+----------------+| France         || United Kingdom |+----------------+

1.5 Filter with relationship properties#

Just as with node properties, relationship properties can be used as filters.

MATCH (:Country {name: 'United Kingdom'})-[r]-(p)WHERE r.date_of_start = 2014RETURN p;


+---------------------------+| p                         |+---------------------------+| (:Person {name: "Harry"}) || (:Person {name: "Anna"})  |+---------------------------+

1.6 Check if property is not null#

To check if a node or relationship property exists use the IS NOT NULL option.

MATCH (c:Country)WHERE = 'United Kingdom' AND c.population IS NOT NULLRETURN, c.population;


+----------------+----------------+|         | c.population   |+----------------+----------------+| United Kingdom | 66000000       |+----------------+----------------+

2. String matching#

Apart from comparison and concatenation operators openCypher provides special string operators for easier matching of substrings:

a STARTS WITH bReturns true if the prefix of string a is equal to string b.
a ENDS WITH bReturns true if the suffix of string a is equal to string b.
a CONTAINS bReturns true if some substring of string a is equal to string b.


+---------+|  |+---------+| Germany |+---------+

3. Regular expressions#

Inside WHERE clause, you can use regular expressions for text filtering. To use a regular expression, you need to use the =~ operator.

For example, finding all Person nodes which have a name ending with a.

MATCH (n :Person) WHERE =~ ".*a$" RETURN n;


+--------------------------+| n                        |+--------------------------+| (:Person {name: "Anna"}) |+--------------------------+

The regular expression syntax is based on the modified ECMAScript regular expression grammar. The ECMAScript grammar can be found here, while the modifications are described in this document.

Data set Queries#

We encourage you to try out the examples by yourself. You can get our data set locally by executing the following query block.

CREATE (c1:Country { name: 'Germany', language: 'German', continent: 'Europe', population: 83000000 });CREATE (c2:Country { name: 'France', language: 'French', continent: 'Europe', population: 67000000 });CREATE (c3:Country { name: 'United Kingdom', language: 'English', continent: 'Europe', population: 66000000 });
MATCH (c1),(c2)WHERE 'Germany' AND = 'France'CREATE (c2)<-[:WORKING_IN { date_of_start: 2014 }]-(p:Person { name: 'John' })-[:LIVING_IN { date_of_start: 2014 }]->(c1);
MATCH (c)WHERE 'United Kingdom'CREATE (c)<-[:WORKING_IN { date_of_start: 2014 }]-(p:Person { name: 'Harry' })-[:LIVING_IN { date_of_start: 2013 }]->(c);
MATCH (p1),(p2)WHERE = 'John' AND = 'Harry'CREATE (p1)-[:FRIENDS_WITH { date_of_start: 2011 }]->(p2);
MATCH (p1),(p2)WHERE = 'John' AND = 'Harry'CREATE (p1)<-[:FRIENDS_WITH { date_of_start: 2012 }]-(:Person { name: 'Anna' })-[:FRIENDS_WITH { date_of_start: 2014 }]->(p2);
MATCH (p),(c1),(c2)WHERE = 'Anna' AND = 'United Kingdom' AND = 'Germany'CREATE (c2)<-[:LIVING_IN { date_of_start: 2014 }]-(p)-[:LIVING_IN { date_of_start: 2014 }]->(c1);
MATCH (n)-[r]->(m) RETURN n,r,m;