With the following SPARQL query I’m trying to list countries with their national flags in descending order of population. I cannot run it without it reaching a timeout limit. It runs in ~2s when the indicated line is commented out (but this returns the cartesian product of all countries and all national flags, not just the associated pairs).
SELECT ?country ?countryLabel ?flag ?population
WHERE {
?country wdt:P31 wd:Q6256;
wdt:P1082 ?population.
?flag wdt:P31 wd:Q186516.
?flag wdt:P1001 ?country. # runs without this line
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY DESC(?population)
LIMIT 100
Does anyone know why this query is to complex to compute, and how to get it to run? Thanks!