query-1a82a662d135cd851b2fd2f5aba51d4b
Query returning countries where the largest city is not the capitalHello, . https://en.wikipedia.org/wiki/List_of_countries_whose_capital_is_not_their_largest_cityI am looking to write a query that basically builds this list I have tried multiple versions but I always get a timeout. What I have managed is to output the capital for each country with the capital's (most recent) population count. I think, I would need a subquery to get the most recent population count for each city. Then, for each country, I would have to determine the city with the highest such count and then check if it is not the capital. For the capital I would need another subquery to make sure it is the current capital (with the maximum start time). I would post my attempts here, but they are incredibly messy. Therefore, I just post my "starting point":
Use at
- https://query.wikidata.org/sparql
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT DISTINCT ?country ?countryLabel ?capital ?capitalLabel ?population1 WHERE {
{ SELECT ?capital (MAX(?date1) AS ?max_date1) WHERE {
?country p:P31/ps:P31 wd:Q6256 .
?country p:P36/ps:P36 ?capital .
?capital p:P1082/pq:P585 ?date1 .
} GROUP BY ?capital }
?country p:P31/ps:P31 wd:Q6256 .
?country p:P36/ps:P36 ?capital .
?capital p:P1082 ?p1 .
?p1 ps:P1082 ?population1 .
?p1 pq:P585 ?max_date1 .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
ORDER BY ASC(?countryLabel)