Query timeout all living people on WikiData - sql

I have the following query to fetch all living people on wikidata but it's timing out. Any suggestions on how to optimize it or change it to make it run?
SELECT ?person ?personLabel WHERE {
?person wdt:P31 wd:Q5.
OPTIONAL { ?person wdt:P570 ?dateOfDeath }
FILTER(!BOUND(?dateOfDeath))
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Related

Get `one_of` specific items in SPAQRL

I use Wikidata as example. The following works (test it):
SELECT ?name ?nameLabel
WHERE
{
wd:Q1339 wdt:P735 ?name.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
Now, suppose I have several items, e.g. wd:Q1339, wd:Q76428 and wd:Q57487.
I want to construct a similar query but applied to exactly those items. If there was a magical one_of property (similar to a, which is a shortcut to rdf:type ), something like the following would be a solution (wrong pseudo-syntax):
SELECT ?name ?nameLabel
WHERE
{
?person one_of (wd:Q1339, wd:Q76428, wd:Q57487).
?person wdt:P735 ?name.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
Question: How can I construct a query which yields the results?
(It should preferably be applicable not only to Wikidata.)
Turning the comments from #Stanislav Kralin and #UninformedUser into an answer:
The desired keyword to assert identity of items is VALUES, see the example:
SELECT ?person ?name ?nameLabel
WHERE
{
VALUES ?person {wd:Q1339 wd:Q76428 wd:Q57487}
?person wdt:P735 ?name.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}

Query for EU universities from Wikidata times out

Asking the Wikidata service for all universities works. But when restricted to only EU universities, this query times out.
WHERE {
?item wdt:P31/wdt:P279* wd:Q3918 ;
wdt:P17 ?country.
?country wdt:P463 wd:Q458 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Any idea?
Strangely, using FILTER EXISTS is returns the result (although quite slow).
SELECT ?item ?itemLabel ?countryLabel ?country
WHERE {
?item wdt:P31/wdt:P279* wd:Q3918 ;
wdt:P17 ?country.
FILTER EXISTS { ?country wdt:P463 wd:Q458 }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Here's a link to the second query.

Wikidata query for finding presidents

I was learning the wikidata query language, and wanted to find the US president's name along with grand father, great grand father and so on...
I tried this to get a father, but how to find the father's father and so on...
SELECT ?valLabel ?resLabel
WHERE {
?val wdt:P31 wd:Q5.
?val wdt:P27 wd:Q30.
?val wdt:P106 wd:Q82955.
?val wdt:P22 wd:Q11806.
OPTIONAL { ?val wdt:P22 ?res. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Your query is looking for humans who are US citizens who are politicians and whose father is John Adams.
Instead, you want a query like this (in pseudo-SPARQL):
SELECT ?presidentLabel ?fatherLabel ?gFatherLabel ?ggFatherLabel ...
WHERE {
?president position_held president_of_the_US .
OPTIONAL{?president has_father ?father .}
OPTIONAL{?father has_father ?gFather .}
OPTIONAL{?gFather has_father ?ggFather .} ...
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Notice that the optional is necessary, as there may be no information up to the great-grandfather of a president, in which case no information about the president would be returned at all.
In Wikidata, the following should work:
SELECT
?presidentLabel
?fatherLabel
?gFatherLabel
?ggFatherLabel
WHERE {
?president wdt:P39 wd:Q11696.
OPTIONAL{?president wdt:P22 ?father}
OPTIONAL{?father wdt:P22 ?gFather}
OPTIONAL{?gFather wdt:P22 ?ggFather}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
However the query seems to time out when you look beyond the president's grandfather.
Bonus: If you want the presidents in chronological order, use this query:
SELECT ?ord ?presidentLabel ?fatherLabel ?gFatherLabel
WHERE {
?president wdt:P39 wd:Q11696 ;
p:P39 ?presidency .
?presidency ps:P39 wd:Q11696 ;
pq:P1545 ?ordString .
OPTIONAL{?president wdt:P22 ?father .}
OPTIONAL{?father wdt:P22 ?gFather .}
BIND(xsd:integer(?ordString) AS ?ord)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?ord
Try this query
#Father - GrandFather and so on ...
SELECT ?childLabel ?fatherLabel ?grandFatherLabel ?greatGrandFather1Label ?greatGrandFather2Label
WHERE {
?child wdt:P31 wd:Q5.
?child wdt:P27 wd:Q30.
?child wdt:P106 wd:Q82955.
?child wdt:P22 wd:Q11806.
OPTIONAL { ?child wdt:P22 ?father. }
OPTIONAL { ?father wdt:P22 ?grandFather. }
OPTIONAL { ?grandFather wdt:P22 ?greatGrandFather1. }
OPTIONAL { ?greatGrandFather1 wdt:P22 ?greatGrandFather2. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Here is a query with all fathers (and their fathers, as far back as there is data in Wikidata) of all US presidents:
SELECT ?presidentLabel ?fatherLabel
WHERE
{
?president wdt:P39 wd:Q11696;
wdt:P31 wd:Q5;
wdt:P22+ ?father .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!

How to retrieve aliases from wikidata

I'm trying to retrieve some information from Wikidata and I have found interesting to collect the aliases of the voices. For examples Francesco Totti is also known as il Capitano or er Pupone :
I'm trying to retrieve all the serie a's football players with this sparql query:
SELECT ?subject ?nomeLabel ?cognomeLabel ?subjectLabel WHERE {
?subject wdt:P31 wd:Q5.
?subject p:P54 ?team .
?team ps:P54 wd:""" + team_code +""" .
FILTER NOT EXISTS { ?team pq:P582 ?end
}
OPTIONAL{
?subject wdt:P735 ?nome .
?subject wdt:P734 ?cognome .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "it". }
}
ORDER BY (?cognomeLabel)
How I can modify my query to take also the aliases?
Thanks
I have attempted a query with various labels. Here just for Roma:
SELECT distinct ?subject ?subjectLabel ?nomeLabel ?cognomeLabel ?nickname ?alternative ?subjectAltLabel WHERE {
?subject wdt:P31 wd:Q5.
?subject p:P54 ?team .
?team ps:P54 wd:Q2739 .
FILTER NOT EXISTS { ?team pq:P582 ?end . }
OPTIONAL { ?subject wdt:P735 ?nome . }
OPTIONAL { ?subject wdt:P734 ?cognome . }
OPTIONAL { ?subject wdt:P1449 ?nickname . }
OPTIONAL { ?subject skos:altLabel ?alternative . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "it,en,fr". }
}
ORDER BY (?cognomeLabel)
I believe the P1449 property should be the most appropriate property to store an alias/nickname, but it does not seem to be used that much for football players. I just added "il Capitano" to Francesco Totti. Beyond that one there does not seem to be other nicknames for Roma players.
The "Also known as" label (in the right column) is not necessarily the nickname, but may be a spelling variation.
Something more generic if someone is interested in all properties that will return only the english also known as:
SELECT ?property ?propertyLabel ?propertyDescription (GROUP_CONCAT(DISTINCT(?altLabel); separator = ", ") AS ?altLabel_list) WHERE {
?property a wikibase:Property .
OPTIONAL { ?property skos:altLabel ?altLabel . FILTER (lang(?altLabel) = "en") }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .}
}
GROUP BY ?property ?propertyLabel ?propertyDescription
LIMIT 5000
Another more simple example for male actors with itemAltLabel :
#Male Actors
SELECT ?item ?itemLabel ?itemAltLabel
WHERE
{
?item wdt:P21 wd:Q6581097.
?item wdt:P106 wd:Q33999.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Wikidata: list every physical objects

I'm trying to get the name of the all physical things (tangible concepts) Wikidata knows about (objects, places, countries, etc), or in other words everything non-abstract.
There are examples close to what I need, but with only a depth of one: all the things that are instances of phone.
I found this example that searches with more depth and I modified the start point to entity:
#Children of Genghis Khan
#added before 2016-10
#defaultView:Graph
PREFIX gas: <http://www.bigdata.com/rdf/gas#>
SELECT ?item ?itemLabel ?pic ?linkTo
WHERE
{
SERVICE gas:service {
gas:program gas:gasClass "com.bigdata.rdf.graph.analytics.SSSP" ;
gas:in wd:Q35120 ;
gas:traversalDirection "Forward" ;
gas:out ?item ;
gas:out1 ?depth ;
gas:maxIterations 4 ;
gas:linkType wdt:279 .
}
OPTIONAL { ?item wdt:P40 ?linkTo }
OPTIONAL { ?item wdt:P18 ?pic }
SERVICE wikibase:label {bd:serviceParam wikibase:language "en" }
}
I still get no results.
As mentionned in a comment, your question is too broad and you'll end up with too much answers
You look for the A that are instance of a subclass of B
so the query you point to is the right one
SELECT DISTINCT ?item
WHERE {
?item wdt:P31/wdt:P279* wd:Q35120
}
the problem is the size
wd:Q35120 has a lot of subclasses
You can check that this way
SELECT ?a ?aLabel WHERE { ?a wdt:P279 wd:Q35120.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
SELECT ?a ?aLabel WHERE { ?a wdt:P279/wdt:P279? wd:Q35120.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
SELECT ?a ?aLabel WHERE { ?a wdt:P279/wdt:P279?/wdt:P279? wd:Q35120.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
And so on : you'll see that there are already 40'000+ at forth level which is huge
You could also huge this nice tool to have a more precise view
https://tools.wmflabs.org/bambots/WikidataClasses.php?id=Q35120&lang=en