Are you a web or application developer?

Introduction to SPARQL

Back to the guide

SPARQL (SPARQL Protocol And RDF Query Language) is the standardized query language for RDF.

The syntax is actually quite simple if you understand the underlying data model RDF which consists of triples.

  • Most forms of SPARQL query contain a set of triple patterns (source)
  • Triple patterns are like RDF triples except that each of the subject, predicate and object may be a variable. (source)
  • URIs are surrounded with pointy brackets, e.g. <http://reegle.info/actors/2354>
  • Literals are surrounded with quotes, e.g. "REEEP"

 

Starting with a Simple Query

A SELECT query e.g. consists of binding variables and a set of triple patterns in the WHERE-clause. Here is a simple SELECT query that selects the names of an actor:

SELECT ?actorName
WHERE {
  <http://reegle.info/actors/2354> <http://xmlns.com/foaf/0.1/name> ?actorName.
}

In our data set the triple pattern from above will match two triples:

Subject Predicate Object
http://reegle.info/actors/2354 http://xmlns.com/foaf/0.1/name "Renewable Energy and Energy Efficiency Partnership"@en
http://reegle.info/actors/2354 http://xmlns.com/foaf/0.1/name "Verein für erneuerbare Energie und Energieeffizienz"

If you run this query against the SPARQL endpoint of reegle you will get two results - namely: the english and local name of the actor with the URI http://reegle.info/actors/2354.

actorName
Renewable Energy and Energy Efficiency Partnership
Verein für erneuerbare Energie und Energieeffizienz

Let's now assume we are only interested in the english name of the actor. We can get that information by including a basic FILTER expression in our query:

SELECT ?actorName
WHERE {
  <http://reegle.info/actors/2354> <http://xmlns.com/foaf/0.1/name> ?actorName.
  FILTER (lang(?actorName) = "en")
}

This will filter out all labels that have no language tag "en" attached to them and leave you just with the english name:

actorName
Renewable Energy and Energy Efficiency Partnership

Prefixes and Namespaces

To strip the redundant namespace information from URIs in your SPARQL query you can use prefixes as in most of RDF serialization formats

So in the simple query from above the foaf namespace can be replaced using a PREFIX declaration at the beginning of the query:

PREFIX foaf:<http://xmlns.com/foaf/0.1/>
SELECT ?actorName
WHERE {
  <http://reegle.info/actors/2354> foaf:name ?actorName.
}

Advanced Example Queries

Below are a couple of more advanced example queries you can try using the reegle SPARQL endpoint.

Retrieve all categories and their labels from the data set:

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

SELECT DISTINCT ?category ?categoryLabel
WHERE {
  ?category a skos:Concept.
  ?category skos:prefLabel ?categoryLabel.
}

Select all actors that are active in portugal and in the category "Research/University":

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX reegle: <http://reegle.info/schema#>

SELECT ?actor ?name 
WHERE {
  ?actor a foaf:Organization.
  ?actor dcterms:subject <http://reegle.info/categories/12>.
  ?actor foaf:name ?name. FILTER (lang(?name) = "en" )
  ?actor reegle:activeIn <http://reegle.info/countries/PT>.
}

Retrieve all specialisations and their titles:

PREFIX reegle: <http://reegle.info/schema#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>

SELECT DISTINCT ?specialisation ?specTitle
WHERE {
  ?specialisation a reegle:Specialisation.
  ?specialisation dc:title ?specTitle.
}

Select the number of project reports for the specialisation "Policy and Regulation" ordered by country:

PREFIX dbprop: <http://dbpedia.org/property/>
PREFIX reegle: <http://reegle.info/schema#>
PREFIX geonames: <http://www.geonames.org/ontology#>

SELECT ?countryName count(?country) as ?count 
WHERE {
  ?project a reegle:ProjectOutput.
  ?project reegle:specialisation <http://reegle.info/specialisations/34>.
  ?project dbprop:country ?country.
  ?country geonames:name ?countryName.
} 
ORDER BY DESC(?count)

 

Note: The underlying SPARQL endpoint is provided by OpenLink Virtuoso, therefore you can use things like COUNT in your SPARQL queries even though it is not supported in the current official SPARQL specification.

SPARQL Endpoint as Web Service

You can use the SPARQL endpoint as a web service simply by sending a HTTP POST request to the endpoint URL and provide the following parameters (URL-encoded):

  • query - the SPARQL query string
  • format - specifies the result format

For SELECT queries the available formats are:

  • application/sparql-results+xml
    XML serialisation
  • application/sparql-results+json
    JSON serialisation
  • text/html
    HTML table
  • application/javascript
    Javascript
  • text/plain
    NTriples
  • application/rdf+xml
    RDF/XML
  • application/vnd.ms-excel
    Excel Spreadsheet

For CONSTRUCT and DESCRIBE queries (returning triples) the available formats are:

  • text/rdf+n3
    Notation 3
  • application/rdf+xml
    RDF/XML
  • text/plain
    NTriples
  • application/xhtml+xml
    HTML + RDFa
  • application/rdf+json
    RDF as JSON