My Understanding of SPARQL, the First Attempt…

(This one’s for Mike…) [Disclaimer: some or all of this post may be wrong…;-)]

So (and that was for Niall)… so: here’s what I think I know about SPARQL; or at least, here’s what I think I know about SPARQL and what I think is all you need to know to get started with SPARQL

SPARQL is a query language that can interrogate an RDF triple store.

An RDF triple store contains facts as 3 parts of a whole:

<knownThing1> <hasKnownAttribute> <knownThing2>
<knownThing1> <hasKnownRelationshipWith> <knownThing3>

That’s it, part one…

Let’s imagine a very small triple store that contains the following facts:

<Socrates> <existsAs> <aMan>
<Plato> <existsAs> <aMan>
<Zeus> <existsAs> <aGod>
<aGod> <hasLife> <Immortal>
<aMan> <hasLife> <Mortal>
<Socrates> <isTeacherOf> <Plato>

We can ask the following sorts of questions of this database, using the SPARQL query language (or should that be: using the SPAR Query Language?!)

Who is <aGod>?
select ?who where {
?who <existsAs> <aGod>
}

This should return: <Zeus>

Who is <aMan>?
select ?who where {
?who <existsAs> <aMan>
}

This should return: <Socrates>, <Plato>.

What sort of existence does <Zeus> have?
select ?existence where {
<Zeus> <existsAs> ?existence.
}

This should return: <aGod>.

What sort of life does <aMan> have?
select ?life where {
<aMan> <hasLife> ?life.
}

This should return: <Mortal>.

We only know that <Zeus> exists, but what sort of life does he have?
select ?life where {
<Zeus> <existsAs> ?dummyVar.
?dummyVar <hasLife> ?life.
}

This should return: <Immortal>.

Note that the full stop in the query is important. It means “AND”. [UPDATE: LD folk have taken issue with me saying the dot represents AND. See comment below for my take on this…]

We don’t know much, other than things exist and they have some sort of life: but who has what sort of life?
select ?who ?life where {
?who <existsAs> ?dummyVar.
?dummyVar <hasLife> ?life.
}

This should return: (<Socrates> <Mortal>),(<Plato> <Mortal>),(<Zeus> <Immortal>).

We know <Socrates>, but little more: what is there to know about him?
select ?does ?what where {
<Socrates> ?does ?what.
}

This should return: (<existsAs> <aMan>),(<isTeacherOf> <Plato>).

I think… If someone wants to set up a small triple store to try this out on, that would be handy…

If I’ve gone wrong somewhere, please let me know… (I’m writing this because I canlt sleep!)

If I haven’t gone wrong – that’s a relief… and furthermore: that is all I think you need to know to get started… (Just bear in mind that in real triple stores and queries, the syntactic clutter is a nightmare and is there solely to confuse you and scare you away…;-)

PS @cgutteridge’s Searching a SPARQL Endpoint demonstrates a useful ‘get you started’ query for exploring a real datastore (look for something by name, his example being a search over the Ordnance Survey triple store for things relating to ‘Ventnor’).

Author: Tony Hirst

I'm a Senior Lecturer at The Open University, with an interest in #opendata policy and practice, as well as general web tinkering...

10 thoughts on “My Understanding of SPARQL, the First Attempt…”

  1. I like this… please keep us informed as to your progress, including the set-up for queries that actually work on a read set of triples (eg., I assume you use some software to run sparql queries, right?)

    1. I will try to blog as many steps in my journey as I can… As to running queries – the above examples were all made up. For running actual queries against arbitrary datastores/SPARQL endpoints, I tend to use sparqlproxy [ https://blog.ouseful.info/2010/09/28/data-open-ac-uk-arrives-with-linked-data-goodness/ ] or YQL [ https://blog.ouseful.info/2010/11/02/accessing-linked-data-in-scraperwiki/ ]

      I suspect most LD folk wouldn’t use these tools though…;-)

  2. [UPDATE: LD folk are taking issue with my saying the dot represents AND. My interpretation is: the things in the { } are a set of statements that define one or more fragments of a graph. The database/datastore itself defines a graph:

    SPARQL test data

    The query seeks to fill the blanks (the ?args before the /where/ in the query) with values that make the graph fragments contained between the { } true, where truth is defined by the graph in the database.

    To me, that means each triple needs to be true AND all the triples need to be true for the select query to pull out each separate result?

    So why isn’t the dot read, to all intents and purposes, as AND?]

  3. Tony, you asked:

    “So why isn’t the dot read, to all intents and purposes, as AND?”

    The AND is implicit: effectively, any patterns simply juxtaposed are ANDed. The “.” terminates a triple. So, where you see a “.”, it can generally be read as AND.

    BUT, not all implicit ANDs will show up as “.”

    e.g.

    select ?life where {
    ?dummyVar.
    ?dummyVar ?life.
    }

    cam also be written as:

    select ?life where {
    [ ?life ]
    }

    Same meaning. Same AND. But no “.” in sight.

    The syntactic role of “.” is part of the story that also includes “;” and “,”, which too, in their ways, signal a conjunction. E.g.

    select ?doc where
    { ?doc “tom”, “dick”, “harry” }

    queries for all resources ?doc such that

    ?doc “tom”
    AND
    ?doc “dick”
    AND
    ?doc “harry”

    are all to be found in the queried triple store.

  4. Damn, wordpress ate my angle brackets in the last comment.

    Try again without angle brackets…

    Tony, you asked:

    “So why isn’t the dot read, to all intents and purposes, as AND?”

    The AND is implicit: effectively, any patterns simply juxtaposed are ANDed. The “.” terminates a triple. So, where you see a “.”, it can generally be read as AND.

    BUT, not all implicit ANDs will show up as “.”

    e.g.

    select ?life where {
    zeus existsAs ?dummyVar.
    ?dummyVar hasLife ?life.
    }

    can also be written as:

    select ?life where {
    zeus existsAs [ hasLife ?life ]
    }

    Same meaning. Same AND. But no “.” in sight.

    The syntactic role of “.” is part of the story that also includes “;” and “,”, which too, in their ways, signal a conjunction. E.g.

    select ?doc where
    { ?doc hasAuthor “tom”, “dick”, “harry” }

    queries for all resources ?doc such that

    ?doc hasAuthor “tom”
    AND
    ?doc hasAuthor “dick”
    AND
    ?doc hasAuthor “harry”

    are all to be found in the queried triple store.

    1. Thanks for that Graham – the additional variant on syntax are really useful…
      @peter – thanks fot that (and yes, the WordPress handling of angle brackets in comments is a real pain. I don’t know of a workaround, either, other than using the different bracket type. Maybe a space after the bracket < sic:-(
      Part of what I'm trying to do is find a way in to writing SPARQL queries for the rest of us. I remember each year in Physics at school being told what we learned the previous year was 'wrong', and approximation, and this year we'd do the real stuff… So where you say "where you see a “.”, it can generally be read as AND", I was trying to offer a way in to explaining what the '.' was doing in the queries in as simple terms as possible, for possible who are starting out with this stuff, really donlt care about the subtleties yet because they want to get something, anything, to try out quickly…

      Admittedly, your use of the word "generally" is really powerful. I remember a book on control theory that effectively managed to completely demystify something for me that had been causing me all sorts of problems simply by saying "We shall find it convenient to…" In other words: think of this way, and you'll find it much easier for now…

Comments are closed.

%d bloggers like this: