We are going to use some of the Public SPARQL endpoints. Let's look first at the set of available magics
%lsmagics
We set a few default options via those magics.
# 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.
Find a few predicates that can point to people
%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)
%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.
%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
%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)
country_name | population |
---|---|
Ethiopia | 99465819 |
Afghanistan | 32564342 |
Uzbekistan | 31025500 |
Kazakhstan | 17563300 |
Burkina Faso | 17322796 |
Malawi | 16407000 |
Zambia | 16212000 |
Where was Henrik Ibsen born? (taken from a StackOverflow answer).
%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")
}
label | type |
---|---|
Norway | literal, 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
%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") .
}
}
president | cause | dob | dod |
---|---|---|---|
Ronald Reagan | Alzheimer's disease | 1911-02-06T00:00:00Z | 2004-06-05T00:00:00Z |
Zachary Taylor | cholera | 1784-11-24T00:00:00Z | 1850-07-09T00:00:00Z |
James K. Polk | cholera | 1795-11-02T00:00:00Z | 1849-06-15T00:00:00Z |
Calvin Coolidge | myocardial infarction | 1872-07-04T00:00:00Z | 1933-01-05T00:00:00Z |
Rutherford B. Hayes | myocardial infarction | 1822-10-04T00:00:00Z | 1893-01-17T00:00:00Z |
Warren G. Harding | myocardial infarction | 1865-11-02T00:00:00Z | 1923-08-02T00:00:00Z |
Grover Cleveland | myocardial infarction | 1837-03-18T00:00:00Z | 1908-06-24T00:00:00Z |
Lyndon B. Johnson | myocardial infarction | 1908-08-27T00:00:00Z | 1973-01-22T00:00:00Z |
Benjamin Harrison | pneumonia | 1833-08-20T00:00:00Z | 1901-03-13T00:00:00Z |
Martin Van Buren | pneumonia | 1782-12-05T00:00:00Z | 1862-07-24T00:00:00Z |
Harry S. Truman | pneumonia | 1884-05-08T00:00:00Z | 1972-12-26T00:00:00Z |
Ronald Reagan | pneumonia | 1911-02-06T00:00:00Z | 2004-06-05T00:00:00Z |
Chester A. Arthur | cerebrovascular disease | 1829-10-05T00:00:00Z | 1886-11-18T00:00:00Z |
Woodrow Wilson | cerebrovascular disease | 1856-12-28T00:00:00Z | 1924-02-03T00:00:00Z |
Millard Fillmore | cerebrovascular disease | 1800-01-07T00:00:00Z | 1874-03-08T00:00:00Z |
John Tyler | cerebrovascular disease | 1790-03-29T00:00:00Z | 1862-01-18T00:00:00Z |
Richard Nixon | cerebrovascular disease | 1913-01-09T00:00:00Z | 1994-04-22T00:00:00Z |
Andrew Johnson | cerebrovascular disease | 1808-12-29T00:00:00Z | 1875-07-31T00:00:00Z |
Franklin Delano Roosevelt | cerebrovascular disease | 1882-01-30T00:00:00Z | 1945-04-12T00:00:00Z |
Andrew Jackson | tuberculosis | 1767-03-15T00:00:00Z | 1845-06-08T00:00:00Z |
Franklin Pierce | liver cirrhosis | 1804-11-23T00:00:00Z | 1869-10-08T00:00:00Z |
Zachary Taylor | gastroenteritis | 1784-11-24T00:00:00Z | 1850-07-09T00:00:00Z |
Warren G. Harding | heart failure | 1865-11-02T00:00:00Z | 1923-08-02T00:00:00Z |
Martin Van Buren | heart failure | 1782-12-05T00:00:00Z | 1862-07-24T00:00:00Z |
Dwight D. Eisenhower | heart failure | 1890-10-14T00:00:00Z | 1969-03-28T00:00:00Z |
William Henry Harrison | sepsis | 1773-02-09T00:00:00Z | 1841-04-04T00:00:00Z |
Gerald Ford | arteriosclerosis | 1913-07-14T00:00:00Z | 2006-12-26T00:00:00Z |
Herbert Hoover | colorectal cancer | 1874-08-10T00:00:00Z | 1964-10-20T00:00:00Z |
Ulysses S. Grant | throat cancer | 1822-04-27T00:00:00Z | 1885-07-23T00:00:00Z |
James A. Garfield | aneurysm | 1831-11-19T00:00:00Z | 1881-09-19T00:00:00Z |
Thomas Jefferson | bacteremia | 1743-04-13T00:00:00Z | 1826-07-04T00:00:00Z |
Calvin Coolidge | coronary thrombosis | 1872-07-04T00:00:00Z | 1933-01-05T00:00:00Z |
Theodore Roosevelt | coronary thrombosis | 1858-10-27T00:00:00Z | 1919-01-06T00:00:00Z |
James Buchanan | respiratory failure | 1791-04-23T00:00:00Z | 1868-06-01T00:00:00Z |
Harry S. Truman | multiple organ dysfunction syndrome | 1884-05-08T00:00:00Z | 1972-12-26T00:00:00Z |
William McKinley | ballistic trauma | 1843-01-29T00:00:00Z | 1901-09-14T00:00:00Z |
James A. Garfield | ballistic trauma | 1831-11-19T00:00:00Z | 1881-09-19T00:00:00Z |
John F. Kennedy | ballistic trauma | 1917-05-29T00:00:00Z | 1963-11-22T00:00:00Z |
Abraham Lincoln | ballistic trauma | 1809-02-12T00:00:00Z | 1865-04-15T00:00:00Z |
Herbert Hoover | internal bleeding | 1874-08-10T00:00:00Z | 1964-10-20T00:00:00Z |
George Washington | acute laryngitis | 1732-02-22T00:00:00Z | 1799-12-14T00:00:00Z |
Thomas Jefferson | uremia | 1743-04-13T00:00:00Z | 1826-07-04T00:00:00Z |
Birthdays for today. We fetch only the oldest 20 (depending on the date, there could be many). Example from the wikidata example list
%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
%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
discoverer | name | count |
---|---|---|
http://www.wikidata.org/entity/Q735603 | Lincoln Near-Earth Asteroid Research | 16477 |
http://www.wikidata.org/entity/Q213563 | Spacewatch | 3222 |
http://www.wikidata.org/entity/Q312255 | Tom Gehrels | 2287 |
http://www.wikidata.org/entity/Q231642 | Ingrid van Houten-Groeneveld | 2272 |
http://www.wikidata.org/entity/Q336095 | Cornelis Johannes van Houten | 2271 |
http://www.wikidata.org/entity/Q312755 | Eric Walter Elst | 2148 |
http://www.wikidata.org/entity/Q1165635 | Lowell Observatory Near-Earth-Object Search | 1758 |
http://www.wikidata.org/entity/Q1140499 | Near-Earth Asteroid Tracking | 1562 |
http://www.wikidata.org/entity/Q814731 | Beijing Schmidt CCD Asteroid Program | 1237 |
http://www.wikidata.org/entity/Q446449 | Schelte J. Bus | 1102 |
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.
%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