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

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"

"Daniel"

39

"Director"

Rows: 3