Wikidata Query Service: how do I search by freebase - sparql

I am using the Wikidata Query Service (https://query.wikidata.org) to get details about freebase ids.
I now have a list of freebase ids
/m/0gqpg /m/0h0vk /m/0h3vhfb /m/0h4xw_ /m/0h52w /m/0hfp /m/0hhbr
How should I modify the following code to achieve a bulk query?
\`SELECT ?s ?sLabel ?p ?o ?oLabel WHERE {
?s wdt:P646 "/m/0181lj"
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
I would like to get the label corresponding to the freebase id

There are multiple options (FILTER with IN; FILTER with ||; UNION), but usually the best is VALUES:
SELECT ?s ?sLabel ?freebaseID
WHERE {
VALUES ?freebaseID {
"/m/0181lj"
"/m/0gqpg"
"/m/0h0vk"
"/m/0h3vhfb"
"/m/0h4xw_"
"/m/0h52w"
"/m/0hfp"
"/m/0hhbr"
}
?s wdt:P646 ?freebaseID .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}

Related

Select all statements using alternate identifier in Wikidata 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.

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

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:.

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!

Query Wikidata by GeoNames ID

I try to query Wikidata for a city by its GeoNames ID.
I can see the id of the property is P1556: https://www.wikidata.org/wiki/Property:P1566
So my query for Berlin (as an example) is:
SELECT * WHERE {
?id wdt:P1566 wd:2950157
}
But I don't get a match.
What am I doing wrong?
A simple trial and error on Wikidata results in the following: No need for wd:2950157, it is a literal, so use only "2950157" instead. Use the following (SERVICE is used to show the name of the city):
Edit: The straightforward method:
SELECT ?id ?idLabel WHERE {
?id wdt:P1566 "2950157".
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
End of EDIT
Using BIND or FILTER
SELECT ?id ?idLabel WHERE {
Bind ("2950157" as ?x)
?id wdt:P1566 ?x.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
Or (a slower option) use FILTER
SELECT ?id ?idLabel WHERE {
?id wdt:P1566 ?x.
filter (?x = "2950157")
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}