0Pricing
Neo4j Graph Database Fundamentals · 강의

WITH와 UNION으로 질의 결합하기

WITH를 사용해 질의 단계를 연결하고 UNION으로 결과 집합을 병합하는 고급 Cypher 질의를 배웁니다.

WITH와 UNION으로 질의 결합하기은(는) CoddyKit의 무료 Neo4j Graph Database Fundamentals 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Neo4j Graph Database Fundamentals 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Neo4j Graph Database Fundamentals 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

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

자주 묻는 질문

“WITH와 UNION으로 질의 결합하기” 강의는 무료인가요?

네 — “WITH와 UNION으로 질의 결합하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Neo4j Graph Database Fundamentals 강의 전체를 잠금 해제할 수 있습니다. Neo4j Graph Database Fundamentals 강의에는 총 4개의 강의가 포함되어 있습니다.

“WITH와 UNION으로 질의 결합하기”에서 뭘 배우나요?

WITH를 사용해 질의 단계를 연결하고 UNION으로 결과 집합을 병합하는 고급 Cypher 질의를 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Neo4j Graph Database Fundamentals을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Neo4j Graph Database Fundamentals을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Neo4j Graph Database Fundamentals은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.

“WITH와 UNION으로 질의 결합하기” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Neo4j Graph Database Fundamentals 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Neo4j Graph Database Fundamentals 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 결과 필터링 및 정렬
  2. 집계 및 프로젝션
  3. 그래프 데이터 업데이트 및 삭제
  4. WITH와 UNION으로 질의 결합하기
← Neo4j Graph Database Fundamentals(으)로 돌아가기