WITH Clause
Overview
The WITH
clause allows query pipelining in Cypher by passing results from one part of a query to another. It enables:
- Intermediate result filtering
- Variable assignment for later use
- Aggregation before further processing
- Query modularization
Syntax
WITH [expression [AS variable][, expression [AS variable]]*]
[WHERE condition]
[ORDER BY expression [ASC | DESC]]
[SKIP integer]
[LIMIT integer]
Examples
Filtering aggregated results:
MATCH (p:Person)
WITH p, count(p.friends) AS friendCount
WHERE friendCount > 10
RETURN p.name, friendCount
Pagination:
MATCH (post:Post)
WITH post
ORDER BY post.date DESC
SKIP 20
LIMIT 10
RETURN post.title
Chaining queries:
MATCH (user:User)-[:FRIENDS_WITH]->(friend)
WITH user, collect(friend) AS friends
WHERE size(friends) > 5
MATCH (user)-[:LIKED]->(post:Post)
RETURN user.name, post.content
Last updated on