Comparison operators
Comparison operators are used to compare values. Cypher® contains the following comparison operators:
-
Equality:
= -
Inequality:
<> -
Less than:
< -
Greater than:
> -
Less than or equal to:
<= -
Greater than or equal to:
>= -
IS NULL -
IS NOT NULL
| For more information about how Cypher orders and compares different value types, see Values and types → Equality, ordering, and comparison of value types |
Example graph
The following graph is used for the examples below:
To recreate the graph, run the following query in an empty Neo4j database:
CREATE (alice:Person {name:'Alice', age: 65, role: 'Project manager', email: 'alice@company.com'}),
(cecil:Person {name: 'Cecil', age: 25, role: 'Software developer', email: 'cecil@private.se'}),
(cecilia:Person {name: 'Cecilia', age: 31, role: 'Software developer'}),
(charlie:Person {name: 'Charlie', age: 61, role: 'Security engineer'}),
(daniel:Person {name: 'Daniel', age: 39, role: 'Director', email: 'daniel@company.com'}),
(eskil:Person {name: 'Eskil', age: 39, role: 'CEO', email: 'eskil@company.com'})
Examples
=)MATCH (n:Person)
WHERE n.role = 'Software developer'
RETURN n.name AS name, n.role AS role
| name | role |
|---|---|
|
|
|
|
Rows: 2 |
|
<>)MATCH (n:Person)
WHERE n.role <> 'Software developer'
RETURN n.name AS name, n.role AS role
| name | role |
|---|---|
|
|
|
|
|
|
|
|
Rows: 4 |
|
<)MATCH (n:Person)
WHERE n.age < 39
RETURN n.name AS name, n.age AS age
| name | age |
|---|---|
|
|
|
|
Rows: 2 |
|
<=)MATCH (n:Person)
WHERE n.age <= 39
RETURN n.name AS name, n.age AS age
| name | age |
|---|---|
|
|
|
|
|
|
|
|
Rows: 4 |
|
>)MATCH (n:Person)
WHERE n.age > 39
RETURN n.name AS name, n.age AS age
| name | age |
|---|---|
|
|
|
|
Rows: 2 |
|
>=)MATCH (n:Person)
WHERE n.age >= 39
RETURN n.name AS name, n.age AS age
| name | age |
|---|---|
|
|
|
|
|
|
|
|
Rows: 4 |
|
IS NULL operatorMATCH (n:Person)
WHERE n.email IS NULL
RETURN n.name AS name
| name |
|---|
|
|
Rows: 2 |
IS NOT NULL operatorMATCH (n:Person)
WHERE n.email IS NOT NULL
RETURN n.name AS name, n.email AS email
| name | |
|---|---|
|
|
|
|
|
|
|
|
Rows: 4 |
|
Chaining comparison operators
There is no limit on how many comparisons can be chained together.
If chaining two or more comparison operators, each comparison is effectively separated by an AND operator (though this AND is not required syntactically).
For example, if a, b, c, …, z are expressions and op1, op2, …, opN are comparison operators, then the following expressions are equivalent:
a op1 b op2 c ... y opN z;
a op1 b AND b op2 c AND ... y opN z
Note that a op1 b op2 c does not imply any kind of comparison between a and c.
For example, in x < y > z, x and z are not compared.
Chaining equality operators
Chains of = and <> are treated in a special way in Cypher.
Specifically, 1=1=true is equivalent to 1=1 AND 1=true and not to (1=1)=true or 1=(1=true).
For example, the following expressions are equivalent.
a < b = c <= d <> e;
a < b AND b = c AND c <= d AND d <> e