Jonas Sourlier
Jonas Sourlier

Reputation: 14435

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)

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

What am I doing wrong?

Here's the relevant query.

Upvotes: 2

Views: 120

Answers (1)

cygri
cygri

Reputation: 9472

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.

Upvotes: 2

Related Questions