I'm interested in cities, towns, villages with their postal codes, states/provinces and official homepages. The data exists mostly in Wikidata - I just seem to be unable to use SPARQL to extract it.
SELECT ?city ?cityLabel WHERE {
?city wdt:P17 wd:Q183;
(wdt:P31/(wdt:P279*)) wd:Q515;
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?cityLabel
This basic approach returns plenty of records without a label. I assume, all cities, towns, villages have labels. Additionally, it returns things like https://www.wikidata.org/wiki/Q436183 as well, which is not a city, town, village but rather a place of sorts. Then I played around some more and came up with this:
SELECT DISTINCT ?cityLabel ?countyLabel ?officialHomepage
WHERE {
?city wdt:P31 wd:Q515.
?city wdt:P17 wd:Q183.
?city wdt:P131 ?county.
OPTIONAL { ?city wdt:P856 ?officialHomepage. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "de". }
}
There are only a few cities. Does anybody have a good approach, on how to build the queries? I understand the SQL somewhat, but putting wdt / wd etc. together is extremely hard for me.