Stephen’s solutions to Star Wars neo4j queries

  1. What characters have come into physical contact with both droids and aliens?
match (a {species:"droid"})-[:touches]-(b)-[:touches]-(c {species:"alien"})
             return distinct b.name;

or, alternately:

match (a {species:"droid"})-[:touches]-(b)
match (c {species:"alien"})-[:touches]-(b)
    return distinct b.name;
  1. Which is the “touchiest” episode? Which is the “talkiest?”
match (a:Character)-[r:touches]->(b:Character)
    return r.ep, count(*) order by count(*) desc;
match (a:Character)-[r:speaksTo]->(b:Character)
    return r.ep, count(*) order by count(*) desc;
  1. Which pairs of characters have appeared together in the greatest number of different locations?
match (a)-[r:appearsWith]->(b)
    return a.name, b.name, count(distinct r.loc);
  1. Which character has mentioned which other character the most times?
match (a:Character)-[r:mentions]->(b:Character)
   return a.name, b.name, count(*) order by count(*) desc;
  1. Who are all the humans who speak to, or mention, wookies, and how many times do they each do so?
match (a:Character {species:"human"})-[r:speaksTo|mentions]->(b:Character {species:"wookie"})
    return a.name, type(r), b.name, count(*);
  1. Which pairs of characters interact in some way (dialogue, presence in a scene, physical contact, etc.) in all three films?
match (a)-[{ep:"IV"}]-(b),
      (a)-[{ep:"V"}]-(b),
      (a)-[{ep:"VI"}]-(b)
    return distinct a.name, b.name;

related:

match (a)-[rIV {ep:"IV"}]-(b),
      (a)-[rV {ep:"V"}]-(b),
      (a)-[rVI {ep:"VI"}]-(b)
    return distinct a.name, type(rIV) as IV, type(rV) as V, type(rVI) as VI, b.name;