Search code examples
sparqlwikidata

Wikidata Query by ItemIdentifies


I want to query translations for a given city by its item identitier. forthis i query for property p31 on the if of karlsruhe

this is my query

 SELECT DISTINCT ?city ?label ?population WHERE {
  ?city (wdt:P31/wdt:P279*) wd:Q515. // is city
  ?city wdt:P31 wd:Q1040. //is karlsruhe
  ?city wdt:P1082 ?population.
  ?city rdfs:label ?label.
  FILTER(((LANG(?label)) = "de") || ((LANG(?label)) = "en") || ((LANG(?label)) = "it") || ((LANG(?label)) = "ru") || ((LANG(?label)) = "ro") || ((LANG(?label)) = "tr") || ((LANG(?label)) = "pl") || ((LANG(?label)) = "fr") || ((LANG(?label)) = "es") || ((LANG(?label)) = "hr"))
}

but i don't get any results.

What am I doing wrong?


Solution

  • In short, the problem is that Karlsruhe is not a Karlsruhe. Please read the definition of wdt:P31 carefully.

    Your query should be:

    SELECT DISTINCT ?city ?label ?population WHERE {
      VALUES (?city) { (wd:Q1040) }      # is Karlsruhe
      ?city (wdt:P31/wdt:P279*) wd:Q515  # is a city
      OPTIONAL { ?city wdt:P1082 ?population } 
      ?city rdfs:label ?label
      FILTER(LANG(?label) IN ("de", "en", "it", "ru", "ro", "tr", "pl", "fr", "es", "hr"))
    }
    

    For better understanding, one could rewrite the query above in this way:

    SELECT DISTINCT ?city ?label ?population WHERE {
      FILTER(?city IN (wd:Q1040))        # is Karlsruhe
      ?city (wdt:P31/wdt:P279*) wd:Q515  # is a city
      OPTIONAL { ?city wdt:P1082 ?population } 
      ?city rdfs:label ?label
      FILTER(LANG(?label) IN ("de", "en", "it", "ru", "ro", "tr", "pl", "fr", "es", "hr"))
    }
    

    Under RDFS++ entailment, one would even write:

    SELECT DISTINCT ?city ?label ?population WHERE {
      ?city owl:sameAs wd:Q1040   # is Karlsruhe
      ?city wdt:P31 wd:Q515       # is a city
      OPTIONAL { ?city wdt:P1082 ?population } 
      ?city rdfs:label ?label
      FILTER(LANG(?label) IN ("de", "en", "it", "ru", "ro", "tr", "pl", "fr", "es", "hr"))
    }