Search code examples
sparqlsemantic-weblinked-data

Good SPARQL query to find all triples with a resource as subject or object


I need to find all triples on DBpedia where http://dbpedia.org/resource/Benin is a subject or object. This query gives me the output that I want in a format that works the best for me (just three variables and no blank spaces):

PREFIX : <http://dbpedia.org/resource/>
SELECT * WHERE {
?s ?p ?o
FILTER (?s=:Benin OR ?o=:Benin)
}

I get similar results if I have this query:

PREFIX : <http://dbpedia.org/resource/>
SELECT * WHERE {
{:Benin ?p ?o}
UNION
{?s ?p :Benin}
}

However, the formatting of the latter is off. It first gives me p and o output leaving s blank and then s and p leaving o blank. Also, the first query takes more time to execute. I will be grateful for an explanation of the mechanics of how the two queries work and why there is a difference in the output.


Solution

  • Try this --

    PREFIX : <http://dbpedia.org/resource/>
    DESCRIBE  :Benin
    

    -- or just --

    DESCRIBE  <http://dbpedia.org/resource/Benin>
    

    You can get the output in various other serializations, including N-triples.