Retrieving all living people from wikipedia/wikidata - sparql

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!

Related

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.

SPARQL - How to find every entity inside two categories and its subcategories

I'm trying to find every public university in Brazil, using Wikidata. The problem is that some of them are categorized as instance of university (Q3918) and public educational institution (Q23002037), but others are categorized as public university (Q875538), and some might be under different categories even.
So I figured if I got every entity in a subclass of public educational institution (Q23002037) and in a subclass of public educational institution (Q23002037), I'd get all the entities I needed. So I tried this, with some optimization:
SELECT ?uni ?uniLabel
WHERE {
hint:Query hint:optimizer "None".
?uni wdt:P17+ wd:Q155;
wdt:P31/wdt:P279 wd:Q38723;
wdt:P31/wdt:P279 wd:Q23002037.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],pt". }
}
Run this code here
However, this only returns 15 entities. One of the entities missing is Federal University of Cariri (Q10387824), which is an entity of university and of public educational institution so it should have appeared in the query results. Can anyone help me understand what's going on, and why that entity and so many others do not show up in my results?
Thank you in advance. I'm very new to SPARQL and Wikidata Queries.
I'm not sure how this works, but I used the Wikidata Query Builder and it worked. I got the entities I wanted in the categories and subcategories I defined.
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:Q38723.
?item p:P31 ?statement1.
?statement1 (ps:P31/(wdt:P279*)) wd:Q23002037.
?item p:P17 ?statement2.
?statement2 (ps:P17) wd:Q155.
}
LIMIT 100
}
}
Run this code here
So yeah. Use the Wikidata Query Builder, then open it as a Wikidata Query Service, and click the (i) "Show query explanation" button, and you can choose what information to display.

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

Filter search in Wikidata API

I am trying to get filtered data from the Wikidata API, currently I can do a general search using this API, but now there have been specific cases where I have to filter this information, for example, I need to get a list of only authors to get the Q identifier and although I also reviewed the Wikidata Query Service this is too heavy to bring all the items, I used a SPARQL query and did a test and to get less than 3000 results it took 26 seconds, this is too much for a search service.
This is the query I use to get the authors.
SELECT DISTINCT ?author ?authorLabel WITH {
SELECT ?item ?author WHERE {
?item wdt:P50 ?author.
} LIMIT 100000
} AS %FOO {
INCLUDE %FOO
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
I also need to search by categories but it has not been possible for me to filter the searches in any way, does anyone know a way to do it?

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.