We are going to use some of the Public SPARQL endpoints. Let's look first at the set of available magics
In [1]:
%lsmagics
We set a few default options via those magics.
In [2]:
# Modify output format
# Don't show more than 80 results (event if more are fetched)
%show 80
# Request whatever format is appropriate for the query type
%format default
# Activate table output
%display table
Note that DBPedia has a set of predefined namespace prefixes that we can use without the need to define them in the query.
In [3]:
%endpoint http://dbpedia.org/sparql
%display table withtypes
SELECT DISTINCT ?property
WHERE {
?s ?property ?person .
?person rdf:type foaf:Person .
}
LIMIT 10
Let's use the dbp:successor
predicate to find chains of at least 4 persons that form a sequence. Since it can produce a nice graph, let's output it as a diagram.
(note: formerly using the dbp:before
property, but that one seems not to be available anymore for DBPedia entries)
In [4]:
%endpoint http://dbpedia.org/sparql
%display diagram
CONSTRUCT {
?p1 dbp:successor ?p2 .
?p2 dbp:successor ?p3 .
?p3 dbp:successor ?p4 .
}
WHERE {
?p1 rdf:type foaf:Person .
?p1 dbp:successor ?p2 .
?p2 dbp:successor ?p3 .
?p3 dbp:successor ?p4 .
}
LIMIT 50
A DESCRIBE
query, also represented as a diagram.
In [5]:
%endpoint http://dbpedia.org/sparql
%display diagram
%lang es
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
DESCRIBE <http://dbpedia.org/resource/Asturias>
LIMIT 10
This one is taken from SPARQL By Example:
Modified: former type:LandlockedCountries
property moved to yago:WikicatLandlockedCountries
In [6]:
%endpoint http://dbpedia.org/sparql
%format default
%display table
%lang all
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX yago: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?country_name ?population
WHERE {
?country a yago:WikicatLandlockedCountries ;
rdfs:label ?country_name ;
prop:populationEstimate ?population .
FILTER (?population > 15000000) .
FILTER (langMatches(lang(?country_name), "EN")) .
} ORDER BY DESC(?population)
Where was Henrik Ibsen born? (taken from a StackOverflow answer).
In [7]:
%endpoint http://dbpedia.org/sparql
%display table withtypes
%lang all
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT ?label {
dbr:Henrik_Ibsen
dbpedia-owl:birthPlace
[ a dbpedia-owl:Country ;
rdfs:label ?label ]
FILTER langMatches(lang(?label),"en")
}
Let's use now the wikidata SPARQL endpoint query service. You can read its user manual. There is also a page with many example queries
This was taken from Causes of death for US presidents
In [8]:
%endpoint http://query.wikidata.org/sparql
%display table
%show all
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?president ?cause ?dob ?dod WHERE {
?pid wdt:P39 wd:Q11696 .
?pid wdt:P509 ?cid .
?pid wdt:P569 ?dob .
?pid wdt:P570 ?dod .
OPTIONAL {
?pid rdfs:label ?president filter (lang(?president) = "en") .
}
OPTIONAL {
?cid rdfs:label ?cause filter (lang(?cause) = "en") .
}
}
Birthdays for today. We fetch only the oldest 20 (depending on the date, there could be many). Example from the wikidata example list
In [9]:
%endpoint http://query.wikidata.org/sparql
SELECT ?entityS ?entity (year(?date) as ?year)
WHERE
{
?entityS wdt:P569 ?date .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
?entityS rdfs:label ?entity
}
FILTER (datatype(?date) = xsd:dateTime && month(?date) = month(now()) && day(?date) = day(now()))
}
ORDER BY ASC(?year)
LIMIT 20
Who discovered the most asteroids?. Taken also from the example page
In [10]:
%endpoint http://query.wikidata.org/sparql
%display table
SELECT ?discoverer ?name (COUNT(?asteroid) AS ?count)
WHERE
{
?asteroid wdt:P31 wd:Q3863 .
?asteroid wdt:P61 ?discoverer .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
?discoverer rdfs:label ?name
}
}
GROUP BY ?discoverer ?name
ORDER BY DESC(?count)
LIMIT 10
linkedMDB contains an RDF database for Movies. It also has a web-based query interface
Let's find movie titles with the ring
word in it.
In [11]:
%endpoint http://data.linkedmdb.org/sparql
%format json
%display table
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX movie: <http://data.linkedmdb.org/resource/movie/>
SELECT ?movie ?label
WHERE {
?movie rdf:type movie:film .
?movie rdfs:label ?label .
FILTER regex(?label, "ring", "i")
} LIMIT 10