cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

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?

2X_9_9c58fce96d05b7f290140ff2a1c354380e22756f.png

1 ACCEPTED SOLUTION

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.

View solution in original post

2 REPLIES 2

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.

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!

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online