4

I'm trying to find cities in Denmark with more than 100 000 in population.

I can find all the cities in Denmark with this code:

SELECT ?s ?o 
WHERE { 
   ?s a <http://dbpedia.org/class/yago/CitiesAndTownsInDenmark>
}

And with this code I can find cities with more than 100 000 in population:

SELECT ?resource ?value 
WHERE { 
   ?resource <http://dbpedia.org/property/populationTotal> ?value 
   FILTER (?value > 100000)
}
ORDER BY ?resource ?value

I would appreciate help about how to combine these queries.

1 Answers1

5

Simple:

SELECT ?resource ?value
WHERE { 
   ?resource a <http://dbpedia.org/class/yago/CitiesAndTownsInDenmark> .
   ?resource <http://dbpedia.org/property/populationTotal> ?value .
   FILTER (?value > 100000)
}
ORDER BY ?resource ?value

In other words: find all the things with type "City or Town in Denmark", and find their populations. You can abbreviate the query, avoiding repetition of 'resource', using ';' rather than '.':

?resource a <http://dbpedia.org/class/yago/CitiesAndTownsInDenmark> ;
          <http://dbpedia.org/property/populationTotal> ?value .

(If you're used to SQL '.' is essentially a natural join: you have ?resource on each side, so join on that value)

user205512
  • 8,798
  • 29
  • 28
  • Any idea where can I find a list of the countries covered ? "China" doesn't works. – Hugolpz Mar 25 '13 at 22:06
  • That useful type exists because people use the category http://en.wikipedia.org/wiki/Category:Cities_and_towns_in_Denmark in wikipedia. Looking at http://dbpedia.org/page/Beijing I see yago:MetropolitanAreasOfChina, but that's not well populated. You might be better off not relying on the existence of such categories and directly querying for populated places in a country. For example: `select ?s { ?s a ; . }` – user205512 Mar 26 '13 at 10:33