An Introduction to SPARQL Queries

What is SPARQL?

SPARQL (SPARQL Protocol and RDF Query Language, pronounced “sparkle”) is a query language used to retrieve and manipulate linked data stored in an RDF format. In this format, data are stored as triples. Triples are formed by a subject, predicate, and object. Together they form an assertion that the subject has a particular property or relationship, indicated by the predicate, to the object.

Components of a Query

The best way to get started is to write a simple query.

Example: This query will return the first ten triples in the dataset:

 Select ?subject ?predicate ?object WHERE {
  ?subject ?predicate ?object .
}
LIMIT 10

Let's break down this query further.

?:

Any word following a question mark is a variable. In this query subject, predicate, and object are names of variables. These variable names can be changed so long as they are from the ontology of the database you are querying.

SELECT:

The variables following SELECT determine what will be displayed in the table of results. For this query the table of results will have a column each of subjects, predicates, and objects. Other keywords such as CONSTRUCT, ASK, and DESCRIBE can be used to display other results.

WHERE:

The WHERE clause is mandatory. Within the WHERE clause are triples that specify the conditions the triples in the dataset must meet to be included in the results. In this query, we are searching the dataset for triples that have subject, predicate, and object.

LIMIT:

The keyword LIMIT is an example of a query modifier. This keyword will restrict the results so that only the first 10 are displayed. There are many other keywords and commands that will modify query results, which are explored in more depth in Query Modifiers.

Prefixes and URIs

Each part of a triple (subject, predicate, object) is identified by a Uniform Resource Identifier (URI).

For example, the gender property "woman" in the CWRC Ontology is represented by the following URI: <http://sparql.cwrc.ca/ontologies/cwrc#woman>

URIs in queries are often shortened using a prefix, or namespace, which is defined at the beginning of the query. These stand in for most of the URI, leaving the variable at the end which designates the terms from that ontology or vocabulary.

Defining a prefix at the beginning of a query uses the keyword PREFIX, the namespace, and the URI of the ontology. For example:

PREFIX cwrc: <http://sparql.cwrc.ca/ontologies/cwrc#>

Thus the same term above could be represented as: cwrc:woman

A Simple Query

PREFIX cwrc: <http://sparql.cwrc.ca/ontologies/cwrc#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?name WHERE {
    ?person cwrc:hasGender cwrc:woman .
    ?person cwrc:hasOccupation cwrc:teacher .
    ?person skos:altLabel ?name
}

This query searches for all of the triples in the CWRC dataset that satisfy the following requirements: the person is a woman and a teacher. We can break down the pieces of this query.

The namespaces used here are cwrc and skos:

PREFIX cwrc: <http://sparql.cwrc.ca/ontologies/cwrc#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

The results will be displayed in a table that has one column, name:

SELECT ?name

The WHERE clause is searching for a person who is a woman and a teacher:

WHERE {
  ?person cwrc:hasGender cwrc:woman .
  ?person cwrc:hasOccupation cwrc:teacher .
  ?person skos:altLabel ?name
}

In plain English, it could be translated as follows:

There is a person who has the gender woman

?person cwrc:hasGender cwrc:woman .

That same person has an occupation of teacher

?person cwrc:hasOccupation cwrc:teacher .

That person has a name represented by an alt label

?person skos:altLabel ?name

Notes on Style

Every triple in a query should end in a period.

If a subject used in one line is repeated in the next line it can be omitted as long as there is a semicolon at the end to indicate use of the same subject.

Example:

PREFIX cwrc: <http://sparql.cwrc.ca/ontologies/cwrc#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?name WHERE {
    ?person cwrc:hasGender cwrc:woman;
            cwrc:hasOccupation cwrc:teacher;
            skos:altLabel ?name.
}