Select members of parliament with SPARQL from Wikidata - sparql

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.

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.

How to search a list of Wikidata IDs and return the P31 ("instance of") property using SPARQL?

How do I get the instance type(s) (i.e., property=P31 and associated labels) for multiple Wikidata IDs in a single query? Ideally, I want to output a list with the columns: Wikidata ID | P31 ID | P31 Label, with multiple rows used if a Wikidata ID has more than one P31 attached.
I am using the web query service, which works well in part, but I am struggling to understand the syntax. I have so far managed to work out how to process a list of items, and return each one as a row (simple I know!), but I can't work out how to generate a new column that gives the P31 item:
SELECT ?item
WHERE {
VALUES ?item { wd:Q1347065 wd:Q731635 wd:Q105492052 }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
I have found the following from a previusly answered question here, which returns multiple rows per an item of interest, but this requires specifying the P31 type at the outset, which is what I am looking to generate.
Any help would be appreciated as I am really stuck understanding the syntax.
Update:
I have now worked out how to return P31s for a single ID. I need to expand this query to receive a list of IDs, and include the ID as a column:
SELECT ?item ?itemLabel
WHERE
{
wd:Q18656 wdt:P31 ?item.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
If I correctly understood your problem, you can use the following query:
SELECT ?item ?class ?classLabel
WHERE {
VALUES ?item { wd:Q1347065 wd:Q731635 wd:Q105492052 }
?item wdt:P31 ?class .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Here, first you fix the possible values for ?item, then you say that ?item is instance of a certain ?class and contestually you also retrieve the label for such ?class.

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". }
}

On wikidata SPARQL get coordinates in degree?

I want to get coordinates via SPARQL from wikidata displayed as degrees (e.g. 54°54'36"N). It is displayed in wikidata like this, so I suspect there is a built-in function for this purpose but I can not find it.
Example query:
SELECT DISTINCT ?countryLabel ?long
{
?country wdt:P31 wd:Q6256 ;
p:P1332 [ psv:P1332 [wikibase:geoLatitude ?long ]].
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
This gives the longitude as number (e.g. -30.08). I can calculate the desired output format from this result but would prefer to get it directly from the query.
Thanks.

Sparql, how to select attributes from entity

This is probably pretty simple but I can't figure out how to do it. I have a query that selects all information about a rockband. I want to select just some of the attributes, for example the name of the band. Any help is appriciated.
And I also want the information to be in english only.
SELECT * WHERE {
wd:Q856941 ?p ?o.
#?o wdt:P571 ?inception.
#?o wdt:P154 ?logo
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}