Tutorial: Getting Started with Cypher

Goals
This tutorial 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 tutorial.
Prerequisites
You should have familiarized yourself with Graph Databases and the Property Graph Model.

Beginner

Pop culture 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 helpful if you run the queries and cypher code to create data as you follow this tutorial.

This tutorial will show you how to:

  1. Create: Insert movie data into the graph.

  2. Find: Retrieve individual movies and actors.

  3. Query: Find patterns in the graph.

  4. Solve: Answer some questions about the graph.

Create the movie graph

  1. Create and start a new Neo4j database.

    1. Create a blank sandbox at https://sandbox.neo4j.com or..

    2. Create a new database in Neo4j Desktop:

      1. Create a new project.

      2. Add a database to the project.

      3. Start the database.

  2. Open Neo4j Browser.

  3. Set the browser settings to allow multi-statements:

  1. Enter :play movie graph in the query pane and click the "Play" button on the right. A new window opens below the query pane with the browser guide.

  2. Go to page 2 of the browser guide.

  3. Click on the Cypher code block which will bring it into the query pane and click the "Play" button.

This is what you should see in Neo4j Browser after loading the movie graph:

This is the graph view of some of the data returned.

If you want to see the table view of the data returned, you click the table icon on the left:

How you view the results will also depend on the data returned. If the query returns nodes, then you can view the data as a graph. If the query returns property values, you can only view the data as a table.

If you need help:

:help cypher

When you run Cypher code in the query pane, it always creates a new pane with the results below the query pane.

Find actors and movies

Next, you will learn about 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

If you need help with syntax:

:help MATCH, :help WHERE, and :help RETURN

Find the person named "Tom Hanks"…​

Copy and paste this code into the query pane and execute it:

MATCH (tom:Person)
WHERE tom.name = "Tom Hanks"
RETURN tom

The graph result should look as follows:

You can also view the properties of the node with the table view:

Find the movie titled "Cloud Atlas"…​

Here we filter the query a different way where we specify the value in the node specification, rather than using a WHERE clause.

Copy and paste this code into the query pane and execute it:

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

Here is the result of this query:

And here is the table view:

Find 10 people…​

Next we want to find the names of 10 people in the graph. This code finds all Person nodes in the graph but just returns the name property value for 10 of them.

Copy and paste this code into the query pane and execute it:

MATCH (people:Person)
RETURN people.name LIMIT 10

Here is the result of this query:

For this query, property values are returned and you can only view the results as a table.

Find movies released in the 1990s…​

Here is a query where we specify a range of values for selecting the Movie nodes to retrieve. Then we return the titles of these Movie nodes.

Copy and paste this code into the query pane and execute it:

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

Here is the result of this query:

Find patterns in the graph

Thus far, you have queried the graph for nodes. Next, you will gain experience retrieving related nodes.

You will execute Cypher code to find 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…​

Here is a query where we want to return the Person node for the actor Tom Hanks and we also want to return all Movie nodes that have the ACTED_IN relationship to Tom Hanks. That is, all movies that Tom Hanks acted in.

Copy and paste this code into the query pane and execute it:

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

Here is the result of this query:

Notice here that we also see the DIRECTED relationships between the Tom Hanks node and the Movie nodes. This is because we have a setting in our Neo4j Browser where result nodes will be connected:

And here is the table view:

Who directed "Cloud Atlas"?

Here is a query where we want to return the nodes that have the DIRECTED relationship to the Cloud Atlas Movie node. It will return the names of the people who directed the movie.

Copy and paste this code into the query pane and execute it:

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

Here is the result of this query:

Tom Hanks' co-actors…​

Next, we want to find all movies that Tom Hanks acted in and for each movie retrieved, also find the people who acted in that movie.

Copy and paste this code into the query pane and execute it:

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

Here is the result of this query:

And here is the table view:

Here is a query where we want to return information about the relationships to and from the Cloud Atlas movie. We find the related nodes and then we return the name of the person, the type of relationship, and the properties for that relationship.

Copy and paste this code into the query pane and execute it:

MATCH (people:Person)-[relatedTo]-(:Movie {title: "Cloud Atlas"})
RETURN people.name, type(relatedTo), relatedTo

Here is the result of this query:

Answer some questions about the graph

You’ve heard of the classic "Six Degrees of Kevin Bacon"? That is, find all people who are up to 6 hops away from Kevin Bacon in the graph. This is simply a shortest path query called the "Bacon Path". To perform this type of query, you need to specify:

Movies and actors up to 3 hops away from Kevin Bacon

In our first query, we want to find all movies and/or people who are up to 3 hops away from Kevin Bacon in the graph.

Copy and paste this code into the query pane and execute it:

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

Here is the result of this query:

Find the Bacon Path to Meg Ryan

What is the shortest path between Kevin Bacon and Meg Ryan in the graph? In this Cypher,, we are returning the path that includes nodes and relationships.

Copy and paste this code into the query pane and execute it:

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

Before you execute the query, you will see a warning that a relationship of '*' could take a long time to execute. Our movie graph is small, so you can ignore this warning.

Here is the result of this query:

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 nodes and relationships in the graph!

Copy and paste this code into the query pane and execute it:

MATCH (n)
DETACH DELETE n

Here is the result of this query:

Notice that although the database information in the left panel shows no nodes or relationships in the graph, the property key names remain.

Verify that the movie graph data is gone

If you perform this query to retrieve all nodes in the graph and return the count, you should see a value of 0 returned.

Copy and paste this code into the query pane and execute it:

MATCH (n)
RETURN count(*)

Here is the result of this query:

Congratulations! You have learned how to use Cypher to query a Neo4j database.

User Contributed Notes