Search code examples
sparqldbpedia

List countries from DBpedia


Trying to query DBpedia for a list of all countries with the dbo:longName property and the capital of each country listed but get 0 results returned. Can't see whats wrong with the query.

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>

SELECT ?country ?capital
WHERE {
 ?country a dbo:longName ;
    dbo:capital ?capital .
}

What's missing?


Solution

  • You are missing that ?country has a rdf:type of dbo:Country and not dbo:longName. The right query should be:

    PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX dbr: <http://dbpedia.org/resource/>
    
    SELECT ?country ?capital
    WHERE {
     ?x a dbo:Country.
     ?x dbo:longName ?country.
     ?x dbp:capital ?capital
    }
    

    Update

    Based on your comment you want the URI's of the country and their capital. Therefore, you don't need dbo:longName, because you don't want the country label name. You will select the instances:

    PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX dbr: <http://dbpedia.org/resource/>
    
    SELECT ?country ?capital
    WHERE {
     ?country a dbo:Country.
     ?country dbo:capital ?capital
    }
    

    Note that results will bring countries that are extinct. If you want to filter countries that have ended, you should get this result with:

    PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX dbr: <http://dbpedia.org/resource/>
    
    SELECT ?country ?capital
    WHERE {
     ?country a dbo:Country.
     ?country dbo:capital ?capital.
     FILTER NOT EXISTS { ?country dbo:dissolutionYear ?yearEnd }
    }