Boolean operators

Boolean operators are used to combine or evaluate logical conditions. Cypher® contains the following boolean operators:

  • Conjunction: AND

  • Disjunction: OR

  • Exclusive disjunction: XOR

  • Negation: NOT

Truth table for boolean operators
a b a AND b a OR b a XOR b NOT a

false

false

false

false

false

true

false

null

false

null

null

true

false

true

false

true

true

true

true

false

false

true

true

false

true

null

null

true

null

false

true

true

true

true

false

false

null

false

false

null

null

null

null

null

null

null

null

null

null

true

null

true

null

null

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. Boolean operators
AND operator
MATCH (n:Person)
WHERE n.age > 30 AND n.role = 'Software developer'
RETURN n.name AS name, n.age AS age, n.role AS role
Result
name age role

"Cecilia"

31

"Software developer"

Rows: 1

OR operator
MATCH (n:Person)
WHERE n.age < 30 OR n.role = 'Software developer'
RETURN n.name AS name, n.age AS age, n.role AS role
Result
name age role

"Cecilia"

31

"Software developer"

"Cecil"

25

"Software developer"

Rows: 2

XOR operator
MATCH (n:Person)
WHERE n.age > 30 XOR n.role = 'Software developer'
RETURN n.name AS name, n.age AS age, n.role AS role
Result
name age role

"Alice"

65

"Project manager"

"Cecil"

25

"Software developer"

"Charlie"

61

"Security engineer"

"Eskil"

39

"CEO"

"Daniel"

39

"Director"

Rows: 5

NOT operator
MATCH (n:Person)
WHERE NOT n.age = 39
RETURN n.name AS name, n.age AS age
Result
name age

"Alice"

65

"Cecil"

25

"Cecilia"

31

"Charlie"

61

Rows: 4

Operator precedence

Operator precedence defines how Cypher groups expressions when parentheses are not present. Within predicates, the following order is relevant (higher precedence first):

  • Comparison operators: =, <>, <, >, , >=, IS NULL, IS NOT NULL, IN, STARTS WITH, ENDS WITH, CONTAINS, =~

  • NOT

  • AND

  • XOR

  • OR

This means NOT applies to the result of a comparison unless you add parentheses.

Examples:

  • NOT true AND false is valid and equivalent to (NOT true) AND false.

  • true AND NOT false is valid and equivalent to true AND (NOT false).

  • NOT true = false is valid and equivalent to NOT (true = false).

  • true = NOT false is not valid. Use true = (NOT false) instead.

When in doubt, add parentheses to make the intended grouping explicit.

Combining boolean operators
MATCH (n:Person)
WHERE n.role = 'Software developer' XOR (n.age > 60 AND n.role = 'Security engineer') OR NOT (n.role = 'Director' OR n.name = 'Eskil')
RETURN n.name AS name, n.age AS age, n.role AS role
Result
name age role

"Alice"

65

"Project manager"

"Cecil"

25

"Software developer"

"Cecilia"

31

"Software developer"

"Charlie"

61

"Security engineer"

Rows: 4