Intro to SPARQL (Part1)

sachini ranasinghe
4 min readNov 23, 2019

Nature of a Query.

SPARQL stands for SPARQL Protocol and RDF Query Language. SPARQL is a semantic query language for databases — able to retrieve and manipulate data stored in Resource Description Framework (RDF) format. SPARQL queries are represented in triple format.

Uses of SPARQL

  • Pull values from structured and semi structured data represented in RDF.
  • Explore RDF by querying umknown relationships.
  • Perform complex joins off desperate RDF repositories in a single query.
  • Transform RDF data form one vocabulary to another.
  • Develop higher level cross platform applications.

First of all we have to see the anatomy of a query. A SPARQL query mainly contains following parts.

Anatomy of a SPARQL query
  • WHERE clause specifies which data to pull out.
  • SELECT picks what data to display.

I’m going to show how SPARQL can be used to extract data from an OWL ontology, pizza.owl created using protégé. First of all for getting results for queries involving properties (which I have discussed in protégé short note part 1), we have to create some individuals to get results for queries which returns individuals. If our ontology doesn’t have individuals created , we will end up getting empty results when executing queries.

individuals tab inside entities tab in protégé

From the above capture you can see some individuals which have created. From the selected example Example-Margherita we can see that it is a member of the class MargheritaPizza and it has a property hasCalorificContentValue whose value is set to 263.

For running SPARQL queries in protégé we have to open the SPARQL Query tab if it cannot be seen by default.

open SPARQL Query tab

In the initial view we can see a query and it is executed correctly.

Simply this query returns sub classes of all classes. As a example we know CheeseTopping is a sub class of PizzaTopping. So, from the query for variable ?subject we get the sub classes of super classes which are shown under the variable ?object. We can identify the anonymous classes which are displayed with a yellow dot. So you can see the triple structure in the query very clearly and you can see the query anatomy can also be clearly identified. And remember, this query is not changed by me with a single letter.

Next we will see an example query involving a property, we have defined in the ontology.

You can see this query have small difference than the first one. I have added a new prefix. This is added in order to refer the properties and classes we have defined in our own ontology. As you can see, for the URL I have provided the IRI of my ontology. The term pizza is arbitrary. So, by executing the query we can see it returns the individuals which have a value for the property hasCalorificContentValue. The query simply says “select all the individuals(not classes) who have property hasCalorificContentValue, show their name(?subject) and the value(?value).

Using the rdf, rdfs and owl namespaces

Using these namespaces are important in executing complex SPARQL queries. A simple example is shown as an introduction for using these namspaces.

The above example query shows the use of ,

rdfs:subClassOf — to declare a class is a specialization of another class. owl:someValuesFrom — to specify an existentional quantification.

This query is used to find the classes (?Y) which are specialization(subclass) of class (?X) where (?X) is class “hasTopping some MushroomTopping” which is created using an existentional quantification.

In simple terms this query finds all the classes of pizza which has at least one topping which is a MushroomTopping.

Furthur information about these namespaces can be obtained by,

RDF Schema — https://www.w3.org/TR/rdf-schema/

OWL — https://www.w3.org/TR/owl-ref/

From the next part I’ll discuss how to use the keywords OPTIONAL, DISTINCT, LIMIT, OFFSET and ORDER BY.

--

--