Search code examples
sparqlwikidata

Query Wikidata by GeoNames ID


I try to query Wikidata for a city by its GeoNames ID.

I can see the id of the property is P1556: https://www.wikidata.org/wiki/Property:P1566

So my query for Berlin (as an example) is:

SELECT * WHERE {
   ?id wdt:P1566 wd:2950157
   }

But I don't get a match.

What am I doing wrong?


Solution

  • A simple trial and error on Wikidata results in the following: No need for wd:2950157, it is a literal, so use only "2950157" instead. Use the following (SERVICE is used to show the name of the city):

    Edit: The straightforward method:

    SELECT ?id ?idLabel WHERE {      
      ?id wdt:P1566 "2950157".      
      SERVICE wikibase:label {
          bd:serviceParam wikibase:language "en" .
      }
    }
    

    End of EDIT

    Using BIND or FILTER

    SELECT ?id ?idLabel WHERE {
      Bind ("2950157" as ?x)
      ?id wdt:P1566 ?x.      
      SERVICE wikibase:label {
          bd:serviceParam wikibase:language "en" .
      }
    }
    

    Or (a slower option) use FILTER

     SELECT ?id ?idLabel WHERE {       
        ?id wdt:P1566 ?x.
        filter (?x = "2950157")
        SERVICE wikibase:label {
            bd:serviceParam wikibase:language "en" .
        }
     }