L28 Irsw Sparql
L28 Irsw Sparql
• # 01a-albums.sparql
• # dataset: beatles
SELECT ?album
WHERE {
?album rdf:type :Album .
}
album
:Please_Please_Me
• There are several syntactic simplifications we can do to this query:
• the WHERE keyword is optional for SELECT queries and
can be omitted
• If all the variables are being selected, then * can be used.
• Just like in RDF, the keyword a can be used instead
of rdf:type
RDF GRAPH (EG : BEATLE BAND)
SELECT ?album
WHERE {
?album rdf:type :Album .
}
Can be written as
# 01b-albums.sparql
# dataset: beatles
SELECT * { ?album a :Album }
BASIC GRAPH PATTERN
# 02-albums-artists.sparql
# dataset: beatles
SELECT *
{
?album a :Album .
?album :artist ?artist .
}
The second triple pattern in this query will match the triples
with :artist predicate
album artist
:Please_Please_Me :The_Beatles
• Now let’s add a third triple pattern to require that the returned artists should be of
the SoloArtist type:
# 03-albums-solo-artists.sparql
# dataset: beatles
SELECT *
{
?album a :Album .
?album :artist ?artist .
?artist a :SoloArtist .
}
# 06-albums-dates-limited.sparql
# dataset: music
SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date
}
ORDER BY desc(?date)
LIMIT 2
• When a query returns too many results we can limit the results by
using the LIMIT keyword:
FILTERING RESULTS
SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date
FILTER (?date >= "1970-01-01"^^xsd:date)
}
ORDER BY ?date
FILTER BASED ON YEAR
SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date
FILTER (year(?date) >= 1970)
}
ORDER BY ?date
SELECT *
{
?album a :Album ;
:artist ?artist ;
:date ?date
BIND (year(?date) as ?year)
FILTER (?year >= 1970)
}
• If we want to display the years in which album released
• SELECT ?year
{
?album a :Album ;
:artist ?artist ;
:date ?date
BIND (year(?date) as ?year)
}
We might get
1956
1956
1957
REMOVE DUPLICATES
Having (count(?date)>1)
PRACTICE
QUERIES OVER THIS DATA
Find the location of the Baron Way building.
Find where the Baron Way Apartment is located.
Find all the information about Baron Way Apartment in
the triple store
Find 10 entries of the information about Baron
Way Apartment
Find all the apartments which are part of a
building located in Amsterdam.
Find all the apartments that have 3 bedrooms.
find all the apartments that have greater than 2
bedrooms
QUERIES OVER THIS DATA