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.

Potentially incorrect answer to Exercise 6.4 in "4.0-intro-neo4j-exercises"

The exercise is: "Retrieve all directors, their movies, and people who acted in the movies, returning the name of the director, the number of actors the director has worked with, and the list of actors." However, the posted answer doesn't include the movies.

Posted answer:
"MATCH (d:Person)-[:DIRECTED]->(m:Movie)<-[:ACTED_IN]-(a:Person) RETURN d.name AS director, count(a) AS number actors , collect(a.name) AS actors worked with"

I believe the correct (or more complete) answer is:
MATCH (d:Person)-[:DIRECTED]->(m:Movie)<-[:ACTED_IN]-(a:Person)
RETURN d.name AS director, collect(DISTINCT m.title) AS movies directed, count(a) AS number actors , collect(a.name) AS actors worked with

(I'm still trying to understand why I need to include DISTINCT to prevent duplicate movie titles. An earlier post about exercise 6.3 seems close but I'm not sure it directly relates to this query.)

1 REPLY 1

Welcome @lukas.mcmichael to the Community!

The description of the query and the return specification does not include the list of movies, but you can certainly return them.

The DISTINCT is used to eliminate duplicate elements in a list or in a set of rows returned so it is the right thing to do.

Elaine