0Pricing
Neo4j Graph Database Fundamentals · Lesson

Combining Queries with WITH and UNION

Learn to chain query stages using WITH and merge result sets using UNION for advanced Cypher querying.

Combining Queries with WITH and UNION is a free Neo4j Graph Database Fundamentals lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Neo4j Graph Database Fundamentals learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Chain Query Stages

Complex questions often need several steps: filter, aggregate, then filter again. Cypher uses WITH to pass results from one stage to the next.

Introducing WITH

WITH works like RETURN but feeds its output into the next part of the query instead of ending it.

MATCH (p:Person)-[:FRIEND]->(f)
WITH p, count(f) AS friendCount
WHERE friendCount > 3
RETURN p.name, friendCount;

Filtering After Aggregation

You cannot put aggregate functions in a WHERE directly. Instead, aggregate with WITH, then filter the aggregated value in the following WHERE.

This is the Cypher equivalent of SQL's HAVING.

MATCH (c:City)<-[:LIVES_IN]-(p:Person)
WITH c, count(p) AS population
WHERE population > 1000
RETURN c.name, population;

Carrying Variables Forward

Only variables listed in WITH stay in scope afterward. Anything omitted is dropped, so list everything you still need.

MATCH (p:Person)-[:BOUGHT]->(prod)
WITH p, collect(prod.name) AS products
RETURN p.name, products;

Ordering and Limiting Mid-Query

WITH can include ORDER BY, SKIP, and LIMIT to shape intermediate results before the next stage.

MATCH (p:Person)
WITH p ORDER BY p.age DESC LIMIT 5
MATCH (p)-[:FRIEND]->(f)
RETURN p.name, f.name;

Introducing UNION

UNION combines the results of two queries into one result set. Both sides must return the same column names.

MATCH (p:Person) RETURN p.name AS name
UNION
MATCH (c:Company) RETURN c.name AS name;

UNION vs UNION ALL

UNION removes duplicate rows. UNION ALL keeps every row, including duplicates, and is faster when you know there are none.

MATCH (p:Person) RETURN p.name AS name
UNION ALL
MATCH (c:Company) RETURN c.name AS name;

Combining WITH and UNION

You can aggregate within each side of a UNION using WITH, then merge the summaries. This builds powerful reporting queries.

MATCH (o:Order) WITH 'orders' AS kind, count(o) AS n RETURN kind, n
UNION
MATCH (u:User) WITH 'users' AS kind, count(u) AS n RETURN kind, n;

Multiple WITH Stages

You can chain many WITH clauses to build a pipeline: each stage refines the data further.

MATCH (p:Person)-[:RATED]->(m:Movie)
WITH m, avg(p.rating) AS avgRating
WITH m, avgRating WHERE avgRating > 4
RETURN m.title, avgRating;

Common Pitfalls

Watch out for:

  • Forgetting to carry a needed variable through WITH
  • Mismatched column names across UNION branches
  • Using WITH when a simple WHERE would do

When to Use Each

Use WITH for multi-step pipelines and post-aggregation filtering. Use UNION to stack results from structurally different queries.

Quick Check

Check your understanding of query composition.

Recap

You learned to compose advanced queries:

  • WITH pipes results between stages
  • Filter aggregates after WITH (like HAVING)
  • Only variables in WITH stay in scope
  • UNION merges result sets; UNION ALL keeps duplicates

Frequently asked questions

Is the “Combining Queries with WITH and UNION” lesson free?

Yes — the full text of “Combining Queries with WITH and UNION” is free to read here on the web, and the Neo4j Graph Database Fundamentals course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Neo4j Graph Database Fundamentals course, upgrade to CoddyKit PRO.

What will I learn in “Combining Queries with WITH and UNION”?

Learn to chain query stages using WITH and merge result sets using UNION for advanced Cypher querying. You practise Neo4j Graph Database Fundamentals with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Neo4j Graph Database Fundamentals?

No prior experience is required. Neo4j Graph Database Fundamentals on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Combining Queries with WITH and UNION” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Neo4j Graph Database Fundamentals lesson?

Yes. Every Neo4j Graph Database Fundamentals lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Filtering and Ordering Results
  2. Aggregation and Projections
  3. Updating and Deleting Graph Data
  4. Combining Queries with WITH and UNION
← Back to Neo4j Graph Database Fundamentals