Where
WHERE
clause is where you specify predicates/constraints on a previous part of your query.
Regardless of what comes before WHERE, the semantics of WHERE is this two step computation:
WHERE
takes the tuples that the previous parts of your query has generated (up to the WHERE clause);- It then runs the boolean predicate specified in the WHERE clause and outputs those that pass the predicates to the next part of your query.
We will use the example database for demonstration, whose schema and data import commands are given here.
For example:
Output:
The boolean predicate specified above can be understood as it reads: Users “a” whose ages are greater than 45 OR whose names start with “Kar”. It combines several means to construct expressions in high-level database query languages, such as as boolean operator (OR), a numeric comparison operator (>), and a string function (starts_with). You can learn more about the operators, functions and expressions here.
Filter with NULL
If an expression is evaluated as NULL, it will be treated as FALSE in WHERE
clause. To check if an expression is NULL, please refer to comparison operators on NULLs.
The following predicate in the WHERE clause filters User nodes whose name start with “Kar” and whose age properties are not NULL.
Output:
WHERE
subquery on a relationship
You can also specify a subquery that matches a relationship using the WHERE
clause.
The above query matches users who follow “Noura” and lives in “Guelph”. Note that you can only
RETURN
the nodes that are in the scope of the MATCH
clause (the nodes and relationships that
are in the WHERE
clause are not returned).