Skip to content
Advertisement

How to convert 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.

Advertisement

Answer

This said, there is DBpedia that offers info similar to what is described, and is a good playground. To query it, http://yasgui.org (which is a better sparql editor) or http://factforge.net/sparql (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:

dbr:Bourbon_Restoration
dbr:France
dbr:Francia
dbr:French_Fifth_Republic
dbr:Kingdom_of_France
dbr:Office_International_d'Hygiène_Publique
dbr:Second_French_Empire
dbr:West_Francia

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 https://en.wikipedia.org/wiki/Template:Infobox_former_country, which “is currently being merged with Template:Infobox country”. See http://mappings.dbpedia.org/index.php/Cleaning_up_Countries

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

geodata:3017382/
http://ontologi.es/place/FR
http://psi.oasis-open.org/iso/3166/#250
leaks:country-FRA
wfr:fr

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
}

yasgui

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

League_of_Nations
International_Authority_for_the_Ruhr
Japanese_occupation_of_British_Borneo

SO THEN, you may have better luck with Wikidata (https://query.wikidata.org/). At https://twitter.com/search?q=wikidatafacts%20country 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.

Map

You can also easily display it on a map:

#defaultView: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 https://twitter.com/valexiev1/status/844870994942603264

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement