# WANT DIFFERENCE operator

**URL:** https://community.neo4j.com/t/want-difference-operator/31686
**Category:** Feedback & Requests
**Created:** [January 7, 2021, 11:11pm UTC](https://community.neo4j.com/t/want-difference-operator/31686 "2021-01-07T23:11:17Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![clem](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/clem/32/4364_2.png) [@clem](https://community.neo4j.com/u/clem)
#### Post date: [January 7, 2021, 11:11pm UTC](https://community.neo4j.com/t/want-difference-operator/31686/1 "2021-01-07T23:11:17Z")

</div>

Suppose I want actors who were in "The Matrix" but **not**"Matrix Revolutions"

I want to do something like:

```auto
MATCH(p:Person) -[:ACTED_IN]-> (:Movie {name:"The Matrix"})
DIFFERENCE
MATCH(p) -[:ACTED_IN]-> (:Movie {name:"The Matrix Revolutions"})
RETURN p

```

[added] This is somewhat of a duplicate of my other post, but I thought it be a good request to ask for a set operator (DIFFERENCE) to be added to the existing UNION operator.

> [@How to match and not match?](https://community.neo4j.com/t/how-to-match-and-not-match/31684):
>
> Suppose I want actors who were in "The Matrix" but not "The Matrix Revolutions" I want to do something like: MATCH(p:Person) -[:ACTED\_IN]-\> (:Movie {name:"The Matrix"}) NOT MATCH(p) -[:ACTED\_IN]-\> (:Movie {name:"The Matrix Revolutions"}) RETURN p which is not legal. I'm not sure how to do this.... Google didn't return anything useful but maybe I didn't ask in the best way. Thanks in advance. (If there was a DIFFERENCE operator in addition to UNION or INTERSECTION would be useful too.) [A…

---

<div class="post-metadata">

### Author: ![dominicvivek06](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dominicvivek06/32/5034_2.png) [@dominicvivek06](https://community.neo4j.com/u/dominicvivek06)
#### Post date: [January 8, 2021, 12:14am UTC](https://community.neo4j.com/t/want-difference-operator/31686/2 "2021-01-08T00:14:06Z")

</div>

Hi @clem , is the movie title to be queried is the other way around. Because, I just queried from the movie sample dataset, Matrix and Matrix Revolutions and "The Matrix" has one extra actor. So I think Who all acted in Matrix but not in Matrix Revolutions.

```auto
MATCH (p:Person)-[:ACTED_IN]->(m:Movie {title:"The Matrix"})
RETURN p.name,m.title

╒════════════════════╤════════════╕
│"p.name" │"m.title" │
╞════════════════════╪════════════╡
│"Emil Eifrem" │"The Matrix"│
├────────────────────┼────────────┤
│"Hugo Weaving" │"The Matrix"│
├────────────────────┼────────────┤
│"Laurence Fishburne"│"The Matrix"│
├────────────────────┼────────────┤
│"Carrie-Anne Moss" │"The Matrix"│
├────────────────────┼────────────┤
│"Keanu Reeves" │"The Matrix"│
└────────────────────┴────────────┘

MATCH (p:Person)-[:ACTED_IN]->(m:Movie {title:"The Matrix Revolutions"})
RETURN p.name,m.title

╒════════════════════╤════════════════════════╕
│"p.name" │"m.title" │
╞════════════════════╪════════════════════════╡
│"Hugo Weaving" │"The Matrix Revolutions"│
├────────────────────┼────────────────────────┤
│"Laurence Fishburne"│"The Matrix Revolutions"│
├────────────────────┼────────────────────────┤
│"Carrie-Anne Moss" │"The Matrix Revolutions"│
├────────────────────┼────────────────────────┤
│"Keanu Reeves" │"The Matrix Revolutions"│
└────────────────────┴────────────────────────┘

```

Anyways, my method , you can use a List Compression ,

```auto
MATCH (p1:Person)-[:ACTED_IN]->(m:Movie {title:"The Matrix"})
with collect (p1.name) as matrix_list
MATCH (p2:Person)-[:ACTED_IN]->(m:Movie {title:"The Matrix Revolutions"})
with collect (p2.name) as matrix_rev_list, matrix_list
return [n in matrix_list WHERE NOT n in matrix_rev_list] as actors

╒═══════════════╕
│"actors" │
╞═══════════════╡
│["Emil Eifrem"]│
└───────────────┘

```

For your debugging of the data

```auto
MATCH (p1:Person)-[:ACTED_IN]->(m:Movie {title:"The Matrix"})
with collect (p1.name) as matrix_list
MATCH (p2:Person)-[:ACTED_IN]->(m:Movie {title:"The Matrix Revolutions"})
with collect (p2.name) as matrix_rev_list, matrix_list
return matrix_list, matrix_rev_list

╒═════════════════════════════════════════════════════════════════════════════════════╤═══════════════════════════════════════════════════════════════════════╕
│"matrix_list" │"matrix_rev_list" │
╞═════════════════════════════════════════════════════════════════════════════════════╪═══════════════════════════════════════════════════════════════════════╡
│["Emil Eifrem","Hugo Weaving","Laurence Fishburne","Carrie-Anne Moss","Keanu Reeves"]│["Hugo Weaving","Laurence Fishburne","Carrie-Anne Moss","Keanu Reeves"]│
└─────────────────────────────────────────────────────────────────────────────────────┴───────────────────────────────────────────────────────────────────────┘

```

---

<div class="post-metadata">

### Author: ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)
#### Post date: [January 8, 2021, 9:48pm UTC](https://community.neo4j.com/t/want-difference-operator/31686/3 "2021-01-08T21:48:48Z")

</div>

isnt `I want actors who were in "The Matrix" but **not**"Matrix Revolutions"`  
simply

```auto
MATCH(p:Person) -[:ACTED_IN]-> (:Movie {name:"The Matrix"})
where not 
      (p) -[:ACTED_IN]-> (:Movie {name:"The Matrix Revolutions"})
return p;

```

and also isnt 'DIFFERENCES' similar to SQL Minus [SQL: MINUS Operator](https://www.techonthenet.com/sql/minus.php) ?

---

<div class="post-metadata">

### Author: ![tard\_gabriel](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tard_gabriel/32/28033_2.png) [@tard\_gabriel](https://community.neo4j.com/u/tard_gabriel)
#### Post date: [January 9, 2021, 3:15am UTC](https://community.neo4j.com/t/want-difference-operator/31686/4 "2021-01-09T03:15:41Z")

</div>

If you are using Neo4j 4.x

MATCH (p:Person)-[:ACTED\_IN]-\>(:Movie {name:"The Matrix"})  
WHERE NOT EXISTS { (p) -[:ACTED\_IN]-\> (:Movie {name:"The Matrix Revolutions"}) }  
RETURN p

The dominicvivek06 answer was not bad and for many cases might be more optimised. It's always better to return only the property(ies) you need.

Neo4j relies on two "world", the graph and the projected data from it.  
It's always better to stay as less as possible in the graph world and work in the projected data world. The clauses RETURN and WITH are projecting clauses.

You can read more about the projection principle in the neo4j cypher manual if you want to know why the dominicvivek06 answer is probably better even if it seems more longer and complicated.
