# SPARQL query GRAPH unions (maybe through virtual graph)

**URL:** <https://community.stardog.com/t/sparql-query-graph-unions-maybe-through-virtual-graph/4292>\
**Category:** Support\
**Created:** [March 13, 2023, 5:57am UTC](https://community.stardog.com/t/sparql-query-graph-unions-maybe-through-virtual-graph/4292 "2023-03-13T05:57:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cesar](https://avatars.discourse-cdn.com/v4/letter/c/e47774/32.png) [@Cesar](https://community.stardog.com/u/Cesar)\
**Post date:** [March 13, 2023, 5:57am UTC](https://community.stardog.com/t/sparql-query-graph-unions-maybe-through-virtual-graph/4292/1 "2023-03-13T05:57:42Z")

</div>

Hi,  
As far as we know, in SPARQL query processing, the FROM clause is used to identify the default graph for a query and if multiple FROM clauses are specified in a query then the contents of those graphs are merged to provide a union graph. Having said that, our questions are:

- If multiple FROM NAMED clauses are specified in a query then the contents of those graphs are not merged to provide a union graph, right?
- If yes, is there any trick or workaround to specify multiple FROM NAMED clauses in a query to execute the query over the union of all named graphs while returning the graph name as a variable too?
- Is it possible to create a virtual graph which is a union of two or more named graphs?

Thank you!

---

<div class="post-metadata">

**Author:** ![PaulJackson](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pauljackson/32/243_2.png) [@PaulJackson](https://community.stardog.com/u/PaulJackson)\
**Post date:** [March 13, 2023, 12:47pm UTC](https://community.stardog.com/t/sparql-query-graph-unions-maybe-through-virtual-graph/4292/2 "2023-03-13T12:47:11Z")

</div>

Suppose you have this data:

```auto
INSERT DATA {
  GRAPH <graph:a> {
    <urn:a> a <urn:Gecko> .
  }
  GRAPH <graph:b> {
    <urn:a> rdfs:label "Carl" .
  }
}

```

and want something like:

```auto
SELECT ?g ?type ?label FROM NAMED <graph:a> FROM NAMED <graph:b> {
  GRAPH ?g {
    <urn:a> a ?type ;
      rdfs:label ?label .
  }
}

```

... but the type triple is in one graph and the label is in another and you don't know which is which.

You can use:

```auto
SELECT ?gt ?type ?gl ?label FROM NAMED <graph:a> FROM NAMED <graph:b> {
  GRAPH ?gt {
    <urn:a> a ?type ;
  }
  GRAPH ?gl {
    <urn:a> rdfs:label ?label .
  }
}

```

Note this is a join rather than a union. The significance is that it will not return any solutions unless it finds a match for both type and label.

-Paul
