Getting labels/names and invert wikidata query - sparql

I'am trying to get the names of the victims, which got a "stolperstein" (stumblestone) in my hometown. But I can't get the names with SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
I think, the query should be
SELECT ?Stolperstein ?StolpersteinLabel ?bild ?Person ?tod WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?Stolperstein wdt:P31 wd:Q26703203;
wdt:P131 wd:Q2107;
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
wdt:P547 ?Person.
But it doesn't work. What am I doing wrong?
My second question is, how can I display the data with missing information. (invert the query / like NOT IN)
SELECT ?Stolperstein ?StolpersteinLabel ?bild ?Person ?tod WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?Stolperstein wdt:P31 wd:Q26703203;
wdt:P131 wd:Q2107;
wdt:P547 ?Person.
?Person wdt:P570 ?tod
gives me only 88 results, but there are 188 "stones", so therefore in 100 datasets is the information date of death (P570) missing.

Related

How to create a SPARQL query to search Wikidata item descriptions

I'd like to be able to find an item based on words in its descriptions.
This is what I'm trying to do, but clearly I don't know what I'm doing. Any help is appreciated.
SELECT ?item ?itemLabel WHERE {
?item schema:description ?desc.
FILTER(CONTAINS(LCASE(?desc), "space telescope"))
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 10
Solved by UninformedUser in the comments!
WHERE { hint:Query hint:optimizer "None".
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:api "Search";
wikibase:endpoint "www.wikidata.org";
mwapi:srsearch "space telescope".
?item wikibase:apiOutputItem mwapi:title . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
limit 10

WIKIDATA/SPARQL: How to get the used language when using SERVICE wikibase:label with [AUTO_LANGUAGE] parameter

About everything in the title : When I use
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } I may get some results with the detected [AUTO_LANGUAGE] and others in 'en'.
Is it possible to get the used language for each of the lines of the result ?
Yes. LANG(?var) does that:
SELECT ?item ?itemLabel (LANG(?itemLabel) as ?langLabel)
WHERE {
?item wdt:P31 wd:Q146.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "de,fr,en".
}
}

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!

Find which direct property applied in a SPARQL query

I have a list of properties I want to apply to a specific entity mathematics: wd:Q395. In this case:
instanceOf: 'wdt:P31'
subclassOf: 'wdt:P279'
The results are:
Mathematics is instance of academic discipline and
Mathematics is subclass of exact science and formal science
Instead of making two different queries I would like to make them all at once:
SELECT ?field ?fieldLabel ?propertyApplied
WHERE {
wd:Q395 wdt:P31 | wdt:P279 ?field.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
BIND("" AS ?propertyApplied)
}
How can I know which property applied to fill the right column? ( for example next to academic discipline I would like that it appears instance of)
I tried this but it looks weird and the results repeat themselves.
SELECT ?instanceOf ?subclassOf ?instanceOfLabel ?subclassOfLabel
WHERE {
OPTIONAL { wd:Q395 wdt:P31 ?instanceOf. }
OPTIONAL { wd:Q395 wdt:P279 ?subclassOf. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Use VALUES or UNION:
SELECT ?field ?fieldLabel ?propertyLabel WHERE {
VALUES (?predicate) {(wdt:P31) (wdt:P279)}
wd:Q395 ?predicate ?field .
?property wikibase:directClaim ?predicate .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!
SELECT ?field ?fieldLabel ?propertyLabel {
{ wd:Q395 wdt:P31 ?field . BIND (wd:P31 AS ?property) }
UNION
{ wd:Q395 wdt:P279 ?field . BIND (wd:P279 AS ?property) }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!

Find people whose name start with a particular letter with sparql Wikidata?

This is my query but I don't get any results for records starting with "n"
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P31/wdt:P279* wd:Q5.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
FILTER regex (?itemLabel, "^(n)").
}