query-49518b72442cf53ae2d3582b5654e93c

rq turtle/ttl

Largest cities with female mayor SELECT DISTINCT ?city ?cityLabel ?mayor ?mayorLabel WHERE { BIND(wd:Q6581072 AS ?sex) BIND(wd:Q515 AS ?c)

?city wdt:P31/wdt:P279* ?c .  # find instances of subclasses of city
?city p:P6 ?statement .            # with a P6 (head of goverment) statement
?statement ps:P6 ?mayor .          # ... that has the value ?mayor
?mayor wdt:P21 ?sex .       # ... where the ?mayor has P21 (sex or gender) female
FILTER NOT EXISTS { ?statement pq:P582 ?x }  # ... but the statement has no P582 (end date) qualifier

# Now select the population value of the ?city
# (wdt: properties use only statements of "preferred" rank if any, usually meaning "current population")
?city wdt:P1082 ?population .
# Optionally, find English labels for city and mayor:
SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
}

} ORDER BY DESC(?population) LIMIT 10

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
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#>
#Largest cities with female mayor
SELECT DISTINCT ?city ?cityLabel ?mayor ?mayorLabel
WHERE
{
  BIND(wd:Q6581072 AS ?sex)
  BIND(wd:Q515 AS ?c)

    ?city wdt:P31/wdt:P279* ?c .  # find instances of subclasses of city
    ?city p:P6 ?statement .            # with a P6 (head of goverment) statement
    ?statement ps:P6 ?mayor .          # ... that has the value ?mayor
    ?mayor wdt:P21 ?sex .       # ... where the ?mayor has P21 (sex or gender) female
    FILTER NOT EXISTS { ?statement pq:P582 ?x }  # ... but the statement has no P582 (end date) qualifier

    # Now select the population value of the ?city
    # (wdt: properties use only statements of "preferred" rank if any, usually meaning "current population")
    ?city wdt:P1082 ?population .
    # Optionally, find English labels for city and mayor:
    SERVICE wikibase:label {
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
    }
}
ORDER BY DESC(?population)
LIMIT 10

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v5("?c") v6("?city"):::projected v7("?mayor"):::projected v1("?population") v4("?sex") v2("?statement") v3("?x") a1((" ")) c9(["bd:serviceParam"]):::iri c11(["#91;AUTO_LANGUAGE#93;,en"]):::literal f0[["not "]] subgraph f0e0["Exists Clause"] e0v1 --"p:qualifier/P582"--> e0v2 e0v1("?statement"):::projected e0v2("?x"):::projected end f0--EXISTS--> f0e0 f0 --> v2 f0 --> c1 f0 --> v3 v2 --"p:qualifier/P582"--> v3 bind1[/"'wd:Q6581072'"/] bind1 --as--o v4 bind2[/"'wd:Q515'"/] bind2 --as--o v5 v6 --"p:direct/P31"--> a1 a1 --"p:direct/P279"--> v5 v6 --"p:P6"--> v2 v2 --"p:statement/P6"--> v7 v7 --"p:direct/P21"--> v4 v6 --"p:direct/P1082"--> v1 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c9 --"wikibase:language"--> c11 end