The Cypher query language is composed of a variety of clauses. These are similar to verbs in a natural language, describing what action to take on the data. Each clause serves a unique purpose and has a specific syntax. This guide will cover the most common and useful clauses in Cypher.
MATCH
The MATCH
clause is used to identify a pattern within the graph database. It is primarily used when reading or querying data.
MATCH (p:Person { name: 'John Doe' })
RETURN p
WHERE
The WHERE
clause adds conditions to the patterns described in the MATCH
clause, similar to the WHERE clause in SQL. It allows you to filter query results.
MATCH (p:Person)
WHERE p.age > 30
RETURN p.name
RETURN
The RETURN
clause specifies what data to return from a query. It is always used at the end of the query.
MATCH (p:Person)
WHERE p.age > 30
RETURN p.name
CREATE
The CREATE
clause is used to create data (nodes and relationships) in the database.
CREATE (p:Person { name: 'John Doe', age: 32 })
DELETE
The DELETE
clause is used to remove nodes or relationships from the database. Note: you must remove all of a node’s relationships before you can delete the node itself.
MATCH (p:Person)
WHERE p.name = 'John Doe'
DELETE p
SET
The SET
clause is used to update the properties of a node or a relationship.
MATCH (p:Person { name: 'John Doe' })
SET p.age = 33
REMOVE
The REMOVE
clause is used to remove properties or labels from a node.
MATCH (p:Person { name: 'John Doe' })
REMOVE p.age
ORDER BY
The ORDER BY
clause is used to sort the result of a query. You can specify ascending (ASC
) or descending (DESC
) order.
MATCH (p:Person)
RETURN p.name
ORDER BY p.age DESC
LIMIT
The LIMIT
clause is used to limit the number of results returned by a query.
MATCH (p:Person)
RETURN p
LIMIT 10
SKIP
The SKIP
clause is used in combination with LIMIT
to paginate through query results.
MATCH (p:Person)
RETURN p
SKIP 10 LIMIT 10
UNION
The UNION
clause is used to combine the results of two or more MATCH queries. It does not remove duplicates.
MATCH (p:Person { name: 'John Doe' })
RETURN p
UNION
MATCH (p:Person { name: 'Jane Doe' })
RETURN p
MERGE
The MERGE
clause is used to ensure that a certain pattern exists in the graph. If it does not exist, MERGE
will create it.
MERGE (p:Person { name: 'John Doe' })
WITH
The WITH
clause is used to carry the results of one query forward into another query. It’s useful for complex queries and for performance reasons.
MATCH (p:Person)
WITH p
ORDER BY p.name ASC
RETURN p
UNWIND
The UNWIND
clause is used to expand lists into separate rows.
WITH [1, 2, 3] AS list
UNWIND list AS number
RETURN number
FOREACH
The FOREACH
clause is used to update data within a list, whether that data is a set of nodes, relationships, or a path.
MATCH (p:Person { name: 'John Doe' })
FOREACH (n IN ['Jane', 'Bob', 'Alice'] |
CREATE (p)-[:KNOWS]->(:Person { name: n }))
CASE
The CASE
clause allows for conditional logic in queries. It’s similar to IF-THEN-ELSE statements in other programming languages.
MATCH (p:Person)
RETURN p.name,
CASE WHEN p.age >= 18 THEN 'Adult'
ELSE 'Minor'
END AS status