+3 votes
in Databases by (19.0k 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 (51.1k points)

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;


...