Skip to main content
The WITH clause allows you to chain query parts together, passing results from one part to the next. This enables complex query composition and data manipulations.

Use Cases

WITH is useful for:
  • Chaining multiple query parts together
  • Performing intermediate aggregations
  • Filtering or transforming results before the next query part
  • Using query modifiers (DISTINCT, ORDER BY, LIMIT, SKIP) mid-query

Example: Filtering by Aggregated Values

Find all children above the average age of all people:

Example: Using Modifiers Mid-Query

You can use query modifiers with WITH to filter or sort before continuing:
This query:
  1. Matches all users
  2. Orders them by last visit (oldest first)
  3. Limits to the 3 least recent visitors
  4. Sets a flag on those users

Key Points

  • WITH acts like a pipeline between query parts
  • Variables not included in WITH are not available in subsequent parts
  • You can rename variables using AS in the WITH clause
  • Aggregations in WITH cause implicit grouping (like RETURN)
  • WHERE filters placed after a WITH boundary are evaluated after any write operations that preceded the WITH. This means the filter sees the post-write graph state — for example, it will not see edges that were deleted earlier in the same query.

Frequently Asked Questions

WITH acts as a pipeline between query parts, passing results from one section to the next. It enables intermediate aggregations, filtering, sorting, and variable scoping within a single query.
Only variables explicitly listed in the WITH clause are available in subsequent query parts. Any variable not included is dropped from scope.
Yes. WITH supports all query modifiers including DISTINCT, ORDER BY, SKIP, and LIMIT. This lets you filter and sort intermediate results before continuing the query.
Aggregation functions in WITH cause implicit grouping, just like in RETURN. Non-aggregated expressions become grouping keys automatically.