I would like to ask wikidata the following question: "Which states of the USA share borders with the max number of states?"
I tried the query below. With the subquery (lines 8-19) I get the maximum number of borders of any US state (?max_bs). And it works, it results to be 8. Then with the outer SELECT I would like to get only those US states with number of borders equal to ?max_bs. But is does not work as I expected.
SELECT ?state WHERE {
?state wdt:P31 ?z1 .
?z1 wdt:P279* wd:Q35657 .
?state wdt:P47 ?borders1 .
?borders1 wdt:P31 ?w1 .
?w1 wdt:P279* wd:Q106458883 .
{
SELECT (MAX(?bs) aS ?max_bs) WHERE {
{
SELECT ?state (COUNT(?borders) AS ?bs) WHERE {
?state wdt:P31 ?z .
?z wdt:P279* wd:Q35657 .
?state wdt:P47 ?borders .
?borders wdt:P31 ?w .
?w wdt:P279* wd:Q106458883
}
GROUP BY ?state
}
}
}
}
GROUP BY ?state
HAVING(COUNT(?borders1) = (?max_bs))
