Wikidata SPARQL query of COUNTRIES who were FORMER COLONIES of UNITED KINGDOM - sparql

I am trying to display all the former colonies of the British Empire. I have just managed to retrieve a country which is currently a colony. I am trying to get my head around the query in order to embrace either no longer a colony, or colony during a specific time period.
SELECT ?countryLabel WHERE {
?country wdt:P31 wd:Q6256, #Country
wd:Q133156. #Colony
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Any help is greatly appreciated!

Related

Unexpected missing value from Wikidata query

I'm using the Wikidata query service to learn the SPARQL query language. I'm trying to get information on countries and their identifying information.
Here is a simple query which is intended to return a list of countries (https://www.wikidata.org/wiki/Q6256) along with their ISO 3-letter codes (https://www.wikidata.org/wiki/Property:P298):
SELECT ?country ?countryLabel ?iso
WHERE
{
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
?country wdt:P31 wd:Q6256; # wd:Q6256="country; wd:Q3624078="sovereign state"
wdt:P298 ?iso.
}
ORDER BY ?countryLabel
I notice that at least one country is consistently missing from the results, Georgia, and I'm confused about why.
According to its wikidata page:
It is an instance of country (wd:Q6256)
It does have an ISO-3166 3-letter country code (wdt:P298)
I've tried various transformations of this query (e.g. don't include the ISO codes, use labels in different languages, etc) and I consistently get the same result: Georgia is missing.
However if I switch from (instance of a country wd:Q6256) to (instance of a sovereign state wd:Q3624078; a subclass of wd:Q6256), then Georgia is included in the results.
I am at a loss to explain this result; the entity in question should be an instance of both "country" and "sovereign state." And clearly it works for most of the other countries of the world, whose data is represented similarly in Wikidata, in that they're listed as instances of both country wd:Q6256 and sovereign state wd:Q3624078.
Can anyone explain what aspect of the SPARQL language, or representation of the data in question, that I'm not understanding here?
The claim for instanceOf Sovereign State has a PreferredRank, so it's selected in preference to all the other claims which have a NormalRank. Also, SPARQL doesn't do inheritance by default unless you explicitly bake it into the query (because it can be expensive), so you don't automatically get Sovereign State just because it's a subclass of Country.
This will include Georgia
SELECT ?country ?countryLabel ?iso
WHERE
{
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
?country p:P31/ps:P31 wd:Q6256; # wd:Q6256="country; wd:Q3624078="sovereign state"
wdt:P298 ?iso.
}
ORDER BY ?countryLabel
but note that it includes deprecated claims as well. I cribbed it from this set of examples: https://en.wikibooks.org/wiki/SPARQL/WIKIDATA_Qualifiers,_References_and_Ranks
As mentioned by #horcrux in the comments, you can modify this to exclude deprecated claims by using a FILTER expression:
SELECT ?country ?countryLabel ?iso
WHERE
{
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
FILTER(?rank != wikibase:DeprecatedRank) . ?country p:P31 [ ps:P31 wd:Q6256 ; wikibase:rank ?rank ] ;
wdt:P298 ?iso.
}
ORDER BY ?countryLabel
The results are the same in this case, but it's something worth thinking about when you're considering what kind of data you're looking for.

Retrieving all living people from wikipedia/wikidata

I have the following query from the WikiData query builder to fetch people, but I would like to fetch only people who are alive. I've tried playing around with date of death but I can't get anything that seems to work, I'm not very familiar with SPARQL.
SELECT DISTINCT ?item ?itemLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
{
SELECT DISTINCT ?item WHERE {
?item p:P31 ?statement0.
?statement0 (ps:P31/(wdt:P279*)) wd:Q215627.
}
LIMIT 100
}
}
https://query.wikidata.org
I'd optimally want to return the top 50,000 living people sorted by some sort of metric of popularity. If there's a way to do this by API or some other pre-built wrapper around Wikidata I'd appreciate that as well. Thanks!

Need help fetching a person's age from wikidata query using sparql

I recently started working on sparql to generate query for getting the age of a specific person (say, Donald Trump)
The only thing I could understand is that it would be an instance of Person class.
Can someone help me with what else needs to be done to get that specific information.
SELECT ?item WHERE {
?item wdt:P31 wd:Q5.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Wikidata SPARQL query timeout: country flags sorted by population

With the following SPARQL query I’m trying to list countries with their national flags in descending order of population.
I cannot run it without it reaching a timeout limit. It runs in ~2s when the indicated line is commented out (but this returns the cartesian product of all countries and all national flags, not just the associated pairs).
SELECT ?country ?countryLabel ?flag ?population
WHERE {
?country wdt:P31 wd:Q6256;
wdt:P1082 ?population.
?flag wdt:P31 wd:Q186516.
?flag wdt:P1001 ?country. # runs without this line
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY DESC(?population)
LIMIT 100
Try it here
Does anyone know why this query is to complex to compute, and how to get it to run? Thanks!

Select members of parliament with SPARQL from Wikidata

Based on wikidata I want to make a list of all members of the European Parliament and I want some metadata about their membership like start date and the party they represent.
As a start I run the following query:
SELECT ?human ?humanLabel ?positionheldLabel
WHERE
{
# human position_held MembEuroParl
?human wdt:P39 wd:Q27169;
wdt:P39 ?positionheld.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
This returns a list of people who once were member of european parliament and the positions they held. However also if those positions were not Member of parliament. See image.
So I change the query to the following adding a line that the positionheld sould be member of parliament:
SELECT ?human ?humanLabel ?positionheld
WHERE
{
# ?human position_held MEP
?human wdt:P39 wd:Q27169;
wdt:P39 ?positionheld.
?positionheld wdt:P31 wd:Q27169.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
However, this last query does not return any results. Besides, it feels repetitive
My question how can I select only those rows where positionheld is member of parliament.
The answer is probably trivial, yet currently I am left clueless. Something I wanted to take 1 minutes is taking an hour.