Query for EU universities from Wikidata times out - sparql

Asking the Wikidata service for all universities works. But when restricted to only EU universities, this query times out.
WHERE {
?item wdt:P31/wdt:P279* wd:Q3918 ;
wdt:P17 ?country.
?country wdt:P463 wd:Q458 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Any idea?
Strangely, using FILTER EXISTS is returns the result (although quite slow).
SELECT ?item ?itemLabel ?countryLabel ?country
WHERE {
?item wdt:P31/wdt:P279* wd:Q3918 ;
wdt:P17 ?country.
FILTER EXISTS { ?country wdt:P463 wd:Q458 }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Here's a link to the second query.

Related

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'. }
}

how to get list of the cities from certain country

I'm trying to get list of the cities of Ukraine for example.
I tried next code:
SELECT ?item ?itemLabel
WHERE {
?item wdt:P17 wd:Q212.
?item wdt:P31 wd:Q515
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
But it only outputs weird 3 results. I think i'm doing something wrong

Retrieving Property of A Class of Entities In SPARQL (wikidata) Not As Expected

Why does the following query:
SELECT ?item ?itemLabel ?o ?oLabel WHERE {
?item wdt:P31 wd:Q3918.
?item wdt:P131 ?o.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
not return https://www.wikidata.org/wiki/Q160302, when clearly this entity has a property P131: https://www.wikidata.org/wiki/Q160302#P131?
Including the OPTIONAL clause does:
SELECT ?item ?itemLabel ?o ?oLabel WHERE {
?item wdt:P31 wd:Q3918.
OPTIONAL {?item wdt:P131 ?o.}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
But why?

Find people whose name start with a particular letter with sparql Wikidata?

This is my query but I don't get any results for records starting with "n"
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P31/wdt:P279* wd:Q5.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
FILTER regex (?itemLabel, "^(n)").
}

SPARQL: Querying Wikidata labels for more than one language

I am trying to get labels in multiple languages from Wikidata's SPARQL endpoint. The following example is given here:
SELECT ?country ?country_EN ?country_DE ?country_FR
WHERE {
?country wdt:P31 wd:Q185441. # member state of the European Union
SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
?country rdfs:label ?country_EN.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "de".
?country rdfs:label ?country_DE.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr".
?country rdfs:label ?country_FR.
}
}
Try it here
However, this returns the following error:
Unknown error: there can be only one "run last" join in any group
Is there a solution to get labels in more than one language?
rdfs:label can be used directly without the wikibase:label service:
SELECT ?country ?country_en ?country_de ?country_fr
WHERE {
?country wdt:P31 wd:Q185441. # member state of the European Union
OPTIONAL {?country rdfs:label ?country_en filter (lang(?country_en) = "en")}.
OPTIONAL {?country rdfs:label ?country_de filter (lang(?country_de) = "de")}.
OPTIONAL {?country rdfs:label ?country_fr filter (lang(?country_fr) = "fr")}.
}
Try it here
The label service optimizer adds a hint:Prior hint:runLast true hint to the label service unless there’s another explicit hint:
LabelServiceUtils.getLabelServiceNodes(op).forEach(service -> {
if (service.getProperty(QueryHints.RUN_LAST) != null ||
service.getProperty(QueryHints.RUN_FIRST) != null) {
return;
}
service.setProperty(QueryHints.RUN_LAST, TRUE);
});
One should just add hint:Prior hint:runLast false to all the label service invocations after the first one.
Your query should be:
SELECT ?country ?country_EN ?country_DE ?country_FR
WHERE {
?country wdt:P463 wd:Q458. # member state of the European Union
SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
?country rdfs:label ?country_EN.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "de".
?country rdfs:label ?country_DE.
} hint:Prior hint:runLast false.
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr".
?country rdfs:label ?country_FR.
} hint:Prior hint:runLast false.
}
Try it!
Obviously, it is possible to fetch labels in multiple languages using regular SPARQL, and this is less verbose. However the label service provides language fallbacks, including the final one to Q-id's.
Source:
https://phabricator.wikimedia.org/T175840