You are on page 1of 25

43

USING SPARQL SELECT

44

SELECT Example (1)


RDF Data:
<http://example.org/book/book1>
<http://purl.org/dc/elements/1.1/title>
"SPARQL handbook" .
Query:
SELECT ?title
WHERE
{
<http://example.org/book/book1>
<http://purl.org/dc/elements/1.1/title>
?title .
}

Result :
?title
SPARQL handbook

45

SELECT Example (2)


RDF Data:
@prefix ab: <http://ld4pe.org/ns/addressbook#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
ab:a01
ab:b01
ab:c01

foaf:name
foaf:mbox
foaf:name
foaf:mbox
foaf:mbox

"Johnny Lee Outlaw" ;


<mailto:jlow@example.com> .
"Peter Goodguy" ;
<mailto:peter@example.org> .
<mailto:carol@example.org> .

46

SELECT Example (2)


Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE
{
?x foaf:name ?name .
?x foaf:mbox ?mbox
}

47

SELECT Example (2)


Result:
name

"Johnny Lee Outlaw


"Peter Goodguy"

mbox

<mailto:jlow@example.com>
<mailto:peter@example.org>

48

SELECT Example (3)


SELECT ?name ?email
WHERE {
?person rdf:type foaf:Person.
?person foaf:name ?name.
?person foaf:mbox ?email.
}

49

SELECT QUIZZES
DBpedia SPARQL Endpoint

50

Quiz (1)
1.1 Find all available information on J. K. Rowling from DBpedia
1.2 Find only J. K. Rowlings birthdate from DBpedia

51

Answer (1)
SELECT ?p ?o
WHERE {
<http://DBpedia.org/resource/J._K._Rowling> ?p ?o;
}
SELECT distinct ?o
WHERE {
<http://DBpedia.org/resource/J._K._Rowling> ?p ?o;
dbo:birthdate
?o
}

52

Quiz (2)
On DBpedia, find people who were lead actors in the movie

Jurassic Park.

53

Answer (2)
select ?person
WHERE {
<http://DBpedia.org/resource/Jurassic_Park> dbo:starring ?
person.
}

54

Quiz (3)
On DBpedia, find people who were born in South Korea.

55

Answer (3)
SELECT

?s

WHERE
{
?s dbo:birthPlace
dbr:South_Korea.
}
ORDER BY ASC(?s)

56

Quiz (4)
4.1 Find East Asian islands on DBpedia.
4.2 Organize the results in ascending order.

57

Answer (4)
SELECT

?s

WHERE
{
?s rdf:type dbo:Island.
?s dbo:location dbr:East_Asia.
}
ORDER BY ASC(?s)

58

Quiz (5)
5.1 Find books on DBpedia.
5.2 Find the author of

The_7_Habits_of_Highly_Effective_People on DBpedia.
5.3 Find the total number of pages the aforementioned book.

59

Answer (5)
SELECT ?book_name
WHERE {
?book_name rdf:type dbo:Book.
}

SELECT ?author
WHERE {
<http://DBpedia.org/resource/The_7_Habits_of_Highly_Effective_People>
dbo:author ?author.
}

60

Answer (5)
SELECT ?author ?page
WHERE {
<http://DBpedia.org/resource/The_7_Habits_of_Highly_Effective_People>
dbo:author ?author;
dbo:numberOfPages ?page.
}

61

Quiz (6)
7.1 Using dbr:Walt_Disney_Studios_Motion_Pictures, find

films distributed by Disney.


7.2 Find all animated films within query results using
dct:subject.

62

Answer (6)
select ?s
where {
?s dbo:distributor dbr:Walt_Disney_Studios_Motion_Pictures.
}

select ?s
where {
?s dbo:distributor dbr:Walt_Disney_Studios_Motion_Pictures.
?s dct:subject dbc:Walt_Disney_Animation_Studios_films.
}

63

Quiz (7)
Find living writers on DBpedia, restricting the results using

age > 50.

64

Answer (7)
SELECT ?s ?birthYear
WHERE
{
?s a dbo:Writer .
?s dbo:birthYear ? birthYear.
FILTER (? birthYear<= 1966-07-10 ^^xsd:date)
FILTER NOT EXISTS {?s dbo:deathYear ?deathyear}
}
ORDER BY ASC(?s)

The NOT EXISTS keyword will be explained shortly.

65

Quiz (8)
9.1 Find all musicians on DBpedia.
9.2 Narrow the ensuing results to albums with the artist

Timbaland as producer and display them with labels.


9.3 Find albums released in the 1990s and sort them in
ascending order according to release date.
9.4 Further restrict the results the soul music genre.

66

Answer (8)
SELECT ?musicalartist
WHERE {
?musicalartist a dbo:MusicalArtist.
}

SELECT ?albumName
WHERE {
?album dbo:producer <http://DBpedia.org/resource/Timbaland> .
?album rdfs:label ?albumName .
}

67

Answer (8)
SELECT ?albumName ?releaseDate
WHERE {
?album dbo:producer <http://DBpedia.org/resource/Timbaland> ;
dbo:releaseDate ?releaseDate.
FILTER (?releaseDate >="1990-01-01"^^xsd:date
&& ?releaseDate <= "1999-12-31" ^^xsd:date)
?album rdfs:label ?albumName .
}
ORDER BY ASC (?releaseDate)

SELECT distinct ?album ?genre


WHERE {
?album dbo:producer <http://DBpedia.org/resource/Timbaland>;
dbo:releaseDate ?releaseDate .
FILTER (?releaseDate >="1990-01-01"^^xsd:date
&& ?releaseDate <= "1999-12-31" ^^xsd:date)
?album dbo:genre ?genre.
FILTER(?genre = dbr:Soul_music)
}

You might also like