SPARQL query GRAPH unions (maybe through virtual graph)

Suppose you have this data:

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

and want something like:

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:

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

1 Like