Sei sulla pagina 1di 87

106

USING OPERATORS
(FILTER and related operators MINUS, OPTIONAL, UNION)

107

FILTER Tutorial (1)


rdfs:label puts @en, @de, @ko marks at the ends of labels in order to

correctly represent their language.


Results may be filtered to include only those in the requested language.
FILTER (lang(?artistName) = en)
FILTER (lang(?albumName) = en)

?artistName is English
?albumName is English

AND facilitates the use of multiple FILTER clauses.


Further filtering is made available through str and datatype.

FILTER (datatype(?dt) = xsd:date)


FILTER (lang(?albumName) = en)

?dt values filtered in date


?albumName is in English

108

FILTER Tutorial (2)


FILTER(!bound(?something))
filters results if they have values in the ?something variable
NOT EXISTS { ?s predicates ?something }
filters results if the ?something variables have values

109

FILTER Tutorial (3)


Using regular expressions can lead to results which include
stipulated characters.
syntax

FILTER regex(?variable, str, i)


variables, string order
i allows for both uppercase and lowercase
i is optional.

Possible to set pattern style:


email pattern ([a-z]+@)([a-z]+)(\\.[a-z]+)

110

FILTER Example (1)


RDF Data:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
_:a foaf:givenName "Alice".
_:b foaf:givenName "Bob" .
_:b dc:date "2005-04-04T04:04:04Z"^^xsd:dateTime .

111

FILTER Example (1)


Find the names of people who have no dc:date values.

112

Answer to Filter Example (1)


Query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?givenName
WHERE { ?x foaf:givenName ?givenName .
OPTIONAL { ?x dc:date ?date } .
FILTER ( !bound(?date) ) }

OPTIONAL will be explained shortly.

113

Answer to Filter Example (1)


Result:
givenName
Alice

reference: http://www.w3.org/TR/rdf-sparql-query/

114

FILTER Example (2)


RDF Data:
@prefix ab: <http://ld4pe.org/ns/addressbook#> .
@prefix d: <http://ld4pe.org/ns/data#> .
d:i0432 ab:firstName "Richard" ;
ab:lastName "Mutt" ;
ab:instrument "sax" ;
ab:instrument "clarinet" .
d:i9771 ab:firstName "Cindy" ;
ab:lastName "Marshall" ;
ab:instrument "drums" .
d:i8301 ab:firstName "Craig" ;
ab:lastName "Ellis" ;
ab:instrument "trumpet" .

115

FILTER Example (2)


Find the names (first name, last name) of people who play the trumpetor
saxophone and specify which instrument they play.

116

Answer to Filter Example (2)


Query:
PREFIX ab: <http://ld4pe.org/ns/addressbook#> .
SELECT ?first ?last ?instrument
WHERE
{ ?person ab:firstName ?first ;
ab:lastName ?last ;
ab:instrument ?instrument .
FILTER (?instrument IN("trumpet", "sax")).
}

Result:

?first

?last

?instrument

"Craig"

"Ellis"

" trumpet"

"Richard"

"Mutt"

sax

117

FILTER Example (3)


RDF Data:
@prefix ab: <http://ld4pe.org/ns/addressbook#> .
@prefix d: <http://ld4pe.org/ns/data#> .
d:i0432 ab:firstName "Richard";
ab:lastName "Mutt" ;
ab:homeTel "(229) 276-5135";
ab:nick "Dick" ;
ab:email "richard49@hotmail.com" .
d:i9771 ab:firstName "Cindy" ;
ab:lastName "Marshall" ;
ab:homeTel "(245) 646-5488" ;
ab:email "cindym@gmail.com" .
d:i8301 ab:firstName "Craig" ;
ab:lastName "Ellis" ;
ab:workTel "(245) 315-5486" ;
ab:email "craigellis@yahoo.com" ;
ab:email "c.ellis@usairwaysgroup.com" .

118

FILTER Example (3)


Find the names (first name, last name) of people who have no
work telephone number.

119

Answer to Filter Example (3)


Query:
PREFIX ab: <http://ld4pe.org/ns/addressbook#> .
SELECT ?first ?last ?workNum
WHERE
{
?s ab:firstName ?first ;
ab:lastName ?last .
FILTER NOT EXISTS{ ?s ab:workTel ?workNum . }
}

Result:

?first

?last

"Cindy"

"Marshall"

"Richard"

"Mutt"

?workNum

120

FILTER OPERATOR
QUIZZES
DBpedia SPARQL Endpoint

121

Quiz (1)
1.1 Using DBpedia SPARQL Endpoint, find albums produced by

Timbaland and indicate them in many languages (rdfs:labels


could be helpful here).
1.2 Restrict results to English albums only.

122

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

SELECT ?albumName
WHERE {
?album dbo:producer <http://DBpedia.org/resource/Timbaland> .
?album rdfs:label ?albumName .
FILTER (lang(?albumName) = en)
}

123

Quiz (2)
Using DBpedia SPARQL Endpoint, find albums produced by

Timbaland and restrict results to make sure that the datatype


of release date is xsd:date.

124

Answer (2)
SELECT ?album ?rdate
WHERE {
?album dbo:producer <http://DBpedia.org/resource/Timbaland>;
dbp:released ?rdate.
filter (datatype(?rdate)= xsd:date).
}

125

Quiz (3)
Find American universities in which the number of students >

20,000 (xsd:integer).

126

Answer (3)
select ?univ
where{
?univ a dbo:University.
?univ dbo:country dbr:United_States.
?univ dbo:numberOfStudents ?number.
filter (?number >= "20000"^^xsd:integer)
}

127

Quiz (4)
Find California-based companies which deal in software or

hardware.

128

Answer (4)
select distinct ?firm
where{
?firm a dbo:Company.
?firm dbo:industry ?industry.
filter (?industry IN (dbr:Software, dbr:Computer_hardware))
?firm dbo:locationCity ?location.
filter (?location = dbr:California)
}

129

Quiz (5)
5.1 Find public companies in which the number of employees >

300,000 (xsd:integer).
5.2 Show the number of employees for each company.

130

Answer (5)
SELECT distinct ?s ?Employees
WHERE
{
?s dbo:type dbr:Public_company.
?s dbo:numberOfEmployees ?Employees.
FILTER(?Employees>"300000"^^xsd:integer).
}

131

Quiz (6)
6.1 Using DBpedia SPARQL Endpoint, find albums produced by

Timbaland which also have the term Who in their title.


6.2 The datatype of the release dates should be xsd:date.
Restrict these results to albums released in the 1990s.

132

Answer (6)
SELECT ?album ?rdate
WHERE {
?album dbo:producer <http://DBpedia.org/resource/Timbaland>;
dbp:released ?rdate.
filter regex(?album, "Who")
filter (datatype(?rdate)= xsd:date)
filter (?rdate >="1990-01-01"^^xsd:date
&& ?rdate <= "1999-12-31" ^^xsd:date).
}

133

Quiz (7)
Find movie directors (yago:FilmDirector110088200) who have

no start date for their active years (dbo:activeYearsStartYear).

134

Answer (7)
SELECT ?movieDirector ?date
WHERE
{
?movieDirector rdf:type yago:FilmDirector110088200.
OPTIONAL {?movieDirector dbo:activeYearsStartYear ?date.}
FILTER (!bound(?date))
}

135

Quiz (8)
Find California-based companies with no thumbnail

(dbo:thumbnail).

136

Answer (8)
select ?c
where {
?c a dbo:Company.
?c dbo:locationCity dbr:California.
optional {?c dbo:thumbnail ?thumbnail}.
filter (!bound(?thumbnail))
}

137

Quiz (9)
Find American universities with no information on the state

(dbo:state) or city (dbo:city) in which they are based.

138

Answer (9)
select ?univ ?state ?city
where{
?univ a dbo:University.
?univ dbo:country dbr:United_States.
Optional {?univ dbo:state ?state}
Optional {?univ dbo:city ?city}
filter (!bound(?state))
filter (!bound(?city))
}

139

Quiz (10)
10.1 Find basketball players (dbo:BasketballPlayer) who were in

the NBA before retiring


(dbc:National_Basketball_Association_players_with_retired_nu
mbers).
10.2 Identify the year in which these players retired.

140

Answer (10)
select ?univ ?state ?city
where{
?univ a dbo:University.
?univ dbo:country dbr:United_States.
Optional {?univ dbo:state ?state}
Optional {?univ dbo:city ?city}
filter (!bound(?state))
filter (!bound(?city))
}

141

Quiz (11)
Using NOT EXISTS, find American professional wrestlers

(yago:WikicatAmericanProfessionalWrestlers) who do not have


a starting date for active years.

142

Answer (11)
SELECT ?ProfWr
WHERE
{
?ProfWr rdf:type
yago:WikicatAmericanProfessionalWrestlers.
FILTER NOT EXISTS{?ProfWr dbo:debut ?Ddate.}
}

143

Quiz (12)
Using NOT EXISTS, find California-based companies that do

not have a thumbnail.

144

Answer (12)
select ?c
where {
?c a dbo:Company.
?c dbo:locationCity dbr:California.
filter not exists {?c dbo:thumbnail ?thumbnail.}
}

145

Quiz (13)
Using NOT EXISTS, find American universities with no

information on the state or city in which they are based.

146

Answer (13)
select ?univ
where{
?univ a dbo:University.
?univ dbo:country dbr:United_States.
filter not exists {?univ dbo:state ?state}
filter not exists {?univ dbo:city ?city}
}

147

Quiz (14)
Find Michael Jackson albums produced by Quincy Jones

(dbr:Quincy_Jones).

148

Answer (14)
SELECT ?albumname
WHERE
{
?albumname dbo:artist dbr:Michael_Jackson;
rdf:type dbo:Album;
dbp:producer ?producer.
FILTER (?producer = dbr:Quincy_Jones).
}

149

Quiz (15)
Find universities with the term California in their name.

150

Answer (15)
select ?univ
where{
?univ a dbo:University.
filter regex(?univ, California")
}

151

Quiz (16)
Without differentiating between upper and lowercase letters,

find James Brown albums with the term funky in the title.

152

Answer (16)
SELECT ?funkyAlbum
WHERE
{
?funkyAlbum dbo:artist dbr:James_Brown;
rdf:type dbo:Album.
FILTER regex(?funkyAlbum,"funky","i")
}

153

Quiz (17)
Find songs with lyrics written by Freddie Mercury

(dbr:Freddie_Mercury) which have the term love in their


title.

154

Answer (17)
SELECT ?QLsong
WHERE
{
?QLsong dbo:writer <http://DBpedia.org/
resource/Freddie_Mercury>.
FILTER regex(?QLsong,"love","i")
}

155

Quiz (18)
Find universities which use veritas or truth as their

motto.

156

Answer (18)
select ?s ?motto
where
{
?s rdf:type dbo:University;
dbo:motto ?motto.
FILTER(?motto IN("Veritas","Truth")).
}

157

Quiz (19)
Find actors (umbel-rc:Actor) who have married Brad Pitt.

158

Answer (19)
select ?s
where
{
?s rdf:type umbel-rc:Actor;
dbp:spouse ?spouse.
FILTER regex(?spouse, "Brad_Pitt").
}

159

MINUS Example (1)


RDF Data:
@prefix ab: <http://ld4pe.org/ns/addressbook#> .
@prefix d: <http://ld4pe.org/ns/data#> .
d:i0432 ab:firstName "Richard";
ab:lastName "Mutt" ;
ab:homeTel "(229) 276-5135";
ab:nick "Dick" ;
ab:email "richard49@hotmail.com" .
d:i9771 ab:firstName "Cindy" ;
ab:lastName "Marshall" ;
ab:homeTel "(245) 646-5488" ;
ab:email "cindym@gmail.com" .
d:i8301 ab:firstName "Craig" ;
ab:lastName "Ellis" ;
ab:workTel "(245) 315-5486" ;
ab:email "craigellis@yahoo.com" ;
ab:email "c.ellis@usairwaysgroup.com" .

160

MINUS Example (1)


Find the names (first name, last name) of people who have no
work telephone number.

161

MINUS Example (1)


Query:
PREFIX ab: <http://ld4pe.org/ns/addressbook#> .
SELECT ?first ?last ?workNum
WHERE
{
?s ab:firstName ?first ;
ab:lastName ?last .
MINUS { ?s ab:workTel ?workNum . }
}

Result:

?first

?last

"Cindy"

"Marshall"

"Richard"

"Mutt"

?workNum

162

Quiz (1)
Find South Korean soccer players who have never been on the

national team (dbp:nationalteam).

163

Answer (1)
SELECT ?Kplayer
WHERE
{
?Kplayer rdf:type dbo:SoccerPlayer;
dbo:birthPlace dbr:South_Korea.
MINUS{?Kplayer dbp:nationalteam ?national.}
}

164

Quiz (2)
Find South Korean baseball players without a specified position.

165

Answer (2)
SELECT ?name
WHERE
{
?name a dbo:BaseballPlayer;
dbp:birthPlace dbr:South_Korea.
MINUS {?name dbp:position ?p}
}

166

OPTIONAL Example (1)


RDF Data:
@prefix ab: <http://ld4pe.org/ns/addressbook#> .
@prefix d: <http://ld4pe.org/ns/data#> .
d:i0432 ab:firstName "Richard";
ab:lastName "Mutt" ;
ab:homeTel "(229) 276-5135";
ab:nick "Dick" ;
ab:email "richard49@hotmail.com" .
d:i9771 ab:firstName "Cindy" ;
ab:lastName "Marshall" ;
ab:homeTel "(245) 646-5488" ;
ab:email "cindym@gmail.com" .
d:i8301 ab:firstName "Craig" ;
ab:lastName "Ellis" ;
ab:workTel "(245) 315-5486" ;
ab:email "craigellis@yahoo.com" ;
ab:email "c.ellis@usairwaysgroup.com" .

167

OPTIONAL Example (1)


Find peoples first name, last name, and work telephone.

168

Answer to Optional Example (1)


Query 1:
@prefix ab: <http://ld4pe.org/ns/addressbook#> .
@prefix d: <http://ld4pe.org/ns/data#> .
SELECT ?first ?last ?workTel
WHERE
{
?s ab:firstName ?first ;
ab:lastName ?last ;
ab:workTel ?workTel .
}

Result:
?first

?last

?worktel

"Craig"

"Ellis"

"(245) 315-5486"

169

OPTIONAL Example (2)


Find peoples first name, last name, and work telephone. (Should
they have no work telephone, leave this result empty.)

170

Answer to Optional Example (2)


Query II:
@prefix ab: <http://ld4pe.org/ns/addressbook#> .
@prefix d: <http://ld4pe.org/ns/data#> .
SELECT ?first ?last ?workTel
WHERE
{
?s ab:firstName ?first ;
ab:lastName ?last ;
OPTIONAL
{?s ab:workTel ?workTel . }
}

Result:
?first

?last

?worktel

"Craig"

"Ellis"

"(245) 315-5486"

"Cindy"

"Marshall"

"Richard"

"Mutt"

171

OPTIONAL Example (3)


Find peoples first name, last name, work telephone, and
nickname. Work telephone and nickname results are optional,
but they must exist simultaneously. Should they have no work
telephone and nickname, leave these results empty.

172

Answer to Optional Example (3)


Query:
@prefix ab: <http://ld4pe.org/ns/addressbook#> .
@prefix d: <http://ld4pe.org/ns/data#> .
SELECT ?first ?last ?worktel ?nick
WHERE
{
?s ab:firstName ?first ;
ab:lastName ?last .
OPTIONAL
{
?s ab:workTel ?workTel ;
ab:nick ?nick .
}
}

Result:
?first

?last

"Craig"

"Ellis"

"Cindy"

"Marshall"

"Richard"

"Mutt"

?worktel

?nick

173

OPTIONAL Example (4)


Find peoples first name, last name, work telephone, and
nickname. Work telephone and nickname results are optional and
do not have to exist simultaneously.

174

Answer to Optional Example (4)


Query:
@prefix ab: <http://ld4pe.org/ns/addressbook#> .
@prefix d: <http://ld4pe.org/ns/data#> .
SELECT ?first ?last ?worktel ?nick
WHERE
{
?s ab:firstName ?first ;
ab:lastName ?last .
OPTIONAL
{?s ab:workTel ?workTel . }
OPTIONAL
?first
{?s ab:nick ?nick .}
}
"Craig"

Result:
?last

?worktel

"Ellis"

"(245)
315-5486"

"Cindy"

"Marshall"

"Richard"

"Mutt"

?nick

Dick

175

Quiz (1)
Find all Steven Spielberg films and specify their budgets where

such information is available.

176

Answer (1)
SELECT ?movie ?budget
WHERE
{
?movie dbo:director <http://DBpedia.org/resource/Steven_Spielberg>.
OPTIONAL {?movie dbo:budget ?budget}
}

177

Quiz (2)
2.1 Find soccer players born in South Korea or in your home
country using http://DBpedia.org/sparql
2.2 Find additional information about their height (dbo:height).
Should there be no height information available, leave this result
blank.

178

Answer (2)
select distinct ?s ?height
where {
?s a dbo:SoccerPlayer ;
dbp:birthPlace dbr:South_Korea .
OPTIONAL
{?s dbo:height ?height}
} order by ASC(?s)

179

UNION Example (1)


RDF Data:
@prefix ab: <http://ld4pe.org/ns/addressbook#> .
@prefix d: <http://ld4pe.org/ns/data#> .
d:i0432 ab:firstName "Richard" ;
ab:lastName "Mutt" ;
ab:instrument "sax" ;
ab:instrument "clarinet" .
d:i9771 ab:firstName "Cindy" ;
ab:lastName "Marshall" ;
ab:instrument "drums" .
d:i8301 ab:firstName "Craig" ;
ab:lastName "Ellis" ;
ab:instrument "trumpet" .

180

UNION Example (1)


Find the first and last names of people who can play the
saxophone or trumpet.

181

Answer to Union Example (1)


Query Answer 1:
@prefix ab: <http://ld4pe.org/ns/addressbook#> .
@prefix d: <http://ld4pe.org/ns/data#> .
SELECT ?first ?last ?instrument
WHERE
{
{ ?person ab:firstName ?first ;
ab:lastName ?last ;
ab:instrument "trumpet" ;
ab:instrument ?instrument .
}
UNION
{ ?person ab:firstName ?first ;
ab:lastName ?last ;
ab:instrument "sax" ;
ab:instrument ?instrument .
}
}

Result:
?first

?last

?instrument

"Craig"

"Ellis"

" trumpet"

"Richard"

"Mutt"

sax

"Richard"

"Mutt"

clarinet

182

Answer to Union Example (1)


Query Answer 2: Eliminating Redundancy
@prefix ab: <http://ld4pe.org/ns/addressbook#> .
@prefix d: <http://ld4pe.org/ns/data#> .
SELECT ?first ?last ?instrument
WHERE
{
?person ab:firstName ?first ;
ab:lastName ?last ;
ab:instrument ?instrument .

Result:

{ ?person ab:instrument "sax" . }


UNION
{ ?person ab:instrument "trumpet" . }
}

?first

?last

?instrument

"Craig"

"Ellis"

" trumpet"

"Richard"

"Mutt"

sax

"Richard"

"Mutt"

clarinet

183

Quiz (1)
1.1 Find soccer players who were born in Seoul (dbr:Seoul)
using DBpedia.org/sparql and display the results alongside the
citys population.
1.2 Sort results in ascending order.

184

Answer (1)
select distinct ?s ?birthplace ?population
where {
{
?s a dbo:SoccerPlayer ;
dbp:birthPlace dbr:Seoul ;
dbp:birthPlace ?birthplace.
}
UNION
{
dbr:Seoul dbo:populationTotal ?population.
}
} order by ASC(?s)

185

Quiz (2)
2.1 Find soccer players who were born in Seoul (dbr:Seoul)

using DBpedia.org/sparql.
2.2 Show soccer players who were born in Incheon
(dbr:Incheon) using the UNION keyword.
2.3 Sorts results in descending order.

186

Answer (2)
select distinct ?s ?birthplace
where {
{
?s a dbo:SoccerPlayer ;
dbp:birthPlace dbr:Seoul ;
dbp:birthPlace ?birthplace.
}
UNION
{
?s a dbo:SoccerPlayer ;
dbp:birthPlace dbr:Incheon ;
dbp:birthPlace ?birthplace.
}
} order by DESC(?s)

187

Quiz (3)
Find all American rappers and indicate those from New York

and California.

188

Answer (3)
SELECT ?MC ?bornIn
WHERE
{
?MC a yago:Rapper110507482;
a yago:WikicatAmericanPeople.
?MC dbo:birthPlace ?bornIn.
{?MC dbo:birthPlace dbr:New_York.}
UNION
{?MC dbo:birthPlace dbr:California.}
}

189

Quiz (4)
Find American and British universities and display their name

and country.

190

Answer (4)
select ?USuniv ?UKuniv
where{
{?USuniv a dbo:University.
?USuniv dbo:country dbr:United_States.
?USuniv dbo:country ?country.}
union
{?UKuniv a dbo:University.
?UKuniv dbo:country dbr:England.
?UKuniv dbo:country ?country.}
}

191

Quiz (5)
Find writers who graduated from Harvard or Yale Law School

and show these results in ascending order.

192

Answer (5)
select ?s ?university
where
{
{?s dbo:almaMater dbr:Harvard_University;
dbo:almaMater ?university;
a dbo:Writer.}
UNION
{?s dbo:almaMater dbr:Yale_Law_School;
dbo:almaMater ?university;
a dbo:Writer.}
} order by ASC(?s)