+3 votes
in Databases by (56.5k points)
I am running Cypher queries on the default movie database of Neo4J. I want to check the name of the actors and how many movies they acted in. What will be the Cypher query for it?

1 Answer

+3 votes
by (348k points)
selected by
 
Best answer

You can use the WITH clause for the aggregate function COUNT(). You can apply COUNT() to either relationship or the "Movie" node to get the number of movies for each actor in the node label "Person".

Here are two queries that will return the same output:

Using relationship

MATCH (p:Person)-[r:ACTED_IN]-(m:Movie)

WITH p.name as actor, count(r) as movieCount

RETURN actor, movieCount;

Using Movie node

MATCH (p:Person)-[r:ACTED_IN]-(m:Movie)

WITH p.name as actor, count(m.title) as movieCount

RETURN actor, movieCount;


...