User:Smalyshev (WMF)/Countries and States

From mediawiki.org

Regions[edit]

Get administrative subdivisions of certain country with their: label, flag and wikipedia page.

SELECT REDUCED ?id ?idLabel 
	   (SAMPLE(?flagImg) as ?flagImg)
	   (SAMPLE(?page) as ?page)
WHERE {
  # Configurable parameters: 
  # ?country is a country
  BIND(wd:Q30 as ?country)
  # ?types is administrative subdivisions we seek for this country
  VALUES ?types { wd:Q34876 wd:Q7275 }
  # No serviceable parts beyond this point
  # Located in the administrative territorial entity of the whole country, but not sub-entity
  ?id wdt:P131 ?country .
  # We want it to be of this type in a country, and have a capital
  FILTER EXISTS { ?id (wdt:P31/wdt:P279*) ?types ;
                          wdt:P17 ?country . }
  # Excluding the country itself
  FILTER(?id != ?country)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  OPTIONAL { ?id wdt:P41 ?flagImg }
  OPTIONAL {
      ?page schema:about ?id .
      ?page schema:isPartOf <https://en.wikipedia.org/> .
  }
} GROUP BY ?id ?idLabel

Try it!

Regional Capitals[edit]

Get capitals of the administrative subdivisions of given country, with: label, population, location, image and wikipedia page. Note that one administrative entity can have more than one capital (rare, but happens)

SELECT REDUCED ?id ?idLabel
       (SAMPLE(?location) as ?location)
       (SAMPLE(?image) as ?image)
       (SAMPLE(?population) as ?population)
       (SAMPLE(?page) as ?page)
WHERE {
  # Configurable parameters: 
  # ?country is a country
  BIND(wd:Q258 as ?country)
  # ?types is administrative subdivisions we seek for this country
  VALUES ?types { wd:Q34876 wd:Q7275 }
  # No serviceable parts beyond this point
  # Located in the administrative territorial entity of the whole country, but not sub-entity
  ?region wdt:P131 ?country .
  # We want it to be of this type in a country, and have a capital
  FILTER EXISTS { ?region (wdt:P31/wdt:P279*) ?types ;
                          wdt:P17 ?country . }
  # Excluding the country itself
  FILTER(?region != ?country)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  ?region wdt:P36 ?id .
  ?id wdt:P625 ?location .
  OPTIONAL { ?id wdt:P18 ?image }
  OPTIONAL { ?id wdt:P1082 ?population }
  OPTIONAL {
      ?page schema:about ?id .
      ?page schema:isPartOf <https://en.wikipedia.org/> .
  }
} GROUP BY ?id ?idLabel

Try it!

Neighbors[edit]

Get countries that given country borders, with their: label, flag image, and Wikipedia page. The result includes the country itself.

SELECT REDUCED ?id ?idLabel 
	   (SAMPLE(?flagImg) as ?flagImg)
	   (SAMPLE(?page) as ?page)
WHERE {
  # Configurable parameters: 
  # ?country is a country
  BIND(wd:Q258 as ?country)
  # No serviceable parts beyond this point
  ?country wdt:P47? ?id .
  # exclude former countries
  FILTER NOT EXISTS { ?id wdt:P31 wd:Q3024240 }
  FILTER NOT EXISTS { ?id wdt:P576 ?date }
  FILTER EXISTS { ?id wdt:P31 wd:Q6256 }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  OPTIONAL { ?id wdt:P41 ?flagImg }
  OPTIONAL {
      ?page schema:about ?id .
      ?page schema:isPartOf <https://en.wikipedia.org/> .
  }
} GROUP BY ?id ?idLabel

Try it!

Neighboring capitals[edit]

Capitals of this and neighboring countries. Note that country can have more than one capital (rare but happens).

SELECT REDUCED ?id ?idLabel
       (SAMPLE(?location) as ?location)
       (SAMPLE(?image) as ?image)
       (SAMPLE(?population) as ?population)
       (SAMPLE(?page) as ?page)
WHERE {
  # Configurable parameters: 
  # ?country is a country
  BIND(wd:Q183 as ?country)
  # No serviceable parts beyond this point
  ?country wdt:P47? ?idObj .
  # exclude former countries
  FILTER NOT EXISTS { ?idObj wdt:P31 wd:Q3024240 }
  FILTER NOT EXISTS { ?idObj wdt:P576 ?date }
  FILTER EXISTS { ?idObj wdt:P31 wd:Q6256 }
  ?idObj wdt:P36 ?id .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  ?id wdt:P625 ?location .
  OPTIONAL { ?id wdt:P18 ?image }
  OPTIONAL { ?id wdt:P1082 ?population }
  OPTIONAL {
      ?page schema:about ?id .
      ?page schema:isPartOf <https://en.wikipedia.org/> .
  }
} GROUP BY ?id ?idLabel

Try it!

Population by year[edit]

Population of a given place by year. Each year will have only one population figure.

SELECT (MAX(?population) as ?population) ?year WHERE {
    # set the item here - e.g. city, country
	BIND(wd:Q84 as ?item)
    ?item p:P1082 ?popstatement .
    ?popstatement ps:P1082 ?population .
    OPTIONAL {
      ?popstatement pq:P585 ?date .
      BIND(year(?date) as ?year)
    }
} GROUP BY ?year ORDER BY DESC(?year)

Try it!