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. <>
  • Literals are surrounded with quotes, e.g. "REEEP"


Starting with a Simple Query

A SELECT query consists of binding variables and a set of triple patterns in the WHERE-clause. Here is a simple SELECT query that selects all synonyms (=alternative labels) of a concept:

SELECT ?synonym
  <> <> ?synonym.

If you run this query against the SPARQL endpoint of reegle you will get the three alternative labels of the concept:

"PV cells"@en
"solar power cells"@en
"photovoltaic cells"@en

Let's now assume we are interested in the spanish preferred label of the concept. We can get that information by using a basic FILTER expression in our query:

SELECT ?prefLabel
  <> <> ?prefLabel.
  FILTER (lang(?prefLabel) = "es")

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

"célula solar"@es

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 SKOS namespace can be replaced using a PREFIX declaration at the beginning of the query:

PREFIX skos: <>
SELECT ?synonym
  <> skos:altLabel ?synonym.


Advanced Example Queries

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

Retrieve all top Concepts of the ConceptScheme "Ocean Power" and their preferred labels in english:

PREFIX skos: <>

SELECT DISTINCT ?topConcept ?prefLabel
  <> skos:hasTopConcept ?topConcept.
  ?topConcept skos:prefLabel ?prefLabel.
  FILTER(lang(?prefLabel) = "en")

Select all related Concepts of the Concept "solar cells" and their preferred labels:

PREFIX skos: <>

SELECT DISTINCT ?relatedConcept ?prefLabel
  <> skos:related ?relatedConcept.
  ?relatedConcept skos:prefLabel ?prefLabel.
  FILTER(lang(?prefLabel) = "en")

Returns all Concepts, their preferred labels that start with the letter "A" and the title of the parent ConceptScheme, sorting them alphabetically. A maximum of 50 concepts are returned.:

PREFIX rdfs:<>
PREFIX dc:<>
PREFIX skos:<>
SELECT DISTINCT ?concept ?prefLabel ?conceptSchemeTitle
   ?concept skos:prefLabel ?prefLabel .
   ?concept skos:broaderTransitive ?broaderTransitive.
   ?scheme skos:hasTopConcept ?broaderTransitive.
   ?scheme dc:title ?conceptSchemeTitle.
   FILTER (regex(str(?prefLabel), '^a', 'i') && lang(?conceptSchemeTitle) = "en" && lang(?prefLabel) = "en") 
ORDER BY ?prefLabel 

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
  • content-type - specifies the result format

For SELECT queries the available formats are:

  • text/xml
    (rendered as XML, but returned as text)
  • application/xml
    (rendered and returned as XML)
  • text/json
    (rendered as JSON, but returned as text)
  • application/json
    (rendered and returned as JSON)
  • html/table

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