Sample Sparql Queries, Updated

Back in February I posted some sample sparql queries that might be useful as additional examples of the SPARQL syntax. Since then we’ve had several new drafts including some syntax changes. In this post I’m including updated versions of all queries. Except for one that is, see later discussion). I’ve also thrown in a few more for good measure, and some notes on other things that I can’t find a way to do (so thats where you can help dear reader…).
Oh, and despite my initial grumblings I’ve not found the tweaked syntax too troublesome.


List all of the names, weblogs and encrypted emails of people defined in http://www.ldodds.com/ldodds-knows.rdf

PREFIX foaf: <http://xmlns.com/foaf/0.1/&gt;
SELECT ?name ?weblog ?sha1
FROM <http://www.ldodds.com/ldodds-knows.rdf&gt;
WHERE
{
?x foaf:name ?name.
?x foaf:weblog ?weblog.
?x foaf:mbox_sha1sum ?sha1
}

That doesn’t list everyone in the document though, as not all people in my “knows” description have weblogs. We can amend the query to become the equivalent of an SQL OUTER JOIN to retrieve everyone with a name, encrypted email, but optionally a foaf:weblog property:

PREFIX foaf: <http://xmlns.com/foaf/0.1/&gt;
SELECT ?name ?weblog ?sha1
FROM <http://www.ldodds.com/ldodds-knows.rdf&gt;
WHERE
{
?x foaf:name ?name.
OPTIONAL {?x foaf:weblog ?weblog.}
?x foaf:mbox_sha1sum ?sha1.
}

‘List the titles and publication dates of documents written by someone with the name “Leigh Dodds”

PREFIX dc: <http://purl.org/dc/elements/1.1/&gt;
PREFIX foaf: <http://xmlns.com/foaf/0.1/&gt;
SELECT ?title ?date
FROM <http://www.ldodds.com/ldodds-documents.rdf&gt;
WHERE
{
?x dc:title ?title.
?x dc:created ?date.
?x foaf:maker ?me.
?me foaf:name "Leigh Dodds".
}

List the state and city for all Australian airports

PREFIX air: <http://www.daml.ri.cmu.edu/ont/AirportCodes.daml#&gt;
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
SELECT ?state ?city
FROM <http://www.daml.ri.cmu.edu/ont/AirportCodes.daml&gt;
WHERE
{
?x air:country "Australia".
?x air:city ?city.
?x air:state ?state.
}

Find all the European princesses and the date they got married

PREFIX ged: <http://www.daml.org/2001/01/gedcom/gedcom#&gt;
SELECT ?name ?marriedOn
FROM <http://www.daml.org/2001/01/gedcom/royal92.daml&gt;
WHERE
{
?royal ged:title "Princess".
?royal ged:name ?name.
?royal ged:spouseIn ?family.
?family ged:marriage ?marriage.
?marriage ged:date ?marriedOn.
}

List the names, symbols, atomic weights and numbers of all the Noble Gases

PREFIX table: <http://www.daml.org/2003/01/periodictable/PeriodicTable#&gt;
SELECT ?name ?symbol ?weight ?number
FROM <http://www.daml.org/2003/01/periodictable/PeriodicTable.owl&gt;
WHERE
{ ?element table:group ?group.
?group table:name "Noble gas".
?element table:name ?name.
?element table:symbol ?symbol.
?element table:atomicWeight ?weight.
?element table:atomicNumber ?number. }

New: Now that SPARQL has an ORDER BY clause, we can improve this query to return the Noble Gases in order of their atomic number:

PREFIX table: <http://www.daml.org/2003/01/periodictable/PeriodicTable#&gt;
SELECT ?name ?symbol ?weight ?number
FROM <http://www.daml.org/2003/01/periodictable/PeriodicTable.owl&gt;
WHERE
{ ?element table:group ?group.
?group table:name "Noble gas".
?element table:name ?name.
?element table:symbol ?symbol.
?element table:atomicWeight ?weight.
?element table:atomicNumber ?number. }
ORDER BY ASC[?number]

The query that I’ve not been able to rewrite is the following:
List the name, mbox of all contributors to the 2003 Dublin Core conference, along with the title of the paper they (co-)authored

PREFIX foaf: <http://xmlns.com/foaf/0.1/&gt;
PREFIX dc: <http://purl.org/dc/elements/1.1/&gt;
SELECT ?name ?mbox ?title
FROM <http://www.siderean.com/dc2003/dc2003_presentations.rdf&gt;
FROM <http://www.siderean.com/dc2003/dc2003_agents.rdf&gt;
WHERE
{?person foaf:publication ?doc.
?doc dc:title ?title.
?person foaf:mbox ?mbox.
?person foaf:name ?name.}

Notice that this one queries two sources, relying on the RDF graph to be automatically merged. There’s currently an open issue in the RDF DAWG that relates to this kind of “union query” see fromUnionQuery. Earlier SPARQL specifications allowed multiple FROM and FROM NAMED clauses, while the current draft only allows a single FROM clause, but multiple NAMED. It may be possible to recast that query using FROM NAMED but I can’t get my head round it yet.
While its likely that in most cases SPARQL queries are going to be run against a pre-defined data set, the ability to do ad hoc aggregation of sources like this is very useful IMO. So I’m hoping that the issue will be resolved in favour allowing union queries.
New: Here are some new queries that demonstrate a few other SPARQL features. For example the next one demonstrates the SPARQL FILTER clause, making use of the RDF index that I’ve added to my blog.
List all documents from http://www.ldodds.com/blog/blog-scutterplan.rdf in order of publication date, that have been published in 2005 and that mention SPARQL in the title.

PREFIX dc: <http://purl.org/dc/elements/1.1/&gt;
PREFIX foaf: <http://xmlns.com/foaf/0.1/&gt;
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#&gt;
SELECT ?title ?date
FROM <http://www.ldodds.com/blog/blog-scutterplan.rdf&gt;
WHERE
{
?doc dc:title ?title.
?doc dc:created ?date.
FILTER ?date > xsd:dateTime("2005-01-01T00:00:00Z").
FILTER REGEX(?title, "sparql", "i").
}
ORDER BY ASC[?date]

List all the name, weight and atomic number of all elements in the periodic table that are heavier than uranium

PREFIX table: <http://www.daml.org/2003/01/periodictable/PeriodicTable#&gt;
SELECT ?name ?symbol ?weight ?number
FROM <http://www.daml.org/2003/01/periodictable/PeriodicTable.owl&gt;
WHERE
{
?uranium table:name "uranium".
?uranium table:atomicWeight ?uraniumWeight.
?element table:name ?name.
?element table:symbol ?symbol.
?element table:atomicWeight ?weight.
?element table:atomicNumber ?number.
FILTER ?weight > ?uraniumWeight.
}
ORDER BY ASC[?weight]

List all Egyptian pharaohs of the 13th Dynasty, in name order

PREFIX p: <http://www.historical-id.info/ns/person/1.0#&gt;
SELECT ?name ?born
FROM <http://www.historical-id.info/files/egypt-pharaohs.rdf&gt;
WHERE
{?pharaoh p:first-name ?name.
?pharaoh p:description ?desc.
?pharaoh p:birth-date ?born.
FILTER REGEX(?desc, "13th Dynasty.", "i").
}
ORDER BY ASC[?name]

Now, at this point I was fishing about in some of the other historical metadata I found linked from rdfdata.org and found this list of UK Kings. You’ll notice that the descriptons include the dates of their reigns.
Ideally this would be separately marked up, but in theory a bit of string manipulation could extract it. This would be useful in a SELECT expression. And using a CONSTRUCT query one could scrape out a few more useful triples. However, I couldn’t find a way to do this. The grammar doesn’t seem to allow this and some brief experiments confirmed it. Hopefully I’ve missed something obvious.
Update: No I’m not missing anything. According to dajobe SPARQL doesn’t have any assignment and so there’s no way to assign values to variables. So the answer in this case is that if I want the dates of those reigns I’ll have to fix up the data in some way. And so, in general, if you want to return a literal value from a SPARQL query, it needs to be explicit in the data source. It’s either that, or you have to pre/post-process the data to extract what you want.
In the above example I could improve the data by publishing additional triples that can be used to annotate the original. But then I’d need a union query to merge them!