# Use the result of Union as input for another match

**URL:** <https://community.neo4j.com/t/use-the-result-of-union-as-input-for-another-match/71677>\
**Category:** Cypher\
**Created:** [November 28, 2024, 7:15pm UTC](https://community.neo4j.com/t/use-the-result-of-union-as-input-for-another-match/71677 "2024-11-28T19:15:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![linuxhippy](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/linuxhippy/32/33426_2.png) [@linuxhippy](https://community.neo4j.com/u/linuxhippy)\
**Post date:** [November 28, 2024, 7:15pm UTC](https://community.neo4j.com/t/use-the-result-of-union-as-input-for-another-match/71677/1 "2024-11-28T19:15:36Z")

</div>

Hi,

I am using UNION to combine the result of two queries into a single node list, which works as expected. However I would like to use this combined node set as input for the next match and I can't seem to get it right...

MATCH(n).....  
RETURN n  
UNION  
MATCH(n).....  
RETURN n  
WITH n  
MATCH(x)....-\>(n)  
RETURN n, x;

Written as-is, it complains about the second RETURN not being at the end of the query.  
If I omit it, the WITH part is interpreted as part of the second query.

Thank you in advance, Clemens

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [November 28, 2024, 7:27pm UTC](https://community.neo4j.com/t/use-the-result-of-union-as-input-for-another-match/71677/2 "2024-11-28T19:27:15Z")

</div>

Try this structure:

```auto
Call () {
  MATCH(n).....
  RETURN n
  UNION
  MATCH(n).....
  RETURN n
{
WITH n
MATCH(x)....->(n)
RETURN n, x;

```

---

<div class="post-metadata">

**Author:** ![linuxhippy](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/linuxhippy/32/33426_2.png) [@linuxhippy](https://community.neo4j.com/u/linuxhippy)\
**Post date:** [November 29, 2024, 10:10am UTC](https://community.neo4j.com/t/use-the-result-of-union-as-input-for-another-match/71677/3 "2024-11-29T10:10:04Z")

</div>

Thank you very much - using call to actually introduce some kind of query hierarchy worked perfectly fine - I only had to omit the empty braces:

```auto
Call {
  MATCH(n).....
  RETURN n
  UNION
  MATCH(n).....
  RETURN n
}
WITH n
MATCH(x)....->(n)
RETURN n, x;

```

---

<div class="post-metadata">

**Author:** ![valerio.malenchino](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/valerio.malenchino/32/5358_2.png) [@valerio.malenchino](https://community.neo4j.com/u/valerio.malenchino)\
**Post date:** [November 29, 2024, 11:19am UTC](https://community.neo4j.com/t/use-the-result-of-union-as-input-for-another-match/71677/4 "2024-11-29T11:19:57Z")

</div>

I don't think you need the `WITH n` statement.

At that point in the query, n is the only variable in scope, so you are not dropping variables out of scope, you are not introducing a new variable, you are not adding reads after writes, you are not filtering aggregates, you are not sorting results, you are not applying limits or where clauses  
-\> You do not need WITH

If you want more info on the WITH statement, see [docs for WITH clause.](https://neo4j.com/docs/cypher-manual/current/clauses/with/)

Empty braces are the recommended syntax from version 5.23 onwards, not available in previous versions.
