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

More Related Content

KEY
SPARQL - Basic and Federated Queries
PDF
Tutorial: SPARQL 1.0 - I. Fundulaki - ESWC SS 2014
PDF
De-mystifying contributing to PostgreSQL
PDF
An Introduction to SPARQL
PPT
From SQL to SPARQL
PPT
SPARQL Tutorial
PPTX
SPARQL-DL - Theory & Practice
PPT
Using semantic to enhance content
SPARQL - Basic and Federated Queries
Tutorial: SPARQL 1.0 - I. Fundulaki - ESWC SS 2014
De-mystifying contributing to PostgreSQL
An Introduction to SPARQL
From SQL to SPARQL
SPARQL Tutorial
SPARQL-DL - Theory & Practice
Using semantic to enhance content

What's hot (6)

PDF
Graph Modelling
PPT
SPARQL in a nutshell
PDF
Primary analysis tutorial depracated
PDF
Drupal case study: ABC Dig Music
PDF
떠먹는 '오브젝트' Ch02 객체지향 프로그래밍
PDF
Backup recovery with PostgreSQL
Graph Modelling
SPARQL in a nutshell
Primary analysis tutorial depracated
Drupal case study: ABC Dig Music
떠먹는 '오브젝트' Ch02 객체지향 프로그래밍
Backup recovery with PostgreSQL
Ad

Viewers also liked (7)

PDF
ESWC SS 2012 - Friday Keynote Marko Grobelnik: Big Data Tutorial
PDF
ESWC SS 2013 - Thursday Keynote Vassilis Christophides: Preserving linked data
PDF
Programming with LOD
PDF
Semantics, Sensors, and the Social Web
PDF
IndustryInform Service of Mozaika
PDF
DAA Keynote- 99 Amazing Big Data Experiences
PDF
Mapping Lo Dto Proton Revised [Compatibility Mode]
ESWC SS 2012 - Friday Keynote Marko Grobelnik: Big Data Tutorial
ESWC SS 2013 - Thursday Keynote Vassilis Christophides: Preserving linked data
Programming with LOD
Semantics, Sensors, and the Social Web
IndustryInform Service of Mozaika
DAA Keynote- 99 Amazing Big Data Experiences
Mapping Lo Dto Proton Revised [Compatibility Mode]
Ad

Similar to ESWC SS 2012 - Monday Tutorial 2 Barry Norton: Introduction to SPARQL (20)

PDF
Mon norton tut_queryinglinkeddata02
PPTX
Querying Linked Data
PPTX
Querying Linked Data on Android
ODP
SPARQL 1.1 Update (2013-03-05)
PPTX
Introduction to SPARQL
PPTX
Introduction to SPARQL
PPTX
The Semantic Web #10 - SPARQL
PDF
ESWC SS 2012 - Monday Tutorial 2 Barry Norton: SPARQL 1.1 Query Language
PDF
Introduction to Bio SPARQL
ODP
NoSQL and Triple Stores
PPT
PPT
Querying the Semantic Web with SPARQL
PPTX
Snyder Kishimoto: RDA for Music: Popular Music, Jazz, and World Music Audio R...
PDF
Notes from the Library Juice Academy courses on “SPARQL Fundamentals”: Univer...
PPSX
Introduction to SPARQL
PDF
Mon fundulaki tut_querying linked data
PPTX
Semantic web meetup – sparql tutorial
PPT
Semantic Web
PPT
Semantic Web
PPTX
Intro to Linked, Dutch Ships and Sailors and SPARQL handson
Mon norton tut_queryinglinkeddata02
Querying Linked Data
Querying Linked Data on Android
SPARQL 1.1 Update (2013-03-05)
Introduction to SPARQL
Introduction to SPARQL
The Semantic Web #10 - SPARQL
ESWC SS 2012 - Monday Tutorial 2 Barry Norton: SPARQL 1.1 Query Language
Introduction to Bio SPARQL
NoSQL and Triple Stores
Querying the Semantic Web with SPARQL
Snyder Kishimoto: RDA for Music: Popular Music, Jazz, and World Music Audio R...
Notes from the Library Juice Academy courses on “SPARQL Fundamentals”: Univer...
Introduction to SPARQL
Mon fundulaki tut_querying linked data
Semantic web meetup – sparql tutorial
Semantic Web
Semantic Web
Intro to Linked, Dutch Ships and Sailors and SPARQL handson

More from eswcsummerschool (20)

PPTX
Semantic Aquarium - ESWC SSchool 14 - Student project
PPTX
Syrtaki - ESWC SSchool 14 - Student project
PDF
Keep fit (a bit) - ESWC SSchool 14 - Student project
PDF
Arabic Sentiment Lexicon - ESWC SSchool 14 - Student project
PPTX
FIT-8BIT An activity music assistant - ESWC SSchool 14 - Student project
PPTX
Personal Tours at the British Museum - ESWC SSchool 14 - Student project
PPTX
Exhibition recommendation using British Museum data and Event Registry - ESWC...
PPTX
Empowering fishing business using Linked Data - ESWC SSchool 14 - Student pro...
PDF
Tutorial: Social Semantic Web and Crowdsourcing - E. Simperl - ESWC SS 2014
PDF
Keynote: Global Media Monitoring - M. Grobelnik - ESWC SS 2014
PDF
Hands On: Amazon Mechanical Turk - M. Acosta - ESWC SS 2014
PDF
Tutorial: Querying a Marine Data Warehouse Using SPARQL - I. Fundulaki - ESWC...
PDF
Mon norton tut_publishing01
PDF
Mon domingue introduction to the school
PDF
Mon norton tut_querying cultural heritage data
PDF
Tue acosta hands_on_providinglinkeddata
PDF
Thu bernstein key_warp_speed
PDF
Fri schreiber key_knowledge engineering
PDF
Mon domingue key_introduction to semantic
PDF
Tue acosta tut_providing_linkeddata
Semantic Aquarium - ESWC SSchool 14 - Student project
Syrtaki - ESWC SSchool 14 - Student project
Keep fit (a bit) - ESWC SSchool 14 - Student project
Arabic Sentiment Lexicon - ESWC SSchool 14 - Student project
FIT-8BIT An activity music assistant - ESWC SSchool 14 - Student project
Personal Tours at the British Museum - ESWC SSchool 14 - Student project
Exhibition recommendation using British Museum data and Event Registry - ESWC...
Empowering fishing business using Linked Data - ESWC SSchool 14 - Student pro...
Tutorial: Social Semantic Web and Crowdsourcing - E. Simperl - ESWC SS 2014
Keynote: Global Media Monitoring - M. Grobelnik - ESWC SS 2014
Hands On: Amazon Mechanical Turk - M. Acosta - ESWC SS 2014
Tutorial: Querying a Marine Data Warehouse Using SPARQL - I. Fundulaki - ESWC...
Mon norton tut_publishing01
Mon domingue introduction to the school
Mon norton tut_querying cultural heritage data
Tue acosta hands_on_providinglinkeddata
Thu bernstein key_warp_speed
Fri schreiber key_knowledge engineering
Mon domingue key_introduction to semantic
Tue acosta tut_providing_linkeddata

Recently uploaded (20)

PDF
Daniels 2024 Inclusive, Sustainable Development
PDF
income tax laws notes important pakistan
DOCX
80 DE ÔN VÀO 10 NĂM 2023vhkkkjjhhhhjjjj
PPTX
Slide gioi thieu VietinBank Quy 2 - 2025
PPTX
basic introduction to research chapter 1.pptx
DOCX
Hand book of Entrepreneurship 4 Chapters.docx
PDF
ICv2 White Paper - Gen Con Trade Day 2025
PPTX
TRAINNING, DEVELOPMENT AND APPRAISAL.pptx
PDF
NEW - FEES STRUCTURES (01-july-2024).pdf
PPTX
Astra-Investor- business Presentation (1).pptx
PDF
533158074-Saudi-Arabia-Companies-List-Contact.pdf
PDF
Kishore Vora - Best CFO in India to watch in 2025.pdf
PDF
Robin Fischer: A Visionary Leader Making a Difference in Healthcare, One Day ...
PPTX
Project Management_ SMART Projects Class.pptx
PDF
ANALYZING THE OPPORTUNITIES OF DIGITAL MARKETING IN BANGLADESH TO PROVIDE AN ...
PDF
Family Law: The Role of Communication in Mediation (www.kiu.ac.ug)
PPTX
Slide gioi thieu VietinBank Quy 2 - 2025
PDF
Booking.com The Global AI Sentiment Report 2025
PPT
Lecture notes on Business Research Methods
PDF
Solaris Resources Presentation - Corporate August 2025.pdf
Daniels 2024 Inclusive, Sustainable Development
income tax laws notes important pakistan
80 DE ÔN VÀO 10 NĂM 2023vhkkkjjhhhhjjjj
Slide gioi thieu VietinBank Quy 2 - 2025
basic introduction to research chapter 1.pptx
Hand book of Entrepreneurship 4 Chapters.docx
ICv2 White Paper - Gen Con Trade Day 2025
TRAINNING, DEVELOPMENT AND APPRAISAL.pptx
NEW - FEES STRUCTURES (01-july-2024).pdf
Astra-Investor- business Presentation (1).pptx
533158074-Saudi-Arabia-Companies-List-Contact.pdf
Kishore Vora - Best CFO in India to watch in 2025.pdf
Robin Fischer: A Visionary Leader Making a Difference in Healthcare, One Day ...
Project Management_ SMART Projects Class.pptx
ANALYZING THE OPPORTUNITIES OF DIGITAL MARKETING IN BANGLADESH TO PROVIDE AN ...
Family Law: The Role of Communication in Mediation (www.kiu.ac.ug)
Slide gioi thieu VietinBank Quy 2 - 2025
Booking.com The Global AI Sentiment Report 2025
Lecture notes on Business Research Methods
Solaris Resources Presentation - Corporate August 2025.pdf

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

  • 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
  • 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 • 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
  • 15. 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
  • 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