I am trying to query wikidata for population numbers of all sub-country administrative units. If I manually look up a Dutch province or a Russian Oblast or a French district, this information appears to be available for most of such sub-country regions.
I created the following query:
SELECT ?land ?landLabel ?is_een ?is_eenLabel ?inwonertal WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?land wdt:P31 wd:Q56061.
OPTIONAL { ?land wdt:P17 ?land. }
OPTIONAL { ?land wdt:P31 ?is_een. }
OPTIONAL { ?land wdt:P1082 ?inwonertal. }
}
limit 10000
where Q56061 is a "administrative territorial entity", P17 (land = country), P31 (is_een = is_a), P1082 (inwonertal = population)
This query returns only 4271 results. Why does the query miss for instance https://www.wikidata.org/wiki/Q694 (Province of South Holland)? Maybe because Q694 is an instance of Q134390 (province of the Netherlands) which is sub-class of Q56061 (administrative territorial entity)?
What should the query look like to filter not only for Q56061 (adm territory entity) but also for all of the subclasses of Q56061? To limit the ouput, I would preferably filter only for the subclasses of Q56061 that are one geographic level below Q6256 (country). Can that be done?
With ?land wdt:P31 wd:Q56061
you query only for instances of Q56061 ("administrative territorial entity").
Most entities, like https://www.wikidata.org/wiki/Q134390, are instance of a subclass of Q56061.
To include such cases, you need to adapt the query as follows:
?land wdt:P31/wdt:P279 wd:Q56061
. (P279 is the Wikidata property for subclasses.)
Often you want includes subclasses of subclasses (and any number of levels down). This can be achieved by writing ?land wdt:P31/wdt:P279* wd:Q56061
. However, for your example this needs more than 60s of execution time and thus it does not succeed.
To limit the output to entites one geographic level below country, you can use https://www.wikidata.org/wiki/Q10864048 ("first-level administrative country subdivision"):
SELECT ?land ?landLabel ?is_een ?is_eenLabel ?inwonertal WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?land wdt:P31/wdt:P279* wd:Q10864048.
OPTIONAL { ?land wdt:P17 ?land. }
OPTIONAL { ?land wdt:P31 ?is_een. }
OPTIONAL { ?land wdt:P1082 ?inwonertal. }
} limit 10000