How to use SPARQL to query DBpedia?

As nowadays data is very often being stored in RDF formats such as RSS, a need has arisen for a simple way to locate specific information. In this article, I’ll try to demonstrate how easy it’s to build a simple query using SPARQL and use it to query DBpedia for a specific information!

When was John Lennon born?

SELECT DISTINCT ?x1 WHERE {
?x0 rdf:type foaf:Person.
?x0 rdfs:label "John Lennon"@en.
?x0 dbpedia-owl:birthDate ?x1.
}

Result:

x1
1940-10-09

Click here to see the Live Result

You can look at this page, for the list of available information about (in this case) John Lennon: http://dbpedia.org/page/John_Lennon

 

What is the population of Slovakia?

SELECT DISTINCT ?x1 WHERE {
?x0 rdf:type dbpedia-owl:Country.
?x0 rdfs:label "Slovakia"@en.
?x0 dbpprop:populationCensus ?x1.
}

Result:

x1
5397036

Click here to see the Live Result

 

Who wrote Anna Karenina?

SELECT DISTINCT ?x2 WHERE {
?x0 rdf:type foaf:Person.
?x0 foaf:name ?x2.
?x1 rdf:type dbpedia-owl:Book.
?x1 rdfs:label "Anna Karenina"@en.
?x1 dbpedia-owl:author ?x0.
} LIMIT 1

Result:

x1
“Leo Tolstoy”@en

Click here to see the Live Result

 

Who was Freddie Mercury?

SELECT DISTINCT ?x1 WHERE {
?x0 rdf:type foaf:Person.
?x0 rdfs:label "Freddie Mercury"@en.
?x0 rdfs:comment ?x1.
} LIMIT 1

Result:

x1
“Freddie Mercury (born Farrokh Bulsara; Gujarati: Pharōkh Balsārā‌; 5 September 1946 – 24 November 1991) was a British musician, record producer, and singer-songwriter, best known as the lead vocalist and lyricist of the rock band Queen. As a performer, he was known for his flamboyant stage persona and powerful vocals over a four-octave range.”@eň

Click here to see the Live Result

 

What albums did Pink Floyd record?

SELECT DISTINCT ?x2 WHERE {
?x0 rdf:type dbpedia-owl:Album.
?x0 dbpedia-owl:producer ?x1.
?x0 foaf:name ?x2.
?x1 rdf:type dbpedia-owl:Band.
?x1 rdfs:label "Pink Floyd"@en.
}

Result:

x2
“Echoes: The Best of Pink Floyd”@en
“Echoes: The Best of Pink Floyd”^^<http://www.w3.org/2001/XMLSchema#string>
“Oh, by the Way”@en
“Animals”@en
“Animals”^^<http://www.w3.org/2001/XMLSchema#string>
“More”@en
“More”^^<http://www.w3.org/2001/XMLSchema#string>
“The Dark Side of the Moon”@en
“Wish You Were Here”@en
“Obscured by Clouds”@en
“Relics”@en
“Works”@en
“The Best of Pink Floyd: A Foot in the Door”@en
“The Best of Pink Floyd: A Foot in the Door”^^<http://www.w3.org/2001/XMLSchema#string>
“Atom Heart Mother”@en
“Ummagumma”@en
“Meddle”@en
“A Collection of Great Dance Songs”@en
“Pink Floyd: Discovery”@en
“The Dark Side of the Moo”@en
“Shine On”@en

Click here to see the Live Result

 

Find all landlocked countries with a population smaller than 100 thousand, with the highest population country first.

PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?country_name ?population
WHERE {
?country a type:LandlockedCountries ;
rdfs:label ?country_name ;
prop:populationEstimate ?population .
FILTER (?population < 100000 &&
langMatches(lang(?country_name), "en")) .
} ORDER BY DESC(?population)

Result:

country_name population
“Andorra”@en 85082
“Liechtenstein”@en 36281
“San Marino”@en 32576
“Vatican City”@en 839
“Swaziland”@en 1.106

Click here to see the Live Result

 

Is the Amazon river longer than the Nile River?

PREFIX prop: <http://dbpedia.org/property/>
ASK
{
<http://dbpedia.org/resource/Amazon_River> prop:length ?amazon .
<http://dbpedia.org/resource/Nile> prop:length ?nile .
FILTER(?amazon > ?nile) .
}       

Result: FALSE

Click here to see the Live Result

 

Note: You can use Virtuoso SPARQL Query Editor against the live DBpedia database at following url: http://live.dbpedia.org/sparql

Facebook Comments