Using WITH to pass on intermediate results
Complete this query to determine the highest average rating for a Tom Hanks movie:
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)<-[r:RATED]-(:User)
WHERE p.name = 'Tom Hanks'
WITH ??????, avg(??????) AS avgRating
WHERE avgRating > 4
RETURN m.title AS Movie, avgRating AS `AverageRating`
ORDER BY avgRating DESC
You will need to use the correct variables in the WITH
clause to pass on the intermediate results.
The average rating should be calculated using the rating
property from the RATED
relationship.
Then answer this question:
What Tom Hanks movie had the highest average rating greater than 4?
Enter the title of the movie. (Note, the answer is case-sensitive).
-
✓ Captain Phillips
Hint
The average rating should be calculated using the rating
property from the RATED
relationship, r
.
The average rating is per movie node, m
.
Solution
The answer is Captain Phillips
.
Run the following query to see the result:
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)<-[r:RATED]-(:User)
WHERE p.name = 'Tom Hanks'
WITH m, avg(r.rating) AS avgRating
WHERE avgRating > 4
RETURN m.title AS Movie, avgRating AS `AverageRating`
ORDER BY avgRating DESC
Summary
In this challenge, you wrote a query to use a WITH
clause to aggregate data.
In the next challenge, you will answer another question using this query.