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