Search code examples
javasparqldbpedia

use union in sparql query


I've got this SPARQL query between 2 DBpedia resources: France and Alstom

select * where{
<http://dbpedia.org/resource/France> ?pre ?obj.

?obj ?pre1 <http://dbpedia.org/resource/Alstom>
}

and it returns nothing

but if I reverse the subject and the object like this:

select * where{
<http://dbpedia.org/resource/Alstom> ?pre ?obj.
?obj ?pre1 <http://dbpedia.org/resource/France>
}

the query returns some results..

SPARQL Result

is there any way to merge the two queries using UNION?


Solution

  • Based on your comment, you just need a straightforward union with the following syntax:

    prefix dbpedia: <http://dbpedia.org/resource/>
    select * where{
    {
        dbpedia:France ?pre ?obj.
        ?obj ?pre1 dbpedia:Alstom.
    }
    Union {
        dbpedia:Alstom ?pre ?obj.
    ?obj ?pre1 dbpedia:France
    }
    }