Search code examples
sparqlwikidata

SPARQL query does not yield expected results


I'm using this SPARQL query to obtain a list of European countries:

SELECT ?item $itemLabel
    WHERE {
        ?item wdt:P31 wd:Q6256.
        #?item wdt:P30 wd:Q46
        #?item wdt:P361* wd:Q46.
        ?item wdt:P30|wdt:P361* wd:Q46.
        SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    }
    ORDER BY ASC($itemLabel)
  • Line 3 limits the results to instances of Q6256, i.e. countries.
  • Line 4 (currently commented out) limits the results to items which are on the continent Q46 (Europe).
  • Line 5 (currently commented out) limits the results to items which are part of Europe, or which are part of something that is part of Europe, etc.
  • Line 6 is an OR-clause which combines line 4 and 5.

I'm running the query with one of the lines 4-6 active, and the other two commented out.

No matter which predicate is active, Austria is not part of the results (other countries are missing as well). However, looking at https://www.wikidata.org/wiki/Q40, we can see that

  • Austria is an instance of Q6256.
  • Austria's continent (P30) is Europe.
  • Austria is part of Central Europe (Q27509), which is part of Europe.

What am I doing wrong?

Here's the relevant query.


Solution

  • Austria is actually not declared as an instance of country (Q6256). It is declared as an instance of sovereign state (Q3624078), which is a subclass (P279) of country. The instance relationship to country that can be seen on Austria's page is inferred, and not available when queried.

    To fix this, we can query for items that are instances of country or its subclasses:

    ?item wdt:P31/wdt:P279* wd:Q6256.
    

    Unfortunately this also pulls in historical countries, so the query will need some additional work to filter those out (e.g., with FILTER NOT EXISTS { ... }). It also needs SELECT DISTINCT.