Question about Cypher statement in Exersice 4.10

Dear Neo4j Community,
I just started to learn Neo4j and have some trouble understanding the logic behind two similar Cypher statements I used in exercise 4.10

The question in the exercise is:

Retrieve all people who have produced a movie, but have not directed a movie, returning their names and the movie titles.

My initial statement looked like this:

MATCH (a:Person)-[:PRODUCED]->(s:Movie)
WHERE NOT EXISTS ((a)-[:DIRECTED]->(s))
RETURN a.name, s.title

and returns 12 records

While the correct answer is displayed as:

MATCH (a:Person)-[:PRODUCED]->(s:Movie)
WHERE NOT EXISTS ((a)-[:DIRECTED]->(:Movie))
RETURN a.name, s.title

and returns 7 records.

The difference is that i initially used the "(s)" variable in pattern matching in line 2 instead of the "(:Movie)" label.

I tried to understand the difference and my assumption is, that my initial statement returns persons that produced a movie but did't direct the same movie, instead of returning person that produced a movie but didn't direct any movie.

Is this assumption correct?

Your assumption is exactly correct. When you use WHERE NOT EXISTS((a)-[:DIRECTED]->(s)), you are pinning s to the specific movie matched in the prior clause. So you are effectively saying find me people who produced a Movie who also did not direct the SAME movie.

The exercise is asking you to find people who produced a movie, but have never directed ANY movie, and the WHERE NOT EXISTS ((a)-[:DIRECTED]->(:Movie)) clause meets that goal.

1 Like

Thanks @brant_boehmann for the quick response! It's highly appreciated.
I am eager to learn Neo4j fast but without missing beeing precice about it.
Hope I can redeem one day!