Introduction to SPARQL

Introduction to SPARQL
Overview
•
•
•
•
•
•
•
•

What is SPARQL?
ASK and SELECT query forms
UNION and ORDER BY
Subsets of results
FILTER expressions and functions
OPTIONAL
DESCRIBE and CONSTRUCT query forms
Common problems

Introduction to SPARQL

#2
What is SPARQL?
• SPARQL is a query language for RDF data
– http://www.w3.org/TR/rdf-sparql-query/

• It is also a protocol
• It is designed around graph patterns
– Graph patterns use Turtle syntax

• SPARQL 1.0 is query only
– SPARQL 1.1 (covered next, not yet a recommendation) includes
syntax for updates

Introduction to SPARQL

#3
Ask
• Did 'Steve Hillage' make the album 'Fish Rising'?
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
ASK { dbpedia:Steve_Hillage foaf:made ?album .
?album dc:title "Fish Rising" . }

• Namespaces are added with 'PREFIX' directive
• Statement patterns that make up the graph are
between {} brackets

Introduction to SPARQL

#4
Select
• What albums and tracks did 'Steve Hillage' make?
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/elements/1.1/>
foaf: <http://xmlns.com/foaf/0.1/>
dbpedia: <http://dbpedia.org/resource/>
music-ont: <http://purl.org/ontology/mo/>

SELECT ?album_name ?track_title
WHERE {
dbpedia:Steve_Hillage foaf:made ?album .
?album dc:title ?album_name.
?album music-ont:track ?track .
?track dc:title ?track_title . }

• All variables in a query can be selected with '*'
– i.e. SELECT * WHERE { … }

Introduction to SPARQL

#5
Union
• 'Steve Hillage' and 'Jimi Hendrix'?
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/elements/1.1/>
foaf: <http://xmlns.com/foaf/0.1/>
dbpedia: <http://dbpedia.org/resource/>
music-ont: <http://purl.org/ontology/mo/>

SELECT ?album_name ?track_title
WHERE
{
{
dbpedia:Steve_Hillage foaf:made ?album .
}
UNION
{
dbpedia:Jimi_Hendrix foaf:made ?album .
}
?album dc:title ?album_name.
?album music-ont:track ?track .
?track dc:title ?track_title .
}

Introduction to SPARQL

#6
Order By
• In what order did 'Steve Hillage' release his albums?
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/elements/1.1/>
foaf: <http://xmlns.com/foaf/0.1/>
dbpedia: <http://dbpedia.org/resource/>
music-ont: <http://purl.org/ontology/mo/>

SELECT distinct ?album_name ?date
WHERE
{
dbpedia:Steve_Hillage foaf:made ?album .
?album dc:title ?album_name.
?album music-ont:track ?track .
?album dc:date ?date.
} ORDER BY( ?date)

Introduction to SPARQL

#7
Subsets of results
• What albums and tracks did 'Steve Hillage' make?
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/elements/1.1/>
foaf: <http://xmlns.com/foaf/0.1/>
dbpedia: <http://dbpedia.org/resource/>
music-ont: <http://purl.org/ontology/mo/>

SELECT ?album_name ?track_title
WHERE {
dbpedia:Steve_Hillage foaf:made ?album .
?album dc:title ?album_name.
?album music-ont:track ?track .
?track dc:title ?track_title . }
OFFSET 10
LIMIT 15

• Start after the 10th solution, provide (up to) 15 more
Introduction to SPARQL

#8
Filtering solutions
• What are 'Steve Hillage' longer tracks?
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/elements/1.1/>
foaf: <http://xmlns.com/foaf/0.1/>
dbpedia: <http://dbpedia.org/resource/>
music-ont: <http://purl.org/ontology/mo/>

SELECT ?album_name ?track_title ?duration
WHERE {
dbpedia:Steve_Hillage foaf:made ?album .
?album dc:title ?album_name.
?album music-ont:track ?track .
?track dc:title ?track_title .
?track music-ont:duration ?duration .
FILTER( ?duration > 500000 ).
}

• Allow only those solutions where the track length is
more than 500 seconds
Introduction to SPARQL

#9
Functions
• What albums and tracks did 'Steve Hillage' make?
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/elements/1.1/>
foaf: <http://xmlns.com/foaf/0.1/>
dbpedia: <http://dbpedia.org/resource/>
music-ont: <http://purl.org/ontology/mo/>

SELECT ?album_name ?track_title
WHERE {
dbpedia:Steve_Hillage foaf:made ?album .
?album dc:title ?album_name.
?album music-ont:track ?track .
?track dc:title ?track_title .
FILTER( REGEX( ?track_title, "hurd.*", "i") ) }

• Allow only those solutions where the track title
contains a substring that begins with 'hurd' in any case
Introduction to SPARQL

#10
Filter expressions
• Can be combined with logical, arithmetic, casts and
comparisons, e.g.
FILTER( ?age < 30 && xsd:double(?weight) < (?empty + ?fuel) )

• Also tests, accessors, e.g.
FILTER( isURI( ?id ) && datatype( ?age ) = xsd:double )

Introduction to SPARQL

#11
Optionals
• Parts of the matching graph can be optional
• Use OPTIONAL {} to enclose the optional parts
• Can test in filter expressions if variable are bound
• Using logical NOT '!' it is possible to filter out
solutions for the optional part

Introduction to SPARQL

#12
Optionals
• Albums from artists who are not dead
PREFIX
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/elements/1.1/>
foaf: <http://xmlns.com/foaf/0.1/>
dbpedia: <http://dbpedia.org/resource/>
music-ont: <http://purl.org/ontology/mo/>
dbp-ont: <http://dbpedia.org/ontology/>

SELECT distinct ?artist_name ?album_name ?place_of_death
WHERE {
?artist foaf:made ?album .
?artist foaf:name ?artist_name .
?album dc:title ?album_name.
?album music-ont:track ?track .
?track dc:title ?track_title .
OPTIONAL { ?artist dbp-ont:deathPlace ?place_of_death }
FILTER( ! BOUND( ?place_of_death ) ).
}
Introduction to SPARQL

#13
Describe
• Returns RDF statements about any resource
identified by the query
PREFIX dbpedia: <http://dbpedia.org/resource/>
DESCRIBE dbpedia:Steve_Hillage

PREFIX foaf:
<http://xmlns.com/foaf/0.1/>
DESCRIBE ?x ?y <http://example.org/>
WHERE
{?x foaf:knows ?y}

Introduction to SPARQL

#14
Construct
• Returns RDF statements created from variable
bindings
PREFIX
PREFIX
PREFIX
PREFIX

dc: <http://purl.org/dc/elements/1.1/>
foaf: <http://xmlns.com/foaf/0.1/>
dbpedia: <http://dbpedia.org/resource/>
music-ont: <http://purl.org/ontology/mo/>

CONSTRUCT { ?album dc:creator dbpedia:Steve_Hillage }
WHERE {
dbpedia:Steve_Hillage foaf:made ?album .
?album dc:title ?album_name.
?album music-ont:track ?track . }

Introduction to SPARQL

#15
Common Problems
• Spelling mistakes in identifiers are not noticed
– It is simply a different URI
– URIs are LONG, are case-sensitive, have '#' or '/' for local part

• Easy to use the right local name with the wrong
namespace
• Literals are compared on type, language and value
– "Steve Hillage"@EN is not the same as "Steve Hillage"
– "3" is not the same as "3"^^xsd:integer

Introduction to SPARQL

#16
Summary
• SPARQL is a powerful query language for RDF
• SPARQL 1.0 is read-only
• SPARQL 1.1 will allow updates

Introduction to SPARQL

#17

ESWC SS 2012 - Monday Tutorial 2 Barry Norton: Introduction to SPARQL

  • 1.
  • 2.
    Overview • • • • • • • • What is SPARQL? ASKand SELECT query forms UNION and ORDER BY Subsets of results FILTER expressions and functions OPTIONAL DESCRIBE and CONSTRUCT query forms Common problems Introduction to SPARQL #2
  • 3.
    What is SPARQL? •SPARQL is a query language for RDF data – http://www.w3.org/TR/rdf-sparql-query/ • It is also a protocol • It is designed around graph patterns – Graph patterns use Turtle syntax • SPARQL 1.0 is query only – SPARQL 1.1 (covered next, not yet a recommendation) includes syntax for updates Introduction to SPARQL #3
  • 4.
    Ask • Did 'SteveHillage' make the album 'Fish Rising'? PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX dbpedia: <http://dbpedia.org/resource/> ASK { dbpedia:Steve_Hillage foaf:made ?album . ?album dc:title "Fish Rising" . } • Namespaces are added with 'PREFIX' directive • Statement patterns that make up the graph are between {} brackets Introduction to SPARQL #4
  • 5.
    Select • What albumsand tracks did 'Steve Hillage' make? PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> SELECT ?album_name ?track_title WHERE { dbpedia:Steve_Hillage foaf:made ?album . ?album dc:title ?album_name. ?album music-ont:track ?track . ?track dc:title ?track_title . } • All variables in a query can be selected with '*' – i.e. SELECT * WHERE { … } Introduction to SPARQL #5
  • 6.
    Union • 'Steve Hillage'and 'Jimi Hendrix'? PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> SELECT ?album_name ?track_title WHERE { { dbpedia:Steve_Hillage foaf:made ?album . } UNION { dbpedia:Jimi_Hendrix foaf:made ?album . } ?album dc:title ?album_name. ?album music-ont:track ?track . ?track dc:title ?track_title . } Introduction to SPARQL #6
  • 7.
    Order By • Inwhat order did 'Steve Hillage' release his albums? PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> SELECT distinct ?album_name ?date WHERE { dbpedia:Steve_Hillage foaf:made ?album . ?album dc:title ?album_name. ?album music-ont:track ?track . ?album dc:date ?date. } ORDER BY( ?date) Introduction to SPARQL #7
  • 8.
    Subsets of results •What albums and tracks did 'Steve Hillage' make? PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> SELECT ?album_name ?track_title WHERE { dbpedia:Steve_Hillage foaf:made ?album . ?album dc:title ?album_name. ?album music-ont:track ?track . ?track dc:title ?track_title . } OFFSET 10 LIMIT 15 • Start after the 10th solution, provide (up to) 15 more Introduction to SPARQL #8
  • 9.
    Filtering solutions • Whatare 'Steve Hillage' longer tracks? PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> SELECT ?album_name ?track_title ?duration WHERE { dbpedia:Steve_Hillage foaf:made ?album . ?album dc:title ?album_name. ?album music-ont:track ?track . ?track dc:title ?track_title . ?track music-ont:duration ?duration . FILTER( ?duration > 500000 ). } • Allow only those solutions where the track length is more than 500 seconds Introduction to SPARQL #9
  • 10.
    Functions • What albumsand tracks did 'Steve Hillage' make? PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> SELECT ?album_name ?track_title WHERE { dbpedia:Steve_Hillage foaf:made ?album . ?album dc:title ?album_name. ?album music-ont:track ?track . ?track dc:title ?track_title . FILTER( REGEX( ?track_title, "hurd.*", "i") ) } • Allow only those solutions where the track title contains a substring that begins with 'hurd' in any case Introduction to SPARQL #10
  • 11.
    Filter expressions • Canbe combined with logical, arithmetic, casts and comparisons, e.g. FILTER( ?age < 30 && xsd:double(?weight) < (?empty + ?fuel) ) • Also tests, accessors, e.g. FILTER( isURI( ?id ) && datatype( ?age ) = xsd:double ) Introduction to SPARQL #11
  • 12.
    Optionals • Parts ofthe matching graph can be optional • Use OPTIONAL {} to enclose the optional parts • Can test in filter expressions if variable are bound • Using logical NOT '!' it is possible to filter out solutions for the optional part Introduction to SPARQL #12
  • 13.
    Optionals • Albums fromartists who are not dead PREFIX PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> dbp-ont: <http://dbpedia.org/ontology/> SELECT distinct ?artist_name ?album_name ?place_of_death WHERE { ?artist foaf:made ?album . ?artist foaf:name ?artist_name . ?album dc:title ?album_name. ?album music-ont:track ?track . ?track dc:title ?track_title . OPTIONAL { ?artist dbp-ont:deathPlace ?place_of_death } FILTER( ! BOUND( ?place_of_death ) ). } Introduction to SPARQL #13
  • 14.
    Describe • Returns RDFstatements about any resource identified by the query PREFIX dbpedia: <http://dbpedia.org/resource/> DESCRIBE dbpedia:Steve_Hillage PREFIX foaf: <http://xmlns.com/foaf/0.1/> DESCRIBE ?x ?y <http://example.org/> WHERE {?x foaf:knows ?y} Introduction to SPARQL #14
  • 15.
    Construct • Returns RDFstatements created from variable bindings PREFIX PREFIX PREFIX PREFIX dc: <http://purl.org/dc/elements/1.1/> foaf: <http://xmlns.com/foaf/0.1/> dbpedia: <http://dbpedia.org/resource/> music-ont: <http://purl.org/ontology/mo/> CONSTRUCT { ?album dc:creator dbpedia:Steve_Hillage } WHERE { dbpedia:Steve_Hillage foaf:made ?album . ?album dc:title ?album_name. ?album music-ont:track ?track . } Introduction to SPARQL #15
  • 16.
    Common Problems • Spellingmistakes in identifiers are not noticed – It is simply a different URI – URIs are LONG, are case-sensitive, have '#' or '/' for local part • Easy to use the right local name with the wrong namespace • Literals are compared on type, language and value – "Steve Hillage"@EN is not the same as "Steve Hillage" – "3" is not the same as "3"^^xsd:integer Introduction to SPARQL #16
  • 17.
    Summary • SPARQL isa powerful query language for RDF • SPARQL 1.0 is read-only • SPARQL 1.1 will allow updates Introduction to SPARQL #17