user3403572 user3403572 -4 years ago 84
SQL Question

How to covert some queries from sql to sparql?

I am just learning Sparql and I have the following tables:

Countries, European Country, City and Capital

I would like to know how to make the following queries, because I didnt understand them...

1) Which country has the city "Paris" as capital.
2) Print all the European Countries
3) Print all the countries and theis capitals.

Thank you very much in advance.

Answer Source

This said, there is DBpedia that offers info similar to what is described, and is a good playground. To query it, (which is a better sparql editor) or (which is our own integration, a few months old, but has some extra goodies).

Which country has the city "Paris" as capital

select * {
  ?x a dbo:Country; dbo:capital dbr:Paris

The yasgui results will surprise you:


I understand all the historic kingdoms, but dbr:Office_International_d'Hygiène_Publique is a bit of a shock. The reason is that it's an International Organization (uses Infobox Former International Organization, and it redirects to, which "is currently being merged with Template:Infobox country". See

factforge returns even more results from linked datasets (all these mean just France):


All the European Countries

That's better because there happens to be a Wikipedia category:

select * {
  ?x a dbo:Country; dct:subject dbc:Countries_in_Europe


all countries and their capitals

select * {
  ?x a dbo:Country; dbo:capital ?capital

This returns a bunch of historic countries and capitals, and again some international organizations, eg


SO THEN, you may have better luck with Wikidata ( At you can find a bunch of interesting queries related to countries.

countries and capitals on Wikidata

select ?country ?countryLabel ?city ?cityLabel {
  ?country wdt:P36 ?city
  filter exists {?country wdt:P31/wdt:P279* wd:Q6256}
    SERVICE wikibase:label {
        bd:serviceParam wikibase:language "en,pl,ru,es" .
} order by ?countryLabel

It uses a bunch of Qnn and Pnn but you can decode them when you mouse-over.

  • How did I find that wdt:P36 means "capital"? Search for property:capital
  • why filter exists? Because a country may have several subtypes of wd:Q6256 "country", and if I use that in the main query, it returns several results per country. This way it returns only one.


You can also easily display it on a map:

select ?country ?countryLabel ?city ?cityLabel ?coords {
  ?country wdt:P36 ?city
  filter exists {?country wdt:P31/wdt:P279* wd:Q6256}
  ?city wdt:P625 ?coords
  SERVICE wikibase:label {bd:serviceParam wikibase:language "en,pl,ru,es"}

See a couple of shots

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download