how to get list of the cities from certain country - sparql

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

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?

SPARQL: enumerate result list

For simplicity I use wikidata as example: I have a query:
SELECT ?item ?itemLabel ?mastoadress
WHERE
{
?item wdt:P4033 ?mastoadress.
?item wdt:P106 wd:Q1930187. # occupation journalist
?item wdt:P27 wd:Q183. # country of citizenship germany
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?itemLabel
How can I achieve the result list to include a column where each entry gets its own unique integer index.

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

Query for EU universities from Wikidata times out

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.

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