Aside: If you haven’t worked with SPARQL at all, you might want to do some background reading first, but this article is pretty much self-contained as long as you don’t mind that we don’t explain all the SPARQL details.

In this tutorial, we’re going to be building and running a query to find the names of all the schools in the London Borough of Brent, but we’ll take it one step at a time. The concepts we introduce here should come in useful elsewhere as so much Linked Data is geographically based.

Our example is based around UK datasets, so apologies to readers from other countries, but hopefully the principles will transfer to similar datsets elsewhere. All the data used in this example is available at DCLG’s SPARQL endpoint, if you want to follow along or experiment further.

Getting Stuck In

Let’s start with a postcode. We’ll use HA9 9HD, which as all of you will recognise, is the home of Brent Council. :)

The first thing we need is a URI for this postcode. The definitive set of geographical information on postcodes in the UK comes from the Ordnance Survey. The OS identifier for our postcode is:

If you look up that URI in your browser, you can see what properties of it are available. We’re going to use the ‘District’ property, which has the URI:

(I found this out by simply clicking on the link for ‘District’ on the page for the postcode.)

Our first SPARQL query

The query below returns the district for the postcode HA9 9HD:

PREFIX pc: <>

SELECT ?district
  <> pc:district ?district

If you try that yourself by entering the SPARQL at OpenDataCommunities or Talis‘s SPARQL endpoint, you’ll see that the district for the postcode is identified by the URI:

Looking that URI up in your browser will confirm that it’s the London Borough of Brent.

Linking up our Linked Data

There’s another key dataset of geographical regions in the UK and that comes from These identifiers are based on codes from the UK Office of National Statistics (ONS) that are used for most government statistical information. Handily, this data is cross-referenced with the Ordnance Survey identifiers, so we can use the URI for our postcode from before to find the ONS district:

PREFIX pc: <>
PREFIX rdfs: <>
PREFIX owl: <>

SELECT ?district ?ons_district
  <> pc:district ?district .
  ?ons_district owl:sameAs ?district

Note the use of ‘owl:sameAs’ which specifies that the two URIs refer to the same real world thing.

So using the query above, we find the ONS identifier is:

(Remember, you can try all the queries out yourself here)

Now what?

Well, we can use that to link up other datasets, for example to look up all the schools in the local authority with the given postcode. To do this, we’ll use another dataset, this one covering all schools in England and Wales.

By exploring that dataset, we can see that there is a property called ‘districtAdministrative’ that relates a school to the local authority district it belongs to. We’ll use that to link the schools to the district for our postcode (and we include the label for the school to make it more readable).

PREFIX pc: <>
PREFIX rdfs: <>
PREFIX owl: <>
PREFIX school: <>

SELECT ?school ?label WHERE {
<> pc:district ?district .
?ons_district owl:sameAs ?district .
?school school:districtAdministrative ?ons_district .
?school rdfs:label ?label

This returns a list of 299 schools in the London Borough of Brent (try it by entering the query here).

The power of SPARQL

With just a few queries, we’ve done something pretty powerful, and hopefully you’ve realised that SPARQL isn’t that scary after all. We plan on writing some tutorials explaining the syntax in more detail soon.

Keep up to date with our news by signing up to our newsletter.
Thanks for reading all the way to the end!
We'd love it if you shared this article.