Neo4j WITH Clause



WITH allows you to pass on data from one part of the query to the next. Whatever you list in WITH will be available in the next query part.

You can use aggregation, SKIP, LIMIT, ORDER BY with WITH much like in RETURN. The only difference is that your expressions have to get an alias with AS alias to be able to access them in later query parts.

That means you can chain query parts where one computes some data and the next query part can use that computed data. In your case it is what GROUP BY and HAVING would be in SQL but WITH is much more powerful than that.

here is another example

match (n:Employee)-[r1:WORKSIN]->(a:Team)
with distinct a
order by a.name limit 10
match (a)-[:INBUILDING]->(c:Property)
return c.name


WITH is just like RETURN just within a query.

it can select, aggregate, limit, sort it's arguments.

The only difference is that you need to name each expression.

Any after the WITH statement only the values / variables that are passed along are accessible/visible.
Referring to variable names not passed along using with, may instantiate new Nodes.



https://www.markhneedham.com/blog/2013/03/20/neo4jcypher-getting-the-hang-of-the-with-statement/

https://neo4j.com/docs/cypher-manual/current/clauses/with/

Comments