Conditionally Returning Data

Conditionally returning values for rows

Here is a query that returns the movies that Charlie Chaplin has acted in and the runtime for the movie.

cypher
MATCH (m:Movie)<-[:ACTED_IN]-(p:Person)
WHERE p.name = 'Charlie Chaplin'
RETURN m.title AS movie,
// Add CASE clause code here
m.runtime AS runTime

Modify this query to return "Short" for runTime if the movie’s runtime is < 120 (minutes) and "Long" for runTime if the movie’s runtime is >= 120.

The syntax for a CASE clause is:

cypher
CASE
WHEN [condition] THEN result
WHEN [condition] THEN result
ELSE result
END

Answer this question:

How many short movies?

How many rows returned a value of Short for Runtime?

  • ✓ 6

Hint

The [condition] should test whether the runtime property is less than (<) or greater than or equal to (>=) 120.

The result should be either "Short" or "Long".

You should enter just the number of rows that returned "Short".

Solution

The answer is 6.

Run the following query to see the result:

cypher
MATCH (m:Movie)<-[:ACTED_IN]-(p:Person)
WHERE p.name = 'Charlie Chaplin'
RETURN m.title AS Movie,
CASE
WHEN m.runtime < 120 THEN "Short"
WHEN m.runtime >= 120 THEN "Long"
ELSE "Unknown"
END AS Runtime

Summary

In this challenge, you modified a query to return conditional results.

In the next challenge, you will answer another question about the query results.

Chatbot

How can I help you today?

Data Model

Your data model will appear here.