Developer Guides Getting Started Getting Started What is a Graph Database? Intro to Graph DBs Video Series Concepts: RDBMS to Graph Concepts: NoSQL to Graph Getting Started Resources Neo4j Graph Platform Graph Platform Overview Neo4j Desktop Intro Neo4j Browser Intro… Read more →

Developer Guides

Want to Speak? Get $ back.

Guide to Cypher Basics

This guide explains the basic concepts of Cypher, Neo4j’s query language, including how to create and query graphs. You should be able to read and understand Cypher queries after finishing this guide.
You should have familiarized yourself with Graph Databases and the Property Graph Model.

Pop-Cultural Connections

The Movie Graph is a mini graph application containing actors and directors that are related through the movies they’ve collaborated on. It is available in your Neo4j Browser if you execute :play movie-graph. It is helpful if you run the queries as you go along, but you should understand them even so.

This guide will show you how to:

  1. Create: insert movie data into the graph
  2. Find: retrieve individual movies and actors
  3. Query: discover related actors and directors
  4. Solve: the Bacon Path


Hidden below this text is a giant code block containing a single Cypher query statement composed of multiple CREATE clauses. This will create the movie graph.

(You can unfold it with the little [role=’icon-plus-sign-alt’]plus button)


Example queries for finding individual nodes.

  1. Look at every query example
  2. Run the query with the play button
  3. Notice the syntax pattern
  4. Try looking for other movies or actors

Find the Person Named “Tom Hanks”…​

MATCH (tom:Person)
WHERE = "Tom Hanks"

Find the Movie Titled “Cloud Atlas”…​

MATCH (cloudAtlas:Movie {title: "Cloud Atlas"})
RETURN cloudAtlas

Find 10 People…​

MATCH (people:Person)

Find Movies Released in the 1990s…​

MATCH (nineties:Movie)
WHERE nineties.released > 1990 AND nineties.released < 2000
RETURN nineties.title


Finding patterns within the graph.

  1. Actors are people who acted in movies
  2. Directors are people who directed a movie
  3. What other relationships exist?

List All Tom Hanks Movies…​

MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(tomHanksMovies)
RETURN tom,tomHanksMovies

Who Directed “Cloud Atlas”?

MATCH (cloudAtlas:Movie {title: "Cloud Atlas"})<-[:DIRECTED]-(directors)

Tom Hanks’ Co-Actors…​

MATCH (tom:Person {name:"Tom Hanks"})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(coActors)
RETURN tom, m, coActors

Solve the Six Degrees Question

You’ve heard of the classic “Six Degrees of Kevin Bacon”? That is simply a shortest path query called the “Bacon Path”.

  1. Variable length patterns
  2. Built-in shortestPath() algorithm

Movies and Actors up to Three Hops Away from Kevin Bacon

MATCH (bacon:Person {name:"Kevin Bacon"})-[*1..3]-(hollywood)

The Bacon Path to Meg Ryan

MATCH p=shortestPath(
  (bacon:Person {name:"Kevin Bacon"})-[*]-(meg:Person {name:"Meg Ryan"})

Clean Up

When you’re done experimenting, you can remove the movie data set.

  1. Nodes can’t be deleted if relationships to them exist
  2. Delete both nodes and relationships together
This will remove all Person and Movie nodes!

Delete All Movie and Person Nodes and their Relationships

MATCH (a:Person),(m:Movie)
OPTIONAL MATCH (a)-[r1]-(), (m)-[r2]-()
DELETE a,r1,m,r2

Prove that the Movie Graph is Gone

RETURN count(*)