Introduction to RDF Query with SPARQL

Part 6 - Exercises

Dave Beckett

Learning Goals

After this part of the tutorial you should

Overview

A series of short tasks leading up to a larger query exercise.:

People and Birthdays

Skeleton Query

Use the following skeleton query (not complete) to setup the data you will be using:

BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>

# ...

FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>

# ...

(data.rq)

Task 1 - Find all the people

Task 1 - Write a query to find all the nodes in the RDF graph that are about a person.

Hint: the nodes about people all have an rdf:type property with the same value.

Answer 1 - Find all the people

BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?person

FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>

WHERE {
  GRAPH ?g { ?person rdf:type foaf:Person } .
}

(people.rq)

Task 1 Problems

This did not return the expected results (6 people) because:

So try to find more information

Task 2 - Write a query to find all people and their names

Hint: use the foaf:name property

Answer 2 - Find all the people and their names

BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?person ?name

FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>

WHERE {
  GRAPH ?g { ?person rdf:type foaf:Person; 
                     foaf:name ?name
           } .
}

(names.rq)

Task 2 Problems

This did not return the expected results (6 people) either because:

So try to find some more information

Task 3 - Write a query to find all people, their names and mailboxes

Hint: use the foaf:mbox property

Answer 3 - Find all people, their names and mailboxes

BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?person ?name ?mbox

FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>

WHERE {
  GRAPH ?g { ?person rdf:type foaf:Person; 
                     foaf:name ?name;  foaf:mbox ?mbox
           } .
}

(mbox.rq)

Answer 3 - Problems

Task 4 - Write a query to find all people and optionally their names and mailboxes

Answer 4 - Find all people and optional their names, mailboxes

BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?person ?name ?mbox

FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>

WHERE {
  GRAPH ?g { ?person rdf:type foaf:Person .
             OPTIONAL { ?person foaf:name ?name } .
             OPTIONAL { ?person foaf:mbox ?mbox }
           } .
}

(optional.rq)

Task 4 - Issues

The last answer showed:

Task 5 - Write a query to find all people, optionally their names and mailboxes eliminating the duplicates

Hint: DISTINCT

Answer 5 - Find distinct people with optional names, mailboxes

BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT DISTINCT ?name ?mbox

FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>

WHERE {
  GRAPH ?g { ?person rdf:type foaf:Person .
             OPTIONAL { ?person foaf:name ?name } .
             OPTIONAL { ?person foaf:mbox ?mbox }
           } .
}

(distinct.rq)

Task 5 - Issues

Task 6 - Order the results of Task 5 sorting by name of the person, in ascending order.

Answer 6 - Order the results of Task 5 sorting by name of the person, in ascending order

BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT DISTINCT ?name ?mbox

FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>

WHERE {
  GRAPH ?g { ?person rdf:type foaf:Person .
             OPTIONAL { ?person foaf:name ?name } .
             OPTIONAL { ?person foaf:mbox ?mbox }
           } .
}
ORDER BY ASC[?name]

(order.rq)

Exercise

Hints below

Exercise Hints

Use the structure of the data for birthday events to create part of the query

An OPTIONAL { ... } block can cover many patterns

Extra Credits

Note: there are no canned answers for all of these yet

Possible Exercise Answer

BASE <http://www.w3.org/2004/Talks/17Dec-sparql/data/>

PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  dc:   <http://purl.org/dc/elements/1.1/>
PREFIX  bio:  <http://purl.org/vocab/bio/0.1/>
PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>

SELECT DISTINCT ?name ?birthday

FROM NAMED <aliceFoaf.ttl>
FROM NAMED <bobFoaf.ttl>
FROM NAMED <celineFoaf.ttl>
FROM NAMED <danFoaf.ttl>
FROM NAMED <eveFoaf.ttl>

WHERE {
  GRAPH ?g {
     ?person a foaf:Person; foaf:name ?name .
     OPTIONAL { 
       ?person bio:event ?b .
       ?b a bio:Birth .
       ?b bio:date ?birthday .
     }
  }
}

ORDER BY ASC[?birthday]

(birthdays.rq)

Questions and Feedback

Questions?

Copyright

Copyright 2005 Dave Beckett, Steve Harris, Eric Prud'hommeaux and Andy Seaborne. Terms of use are given on the main Introduction to RDF Query with SPARQL Tutorial page.