OPTIONAL MATCH

Using OPTIONAL MATCH

Optionally returning rows

This query returns all the titles movies in the Film-Noir genre and the users who rated them.

cypher
MATCH (m:Movie)-[:IN_GENRE]->(g:Genre)
WHERE g.name = 'Film-Noir'
MATCH (m)<-[:RATED]-(u:User)
RETURN m.title, u.name

Currently it only returns movies that have also been rated.

Modify the query so that it returns all movies in the Film-Noir genre, regardless of whether they have been rated.

How many rows are returned?

  • ✓ 1152

Hint

You can make a MATCH clause optional by prefixing the OPTIONAL keyword.

Solution

The answer is 1152.

You can run the following query to see the result:

cypher
MATCH (m:Movie)-[:IN_GENRE]->(g:Genre)
WHERE g.name = 'Film-Noir'
OPTIONAL MATCH (m)<-[:RATED]-(u:User)
RETURN m.title, u.name

Summary

In this challenge, you modified a query and answered a question about what was retrieved which expanded the data returned to specify an OPTIONAL MATCH.

In the next module, you will learn about controlling results returned.