WITHとUNIONによるクエリの結合
WITHでクエリの段階を連結し、UNIONで結果セットを統合する、発展的なCypherクエリの方法を学びます。
「WITHとUNIONによるクエリの結合」はCoddyKit上の無料Neo4j Graph Database Fundamentalsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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時間対応のAIチューター)、Neo4j Graph Database Fundamentalsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Neo4j Graph Database Fundamentalsコースには全4レッスンが含まれています。
「WITHとUNIONによるクエリの結合」で何を学びますか?
WITHでクエリの段階を連結し、UNIONで結果セットを統合する、発展的なCypherクエリの方法を学びます。 ブラウザで直接実行するハンズオンコードでNeo4j Graph Database Fundamentalsを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 結果のフィルタリングと並べ替え
- 集計とプロジェクション
- グラフデータの更新と削除
- WITHとUNIONによるクエリの結合