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" .
}
}
Related
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" . }
}
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]". }
}
I'm trying to perform path matching inside a VALUES clause in sparql in order to match all instances and subclasses of both battles and sieges in wikidata. The following request repeatedly times out.
SELECT DISTINCT ?battle ?battleLabel WHERE {
{
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
VALUES ?type {wd:Q178561 wd:Q188055} ?battle (wdt:P31/wdt:P279*) ?type .
?battle rdfs:label ?queryByTitle.
FILTER(REGEX(?queryByTitle, "saratoga", "i"))
}
}
It seems that VALUES, esp. in conjunction with /, confuses the Blazegraph's query optimizer in that case.
Use UNION instead of VALUES:
SELECT DISTINCT ?battle ?battleLabel WHERE {
{ ?battle wdt:P31/wdt:P279* wd:Q178561 }
UNION
{ ?battle wdt:P31/wdt:P279* wd:Q188055 }
?battle rdfs:label ?queryByTitle.
FILTER(REGEX(?queryByTitle, "saratoga", "i"))
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
Alternatively, disable the optimizer and specify explicit order:
SELECT DISTINCT ?battle ?battleLabel WHERE {
hint:Query hint:optimizer "None" .
VALUES ?type {wd:Q178561 wd:Q188055}
?subtype wdt:P279* ?type .
?battle wdt:P31 ?subtype .
?battle rdfs:label ?queryByTitle.
FILTER(REGEX(?queryByTitle, "saratoga", "i"))
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
For a given property like 'occupation (P106)', I want to retrieve all its aliases like: profession, job, work, career, employment, craft. All of this is present on the properties wikidata page, under 'Also known as'. How can I go about retrieving this using SPARQL?
I tried using the following query.
SELECT ?predicate ?object WHERE {
wdt:P106 wdt:P1449 ?predicate . //Nickname
wdt:P106 wdt:P734 ?predicate . //Family Name
wdt:P106 wdt:P735 ?predicate . //Given Name
wdt:P106 skos:altLabel ?predicate .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
SELECT ?altLabel
{
VALUES (?wd) {(wd:P106)}
?wd skos:altLabel ?altLabel .
FILTER (lang(?altLabel) = "en")
}
or
SELECT ?altLabel
{
VALUES (?wdt) {(wdt:P106)}
?wd wikibase:directClaim ?wdt .
?wd skos:altLabel ?altLabel .
FILTER (lang(?altLabel) = "en")
}
These paragraphs provide some explanation:
Truthy statements
Properties
Predicates
Update
You still could use the label service:
SELECT ?wdAltLabel
{
VALUES (?wdt) {(wdt:P106)}
?wd wikibase:directClaim ?wdt .
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