Wikidata SPARQL Query. Not all properties in the result - sparql

Can anybody help me to understand why the ?sEnd attribute is empty for the last record here?
SELECT ?sLabel ?sStart ?sEnd
WHERE {
BIND(wd:Q32522 as ?p).
?s wdt:P26 ?p .
OPTIONAL { ?s p:P26 [pq:P580 ?sStart] }
OPTIONAL { ?s p:P26 [pq:P582 ?sEnd] }
SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' }
}
Basically, what I want to retrieve is Jennifer Aniston's spouses with marriage start and end dates.
Thanks in advance!

The issue seems to be that you are querying for the marriage dates of Jennifer Aniston's spouses, as opposed to hers directly.
Your query returns some odd results, for two different reasons:
1.Brad Pitt was married a second time, to Angelina Jolie (2014-19).
Your query is therefore returning those dates as well.
2.The data for Justin Theroux and Jennifer Aniston are inconsistent (this is a Wikidata issue). In particular, as #UninformedUser pointed out, Justin's page has no end date for their marriage.
Jennifer Aniston's data on her marriage with Justin is more specific than his data and has both start and end date.
Reasoning is something that can be used to deal with this sort of issues, and some triplestores have it.
The query you need is this nonetheless:
SELECT ?spouseLabel ?sStart ?sEnd
WHERE {
BIND(wd:Q32522 as ?person).
?person p:P26 ?marriage .
?marriage pq:P580 ?sStart ;
ps:P26 ?spouse .
OPTIONAL{?marriage pq:P582 ?sEnd}
SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' }
}
This gives the required result. Notice that the namespace for the properties of the wedding changes depending on what the object is.
See here for more.

Related

Get all properties, sub-properties and label IDs from Wikidata item

I am trying to write a query that returns all possible information from a wikidata page, as https://www.wikidata.org/wiki/Q1299.
Ideally I would like to retrieve all info that are present in that page in english language.
So I am trying this query:
SELECT ?wdLabel ?ps_Label ?wdpqLabel ?pq_Label WHERE {
VALUES ?artist {
wd:Q1299
}
?artist ?p ?statement.
?statement ?ps ?ps_.
?wd wikibase:claim ?p;
wikibase:statementProperty ?ps.
OPTIONAL {
?statement ?pq ?pq_.
?wdpq wikibase:qualifier ?pq.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
which works quite fine but I would like to retrieve all wikidata ids on the ps_Label column.
For example here I have Paul McCartney as string and I would like to also have the wikidata ID associated to that item, which is Q2599
has part Paul McCartney start time 1960-01-01T00:00:00Z
has part Paul McCartney end time 1970-01-01T00:00:00Z
has part Paul McCartney object has role singer
has part Paul McCartney object has role instrumentalist
Something similar to this other below but I can't merge the two together as I am missing some hint on sub-properties here.
SELECT ?propUrl ?propLabel ?valUrl ?valLabel WHERE {
wd:Q1299 ?propUrl ?valUrl.
?property ?ref ?propUrl;
rdf:type wikibase:Property;
rdfs:label ?propLabel.
?valUrl rdfs:label ?valLabel.
FILTER((LANG(?valLabel)) = "en")
FILTER((LANG(?propLabel)) = "en")
}
ORDER BY (?propUrl) (?valUrl)
Thanks a lot for your help!
This isn’t exactly what you’re asking for, but if you really just want all data on a single object, it is far easier and more economical to use the data access provided by https://www.wikidata.org/wiki/Special:EntityData/Q2599.json

Easiest way to query if a name exists in Wikidata?

I'm trying to create the simplest possible query to check if a name exists in Wikidata. For example, I just to want to see if the common name "Jack Smith" is in Wikidata.
Following the example query in this StackOverflow answer, I created the following SPARQL query (run it):
SELECT distinct ?item ?itemLabel ?itemDescription WHERE{
?item ?label "Jack Smith" .
?article schema:about ?item .
?article schema:inLanguage "en" .
?article schema:isPartOf <https://en.wikipedia.org/>.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} LIMIT 10
However, it returns zero results.
On the other hand, if I search for "Jack Smith" through the Wikidata search webpage, I get back 1200+ results with a lot of people named "Jack Smith".
The behavior with the SPARQL interface is inconsistent. When I run a SPARQL search for "Abraham Lincoln" (run it), I correctly get back an entry for the American president by that name.
My specific questions are:
Why is my SPARQL query returning zero results for "Jack Smith"?
What is the simplest API call to check if a name (e.g. Jack Smith) exists in Wikidata? It could be SPARQL or some other REST API.
Thank you.
The query returns zero results because there is no Jack Smith about whom there is an article in English and is part of Wikipedia.org.
Every line in the body of a SPARQL query is a way to restrict the search as opposed to just a way to add new variables.
Look at this query:
SELECT ?person ?dateOfDeath
WHERE {
?person a :Person .
?person :date_of_death ?dateOfDeath .
}
This would only return people who have a date of death, i.e. who are dead, as well as the date.
If you wanted to return people, and optionally their date of death, i.e. if they are still alive, you don't want a date, but you do want the person, then use this:
SELECT ?person ?dateOfDeath
WHERE {
?person a :Person .
OPTIONAL {?person :date_of_death ?dateOfDeath }
}
In terms of your second question, I'd try something like this:
SELECT ?boolean
WHERE{
BIND(EXISTS{?item ?label "Jack Smith"} AS ?boolean)
}
This can of course also be issued as, say, a cURL request:
curl https://query.wikidata.org/bigdata/namespace/wdq/sparql -X POST --data 'query=SELECT%20%3Fboolean%0AWHERE%7B%0ABIND%28EXISTS%7B%3Fitem%20%3Flabel%20%22Jack%20Smith%22%7D%20AS%20%3Fboolean%29%0A%20%20%7D'

Inconsistent Wikidata query results [duplicate]

While examining the results of the official example query "Continents, countries, regions and capitals" (on https://query.wikidata.org/, limited to Germany for your convenience here: link), I noticed that some capitals of German federal states were missing. For example Wiesbaden as capital of Hesse. I noticed that Wiesbaden is an instance of big city, but not of city (see https://www.wikidata.org/wiki/Q1721), in contrast to some other cities. I was able to alleviate the problem by also including cities that are subclasses of city by changing line 17 to ?city wdt:P31/wdt:P279? wd:Q515.
One of the four cities that are still missing is Magdeburg, the capital of Saxony-Anhalt.
The diagnostic query
SELECT ?cityLabel ?props
WHERE {
?city wdt:P31 ?props.
FILTER(?city = wd:Q1733 || ?city = wd:Q1726).
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
shows that Magdeburg is not even an instance of city, although it clearly is according to its Wikidata page https://www.wikidata.org/wiki/Q1733.
I am new to Wikidata and SPARQL. However, this seems wrong to me. What can I do to get all capitals of the german federal states? And what is the reason for this behaviour?
These missing statements are not truthy:
SELECT ?statement ?valueLabel ?rank ?best
WHERE {
wd:Q1733 p:P31 ?statement.
?statement ps:P31 ?value .
?statement wikibase:rank ?rank .
OPTIONAL { ?statement a ?best . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!
They are normal-rank statements, but there is a preferred-rank statement.
Truthy statements represent statements that have the best
non-deprecated rank for given property. Namely, if there is a
preferred statement for property P2, then only preferred statements
for P2 will be considered truthy. Otherwise, all normal-rank
statements for P2 are considered truthy.
Update
I have decreased the rank of the preferred statement just now. Please test your query again.

Wikidata SPARQL query not returning a known match [duplicate]

While examining the results of the official example query "Continents, countries, regions and capitals" (on https://query.wikidata.org/, limited to Germany for your convenience here: link), I noticed that some capitals of German federal states were missing. For example Wiesbaden as capital of Hesse. I noticed that Wiesbaden is an instance of big city, but not of city (see https://www.wikidata.org/wiki/Q1721), in contrast to some other cities. I was able to alleviate the problem by also including cities that are subclasses of city by changing line 17 to ?city wdt:P31/wdt:P279? wd:Q515.
One of the four cities that are still missing is Magdeburg, the capital of Saxony-Anhalt.
The diagnostic query
SELECT ?cityLabel ?props
WHERE {
?city wdt:P31 ?props.
FILTER(?city = wd:Q1733 || ?city = wd:Q1726).
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
shows that Magdeburg is not even an instance of city, although it clearly is according to its Wikidata page https://www.wikidata.org/wiki/Q1733.
I am new to Wikidata and SPARQL. However, this seems wrong to me. What can I do to get all capitals of the german federal states? And what is the reason for this behaviour?
These missing statements are not truthy:
SELECT ?statement ?valueLabel ?rank ?best
WHERE {
wd:Q1733 p:P31 ?statement.
?statement ps:P31 ?value .
?statement wikibase:rank ?rank .
OPTIONAL { ?statement a ?best . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!
They are normal-rank statements, but there is a preferred-rank statement.
Truthy statements represent statements that have the best
non-deprecated rank for given property. Namely, if there is a
preferred statement for property P2, then only preferred statements
for P2 will be considered truthy. Otherwise, all normal-rank
statements for P2 are considered truthy.
Update
I have decreased the rank of the preferred statement just now. Please test your query again.

Some cities aren't instances of city or big city? Odd behaviour of Wikidata

While examining the results of the official example query "Continents, countries, regions and capitals" (on https://query.wikidata.org/, limited to Germany for your convenience here: link), I noticed that some capitals of German federal states were missing. For example Wiesbaden as capital of Hesse. I noticed that Wiesbaden is an instance of big city, but not of city (see https://www.wikidata.org/wiki/Q1721), in contrast to some other cities. I was able to alleviate the problem by also including cities that are subclasses of city by changing line 17 to ?city wdt:P31/wdt:P279? wd:Q515.
One of the four cities that are still missing is Magdeburg, the capital of Saxony-Anhalt.
The diagnostic query
SELECT ?cityLabel ?props
WHERE {
?city wdt:P31 ?props.
FILTER(?city = wd:Q1733 || ?city = wd:Q1726).
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
shows that Magdeburg is not even an instance of city, although it clearly is according to its Wikidata page https://www.wikidata.org/wiki/Q1733.
I am new to Wikidata and SPARQL. However, this seems wrong to me. What can I do to get all capitals of the german federal states? And what is the reason for this behaviour?
These missing statements are not truthy:
SELECT ?statement ?valueLabel ?rank ?best
WHERE {
wd:Q1733 p:P31 ?statement.
?statement ps:P31 ?value .
?statement wikibase:rank ?rank .
OPTIONAL { ?statement a ?best . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!
They are normal-rank statements, but there is a preferred-rank statement.
Truthy statements represent statements that have the best
non-deprecated rank for given property. Namely, if there is a
preferred statement for property P2, then only preferred statements
for P2 will be considered truthy. Otherwise, all normal-rank
statements for P2 are considered truthy.
Update
I have decreased the rank of the preferred statement just now. Please test your query again.