I want a SPARQL query to retrieve the official languages of a country, say for Afghanistan:
SELECT ?langCode ?itemLabel WHERE {
wd:Q889 wdt:P37 ?officialLanguage.
?officialLanguage rdfs:label ?itemLabel.
OPTIONAL { ?officialLanguage wdt:P424 ?langCode. }
FILTER(LANG(?itemLabel) = "en")
}
That query works as far as it goes and returns nine results. However, most of these (e.g. Balochi and Nuristani) are marked as "regional language" (Q770220) in Wikidata, using the "applies to part" property (P518). How can I exclude these from my results?
I tried a number of things already, using MINUS
or FILTER NOT EXISTS
, but wasn't able to get it working.
For handling qualifiers, you can use the following prefixes:
p:
for linking an item with a statement;ps:
a statement with its object;pq:
a statement with its qualifier(s).Thus, the query that you want takes this form:
SELECT ?langCode ?itemLabel WHERE {
wd:Q889 p:P37 ?officialLanguageStatement .
?officialLanguageStatement ps:P37 ?officialLanguage .
?officialLanguage rdfs:label ?itemLabel .
OPTIONAL { ?officialLanguage wdt:P424 ?langCode . }
FILTER(LANG(?itemLabel) = "en")
FILTER NOT EXISTS {
?officialLanguageStatement pq:P518 wd:Q770220 .
}
}
See also: