Advanced Topics

Once you’re comfortable with the basics of Cypher, there are many advanced features that can help you write more powerful and efficient queries. This section will cover several advanced topics in Cypher, including subqueries, conditional execution, temporal and spatial types, using Cypher with procedures and functions, working with nulls, and error handling.

Subqueries

Subqueries allow you to break down complex queries into smaller, more manageable parts. They can be used with the CALL clause, and the results can be used in the enclosing query. Here’s an example of a subquery:

CALL {
  MATCH (p:Person { name: 'John Doe' })
  RETURN p.age AS age
}
MATCH (p:Person)
WHERE p.age > age
RETURN p

This query first finds the age of ‘John Doe’, and then returns all Person nodes where the age is greater than ‘John Doe’s age.

Conditional Execution

Cypher supports conditional execution of parts of a query using the CASE clause. This can be useful for executing different parts of a query based on certain conditions. For example:

MATCH (p:Person)
RETURN p.name,
CASE
  WHEN p.age >= 18 THEN 'Adult'
  ELSE 'Minor'
END AS status

This query returns the status of each person as either ‘Adult’ or ‘Minor’, depending on their age.

Temporal and Spatial Types

Cypher includes support for temporal types (like dates, times, and durations) and spatial types (like points and geometries).

Temporal types can be used to model dates and times, and Cypher provides several functions for manipulating these types, such as date(), time(), and datetime().

Spatial types can be used to model geographic data. The point() function can be used to create point values, and there are several functions for calculating distances and performing other spatial operations.

Using Cypher with Procedures and Functions

Procedures and functions are predefined pieces of code that can be executed within a Cypher query. They can be used with the CALL clause, and the results can be used in the rest of the query. For example:

CALL db.labels()
YIELD label
RETURN count(*) AS numberOfLabels

This query calls the db.labels() procedure, which returns all labels in the database, and then counts the number of labels.

Working with Nulls

In Cypher, null represents the absence of a value. It can be the result of a property not being set, a missing relationship, or the result of a function that returns no value. Cypher includes several functions for dealing with null values, such as coalesce() and isNull().

© Polypheny GmbH. All Rights Reserved.