Select all statements using alternate identifier in Wikidata SPARQL - sparql

When I use a Q ID to do a query, I can get all the statements and identifiers -- EVERYTHING, e.g.
SELECT ?value ?valueLabel ?prop ?propLabel {
wd:Q112133367 ?prop ?value .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
This query successfully returns about 30 rows of data.
However, that assumes that I know the Q ID in advance. I am trying to retrieve the same results, but using an external identifier, e.g. the IMDb ID because I don't know the Q ID in advance.
This query works to look up the Q ID given the IMDb external ID:
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P345 "tt13276352"
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
But this only returns a single row of data (the Q ID). I have tried adding some additional values to the query, but the query times out, e.g.
SELECT ?item ?itemLabel ?foo ?prop ?value
WHERE
{
?item wdt:P345 "tt13276352" .
?foo ?prop ?value .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
I am stuck thinking about this too much from the point of view of a relational database so I don't see how to select all the same bits of data as when I use the Q ID.

The problem is that you have introduced an extra variable ?foo.
Re-use the ?item variable instead:
SELECT ?item ?itemLabel ?prop ?value
WHERE
{
?item wdt:P345 "tt13276352" .
?item ?prop ?value .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
Link to Wikidata Query Service for the above.

Related

Wikidata SPARQL filter properties by id

I have the following sparql:
#Cats
SELECT ?item ?itemLabel ?prop
WHERE
{
?item ?prop wd:Q7206.
filter not exists {?prop **FILTER** wdt:P921 }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
# Helps get the label in your language, if not, then en language
}
I want to return relations excluding P921. What should I enter instead of FILTER?

How to return null results in SPARQL?

I'm using the Wikidata SPARQL Query Service with the following query:
SELECT ?item ?itemLabel ?class ?classLabel ?projectLabel WHERE {
VALUES ?item { wd:Q1 wd:Q367204 }
?item wdt:P31 ?class;
wdt:P18 ?project.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,en". } }
link to the query
When running the query there is a result for Q1 because there is an image, but there is no result for Q367204 because there isn't an image.
My question: How can I get results for both Q1 and Q367204 regardless if there is an image available or not?
wd:Q367204 is missing from the results because "there isn't an image", but also because there isn't an "instance of" statement (P31). Therefore, you can get results for both instances by wrapping both of these in an OPTIONAL block, with just ?item and ?itemLabel for Q367204, and all variables for Q1:
SELECT ?item ?itemLabel ?class ?classLabel ?projectLabel WHERE {
VALUES ?item { wd:Q1 wd:Q367204 }
OPTIONAL {
?item wdt:P31 ?class;
wdt:P18 ?project.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,en". }
}

Get specific item from Wikidata with Sparql

I want to get the data of a specific wikidata item (e.g. Q1651322) with Sparql and then extract the image URL.
I tried this:
SELECT ?pic
WHERE
{
?item wdt:P18 ?pic .
FILTER(?item = 'Q1651322')
SERVICE wikibase:label { bd:serviceParam wikibase:language "de" }
}
LIMIT 1
and also this:
SELECT DISTINCT ?item WHERE {
?item ps:P1629 wd:Q1651322.
}
But these queries don't find records. How can I filter for a specific item by its ID?
SELECT ?pic
WHERE
{
?item wdt:P18 ?pic .
FILTER(?item = wd:Q1651322)
SERVICE wikibase:label { bd:serviceParam wikibase:language "de" }
}
LIMIT 1
This query finds the record. It is necessary to prefix the item ID with wd:.

Wikidata entries that can't be retrieved by SPARQL queries?

I am trying to run a simple query, looking up an item by its English label. For some reason, some entries are not showing. Most are fine, but some aren't. I can't pinpoint why.
An example: oyster bed. Here is the entry: https://www.wikidata.org/wiki/Q65953972
This is the query:
SELECT distinct ?item ?itemLabel ?itemDescription WHERE {
?item ?label 'oyster bed'#en.
?article schema:about ?item .
?article schema:inLanguage 'en' .
SERVICE wikibase:label { bd:serviceParam wikibase:language 'en'. }
}
and I get nothing.
The same query with oyster yields an expected result.
What am I doing wrong?
Hat tip of #UninformedUser helped.
As a SPARQL newbie, I did not realise the ?article was an implicit JOIN.
SELECT distinct ?item ?itemLabel ?itemDescription WHERE {
?item ?label 'oyster bed'#en.
OPTIONAL { ?article schema:about ?item .
?article schema:inLanguage 'en' . }
SERVICE wikibase:label { bd:serviceParam wikibase:language 'en'. }
}
is to be used if the article is required, but if there is no real need, the query can be simplified to:
SELECT distinct ?item ?itemLabel ?itemDescription WHERE {
?item ?label 'oyster bed'#en.
SERVICE wikibase:label { bd:serviceParam wikibase:language 'en'. }
}

Wikidata SPARQL query cast & crew

My objective is to create a sparql query that retrieves a list of cast and crew and it's wikidata type ("director", "screenwriter", "cast member", ...)
So far I have this:
SELECT ?titleLabel ?castLabel ?property ?propertyLabel
WHERE {
?title wdt:P345 "tt0848228".
?title wdt:P57 ?cast.
?property wikibase:propertyType ?propertyType.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
The output I want to achive is a table with rows like:
"The Avengers", "Joss Whedon", "wd:P57", "director"
...
I came up with this query:
SELECT ?titleLabel ?castLabel ?property ?propLabel
WHERE {
?title wdt:P345 "tt0848228".
# take all claims on this movie
?title ?property ?cast .
# that involve a human
?cast wdt:P31 wd:Q5 .
# get the property label
# see https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries#Adding_labels_for_properties
hint:Query hint:optimizer "None" .
?prop wikibase:directClaim ?property .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}