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.

Find Actors with Common Movies

vansh_k
Node

Write a query to find all actors who satisfy the following conditions:

  • The actor had played together with Tom Hanks in a movie that was released before or in 1995, AND
  • The actor has not played together with Tom Hanks in a movie that was released after 1995.

Eleven actors in the graph satisfy the above conditions - 11 records returned in total.

The data set for this query is the default Movie database in Neo4j. 

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

This is one approach:

 

match(n:Person{name: 'Tom Hanks'})-[:ACTED_IN]->(m:Movie)<-[:ACTED_IN]-(o:Person) 
where m.released <= 1995
and not exists {
    match(n)-[:ACTED_IN]->(x:Movie)<-[:ACTED_IN]-(o) 
    where x.released > 1995
}
return distinct o.name

 

View solution in original post

2 REPLIES 2

glilienfield
Ninja
Ninja

This is one approach:

 

match(n:Person{name: 'Tom Hanks'})-[:ACTED_IN]->(m:Movie)<-[:ACTED_IN]-(o:Person) 
where m.released <= 1995
and not exists {
    match(n)-[:ACTED_IN]->(x:Movie)<-[:ACTED_IN]-(o) 
    where x.released > 1995
}
return distinct o.name

 

Thanks, it works! 

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online