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:

predicate operators

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

Example 1. Comparison operators
Equality operator (=)
MATCH (n:Person)
WHERE n.role = 'Software developer'
RETURN n.name AS name, n.role AS role
Result
name role

"Cecil"

"Software developer"

"Cecilia"

"Software developer"

Rows: 2

Inequality operator (<>)
MATCH (n:Person)
WHERE n.role <> 'Software developer'
RETURN n.name AS name, n.role AS role
Result
name role

"Alice"

"Project manager"

"Charlie"

"Security engineer"

"Daniel"

"Director"

"Eskil"

"CEO"

Rows: 4

Less than operator (<)
MATCH (n:Person)
WHERE n.age < 39
RETURN n.name AS name, n.age AS age
Result
name age

"Cecil"

25

"Cecilia"

31

Rows: 2

Less than or equal operator (<=)
MATCH (n:Person)
WHERE n.age <= 39
RETURN n.name AS name, n.age AS age
Result
name age

"Cecil"

25

"Cecilia"

31

"Daniel"

39

"Eskil"

39

Rows: 4

Greater than operator (>)
MATCH (n:Person)
WHERE n.age > 39
RETURN n.name AS name, n.age AS age
Result
name age

"Alice"

65

"Charlie"

61

Rows: 2

Greater than or equal operator (=>)
MATCH (n:Person)
WHERE n.age => 39
RETURN n.name AS name, n.age AS age
Result
name age

"Alice"

65

"Charlie"

61

"Daniel"

39

"Eskil"

39

Rows: 4

IS NULL operator
MATCH (n:Person)
WHERE n.email IS NULL
RETURN n.name AS name
Result
name

"Cecilia"

"Charlie"

Rows: 2

IS NOT NULL operator
MATCH (n:Person)
WHERE n.email IS NOT NULL
RETURN n.name AS name, n.email AS email
Result
name email

"Alice"

"alice@company.com"

"Cecil"

"cecil@private.se"

"Daniel"

"daniel@company.com"

"Eskil"

"eskil@company.se"

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:

Equivalent expressions
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.

Equivalent expressions
a < b = c <= d <> e;
a < b AND b = c AND c <= d AND d <> e