Skip to content

Instantly share code, notes, and snippets.

@jrc
Last active November 14, 2019 12:54
Show Gist options
  • Save jrc/458d03ea503bded5c4a1781a9426c1a6 to your computer and use it in GitHub Desktop.
Save jrc/458d03ea503bded5c4a1781a9426c1a6 to your computer and use it in GitHub Desktop.
# Get ISO country codes and demonyms for all countries and geo regions
SELECT ?item ?itemLabel ?countryCode ?demonym
WHERE {
{
# see "Query not returning all expected results"
# https://www.wikidata.org/wiki/Wikidata_talk:SPARQL_query_service/queries
?item p:P31 [ps:P31 wd:Q6256]. # categoryof (P31) country (Q6256)
?item wdt:P297 ?countryCode .
?item wdt:P1549 ?demonym .
FILTER (LANG(?demonym) = "en") .
}
UNION {
?item p:P31 [ps:P31 wd:Q82794]. # categoryof (P31) geo region (Q82794)
?item wdt:P1549 ?demonym .
FILTER (LANG(?demonym) = "en") .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
# Get all U.S. state codes
SELECT ?item ?itemLabel ?stateCode
WHERE {
{
# see "Query not returning all expected results"
# https://www.wikidata.org/wiki/Wikidata_talk:SPARQL_query_service/queries
?item p:P31 [ps:P31 wd:Q35657]. # categoryof (P31) state of the United States (Q35657)
?item wdt:P1813 ?stateCode .
FILTER (strlen(?stateCode) = 2) .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment