Post on 14-Jul-2020
ICWE 2012 Tutorial
An Introduction to SPARQL and Queries over Linked Data
● ● ●
Hands-on Exercises
Olaf Hartighttp://olafhartig.de/foaf.rdf#olaf
@olafhartig
Database and Information Systems Research GroupHumboldt-Universität zu Berlin
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 2
Exercise 1
Ask for the title (property: 'dc:title') of each workshop (class: 'swc:WorkshopEvent').
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 3
Exercise 1
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?wt WHERE {
?w a swc:WorkshopEvent ;
dc:title ?wt .
}
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?wt WHERE {
?w a swc:WorkshopEvent ;
dc:title ?wt .
}
Ask for the title (property: 'dc:title') of each workshop (class: 'swc:WorkshopEvent').
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 4
Exercise 2
Ask for an ordered list of the subjects (property:'dc:subject') of all workshops; each subject must not appear more than once.
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 5
Exercise 2
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT DISTINCT ?s WHERE {
?w a swc:WorkshopEvent ;
dc:subject ?s .
}
ORDER By ?s
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT DISTINCT ?s WHERE {
?w a swc:WorkshopEvent ;
dc:subject ?s .
}
ORDER By ?s
Ask for an ordered list of the subjects (property:'dc:subject') of all workshops; each subject must not appear more than once.
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 6
Exercise 3
Ask for the title of workshops that have "Linked Data" as their subject.
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 7
Exercise 3
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?wt WHERE {
?w a swc:WorkshopEvent ;
dc:title ?wt ;
dc:subject "Linked Data" .
}
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?wt WHERE {
?w a swc:WorkshopEvent ;
dc:title ?wt ;
dc:subject "Linked Data" .
}
Ask for the title of workshops that have "Linked Data" as their subject.
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 8
Exercise 4
Does the result for the previous query contain the USEWOD workshop? Investigate why (using SPARQL queries similar to the previous ones) and adjust the query so that the USEWOD workshop becomes part of the result.
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 9
Exercise 4
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?wt WHERE {
?w a swc:WorkshopEvent ;
dc:title ?wt .
{ ?w dc:subject "Linked Data" }
UNION
{ ?w dc:subject "linked data" }
}
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?wt WHERE {
?w a swc:WorkshopEvent ;
dc:title ?wt .
{ ?w dc:subject "Linked Data" }
UNION
{ ?w dc:subject "linked data" }
}
Does the result for the previous query contain the USEWOD workshop? Investigate why (using SPARQL queries similar to the previous ones) and adjust the query so that the USEWOD workshop becomes part of the result.
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 10
Exercise 4 (Alternative)
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?wt WHERE {
?w a swc:WorkshopEvent ;
dc:title ?wt ;
dc:subject ?ws .
FILTER regex( ?ws, "linked data", "i" )
}
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?wt WHERE {
?w a swc:WorkshopEvent ;
dc:title ?wt ;
dc:subject ?ws .
FILTER regex( ?ws, "linked data", "i" )
}
Does the result for the previous query contain the USEWOD workshop? Investigate why (using SPARQL queries similar to the previous ones) and adjust the query so that the USEWOD workshop becomes part of the result.
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 11
Exercise 5
Each workshop took place in a particular location (property: 'swc:hasLocation'). Ask for the label (property: 'rdfs:label') of these locations; pair them with the title of the corresponding workshop.
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 12
Exercise 5
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?wt ?ll WHERE {
?w a swc:WorkshopEvent ;
dc:title ?wt ;
swc:hasLocation ?l .
?l rdfs:label ?ll .
}
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?wt ?ll WHERE {
?w a swc:WorkshopEvent ;
dc:title ?wt ;
swc:hasLocation ?l .
?l rdfs:label ?ll .
}
Each workshop took place in a particular location (property: 'swc:hasLocation'). Ask for the label (property: 'rdfs:label') of these locations; pair them with the title of the corresponding workshop.
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 13
Exercise 6
Ask for the URI of any event that took place in the same location as the USEWOD workshop; the USEWOD workshop must not be listed
(URI: 'http://data.semanticweb.org/workshop/usewod/2012')
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 14
Exercise 6
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
SELECT ?e WHERE {
<http://data.semanticweb.org/workshop/usewod/2012>
swc:hasLocation ?l .
?e swc:hasLocation ?l .
FILTER ( ?e != <http://data.semanticweb.org/workshop/usewod/2012> )
}
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
SELECT ?e WHERE {
<http://data.semanticweb.org/workshop/usewod/2012>
swc:hasLocation ?l .
?e swc:hasLocation ?l .
FILTER ( ?e != <http://data.semanticweb.org/workshop/usewod/2012> )
}
Ask for the URI of any event that took place in the same location as the USEWOD workshop; the USEWOD workshop must not be listed
(URI: 'http://data.semanticweb.org/workshop/usewod/2012')
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 15
Exercise 7
Ask for events that took place before the USEWOD workshop, in the same location as the USEWOD workshop. (Hint: events have properties 'ical:dtstart' and 'ical:dtend')
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 16
Exercise 7
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX ical: <http://www.w3.org/2002/12/cal/icaltzd#>
SELECT ?e WHERE {
<http://data.semanticweb.org/workshop/usewod/2012>
swc:hasLocation ?l ;
ical:dtstart ?dt1 .
?e swc:hasLocation ?l ;
ical:dtend ?dt2 .
FILTER (?dt2 < ?dt1)
}
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX ical: <http://www.w3.org/2002/12/cal/icaltzd#>
SELECT ?e WHERE {
<http://data.semanticweb.org/workshop/usewod/2012>
swc:hasLocation ?l ;
ical:dtstart ?dt1 .
?e swc:hasLocation ?l ;
ical:dtend ?dt2 .
FILTER (?dt2 < ?dt1)
}
Ask for events that took place before the USEWOD workshop, in the same location as the USEWOD workshop. (Hint: events have properties 'ical:dtstart' and 'ical:dtend')
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 17
Exercise 8
While all events that took place in the same location as the USEWOD workshop have a label (property: 'rdfs:label'), only some of them have a title (property: 'dc:title'). List these labels and if the corresponding event also has a title, then show this title in addition to the label. (Hint: use OPTIONAL)
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 18
Exercise 8
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
SELECT ?el ?et WHERE {
<http://data.semanticweb.org/workshop/usewod/2012>
swc:hasLocation ?l .
?e swc:hasLocation ?l ;
rdfs:label ?el .
OPTIONAL { ?e dc:title ?et }
}
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
SELECT ?el ?et WHERE {
<http://data.semanticweb.org/workshop/usewod/2012>
swc:hasLocation ?l .
?e swc:hasLocation ?l ;
rdfs:label ?el .
OPTIONAL { ?e dc:title ?et }
}
While all events that took place in the same location as the USEWOD workshop have a label (property: 'rdfs:label'), only some of them have a title (property: 'dc:title'). List these labels and if the corresponding event also has a title, then show this title in addition to the label. (Hint: use OPTIONAL)
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 19
Exercise 9
Adjust the previous query such that it only lists the labels of those events that do not have a title (and that took place in the same location as the USEWOD workshop). (Hint: use the negation by failure pattern)
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 20
Exercise 9
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
SELECT ?el ?et WHERE {
<http://data.semanticweb.org/workshop/usewod/2012> swc:hasLocation ?l .
?e swc:hasLocation ?l ;
rdfs:label ?el .
OPTIONAL { ?e dc:title ?et . }
FILTER ( ! BOUND(?et) )
}
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>PREFIX dc: <http://purl.org/dc/elements/1.1/>PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
SELECT ?el ?et WHERE {
<http://data.semanticweb.org/workshop/usewod/2012> swc:hasLocation ?l .
?e swc:hasLocation ?l ;
rdfs:label ?el .
OPTIONAL { ?e dc:title ?et . }
FILTER ( ! BOUND(?et) )
}
Adjust the previous query such that it only lists the labels of those events that do not have a title (and that took place in the same location as the USEWOD workshop). (Hint: use the negation by failure pattern)
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 21
Exercise 10
Adjust the previous query such that it lists the labels of those events that do not have a title and it lists the title of those events that have a title (and a label); in both cases use the same variable for the output, that is, the query result should consist of a single column only.
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 22
Exercise 10
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?el WHERE {
<http://data.semanticweb.org/workshop/usewod/2012> swc:hasLocation ?l . ?e swc:hasLocation ?l .
{ ?e rdfs:label ?el . OPTIONAL { ?e dc:title ?et } FILTER ( ! BOUND(?et) )
} UNION { ?e dc:title ?el }
}
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?el WHERE {
<http://data.semanticweb.org/workshop/usewod/2012> swc:hasLocation ?l . ?e swc:hasLocation ?l .
{ ?e rdfs:label ?el . OPTIONAL { ?e dc:title ?et } FILTER ( ! BOUND(?et) )
} UNION { ?e dc:title ?el }
}
Adjust the previous query such that it lists the labels of those events that do not have a title and it lists the title of those events that have a title (and a label); in both cases use the same variable for the output, that is, the query result should consist of a single column only.
Olaf Hartig - ICWE 2012 Tutorial "An Introduction to SPARQL and Queries over Linked Data" - Hands-on Exercises 23
These slides have been created byOlaf Hartig
http://olafhartig.de
This work is licensed under aCreative Commons Attribution-Share Alike 3.0 License
(http://creativecommons.org/licenses/by-sa/3.0/)