Sample Sparql Queries

At some point I’m intending to write a little series of tutorials for Sparql which, coupled with Twinkle, will hopefully prove useful to people getting to grips with the language.
For the moment though I thought I’d post a few sample queries to get people started. The examples include some that refer to my own FOAF description, and a few from rdfdata.org. All of these can be simply cut-and-pasted into the Twinkle query box to run them.
Caveat: the specification isn’t finished, so syntax may change in the future. I’ll try and keep these current though.


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)
[(?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)

List the name, mbox of all contributors to the 2003 Dublin Core conference, along with the title of the paper the (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.
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)

One thought on “Sample Sparql Queries

  1. The Room says:

    links for 2005-04-07

    O’Reilly: Take My Advice: Don’t Learn XML (tags: publishing tutorials webdev writing xml) Lost Boy: Sample Sparql Queries (tags:…

Comments are closed.

Follow

Get every new post delivered to your Inbox.

Join 29 other followers

%d bloggers like this: