How to find cities with more than X population in

2019-04-09 10:43发布

问题:

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:

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)