0Pricing
Neo4j Graph Database Fundamentals · Lekcja

Łączenie zapytań za pomocą WITH i UNION

Nauczą się Państwo łączyć etapy zapytań za pomocą WITH oraz scalać zbiory wyników przy użyciu UNION podczas zaawansowanego tworzenia zapytań Cypher.

Łączenie zapytań za pomocą WITH i UNION to bezpłatna lekcja Neo4j Graph Database Fundamentals na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Neo4j Graph Database Fundamentals, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Neo4j Graph Database Fundamentals zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Łączenie zapytań za pomocą WITH i UNION” jest bezpłatna?

Tak — pełny tekst „Łączenie zapytań za pomocą WITH i UNION” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Neo4j Graph Database Fundamentals, przejdź na CoddyKit PRO. Kurs Neo4j Graph Database Fundamentals zawiera 4 lekcji w sumie.

Co nauczysz się w „Łączenie zapytań za pomocą WITH i UNION”?

Nauczą się Państwo łączyć etapy zapytań za pomocą WITH oraz scalać zbiory wyników przy użyciu UNION podczas zaawansowanego tworzenia zapytań Cypher. Ćwiczysz Neo4j Graph Database Fundamentals z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Neo4j Graph Database Fundamentals?

Nie wymagamy żadnego doświadczenia. Neo4j Graph Database Fundamentals w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Łączenie zapytań za pomocą WITH i UNION”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Neo4j Graph Database Fundamentals?

Tak. Każda lekcja Neo4j Graph Database Fundamentals zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Filtrowanie i sortowanie wyników
  2. Agregowanie i projekcje
  3. Aktualizowanie i usuwanie danych grafowych
  4. Łączenie zapytań za pomocą WITH i UNION
← Powrót do Neo4j Graph Database Fundamentals