2. 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
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 '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
5. 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
7. 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
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
• 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
10. 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
11. 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
12. 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
13. 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
14. 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
16. 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
17. 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