Intro to SPARQL (Part 2)

sachini ranasinghe
4 min readDec 27, 2019

--

OPTIONAL, DISTINCT, LIMIT, OFFSET and ORDERBY keywords.

Hi all, this is the second part of my SPARQL introduction tutorial. Here I’m going to emphasize on some commonly used keywords in SPARQL. If you are familiar with MYSQL queries you might come across these keywords. Their usage may be exactly same but, use of syntax is bit different due to the triple format of SPARQL queries. I myself come across great difficulties in finding suitable examples for the pizza.owl ontology as other namespaces also can be used in creating these queries. As this is an introduction tutorial I tried to simplify the queries as much as possible for the sake of understanding.

As these queries need a good structure for presenting clear outputs I used a completed pizza.owl ontology which is available online. It is available at https://protege.stanford.edu/ontologies/pizza/pizza.owl

DISTINCT and OPTIONAL keywords.

Necessarily, DISTINCT keyword is used in SPARQL to eliminate the duplicates same as in MYSQL. OPTIONAL keyword is used to query data that may or may not be exist in the results. Let me explain with examples.

query without DISTINCT keyword

As you can see this query doesn’t contain any DISTINCT keyword and from the results we can see same property is repeated several times. This query intends to get the property names and comment in string format of object properties which have a comment specified for them. As the object properties are used in several instances in the ontology, they are repeated accordingly. If we want to eliminate the duplicates we can use distinct keyword as shown below. (Here some string operations STR and AS are used.They will be covered eventually in the next parts. What you have to know is, it is done to convert the RDF format of the comment to natural string format.)

query with DISTINCT keyword

As you can see lot of jargon has removed. Actually there were only 5 object properties with comments exist, Interesting…

Next one is OPTIONAL. As I mentioned before it is used for representing data that is exist or may not exist. Just compare with the one we used DISTINCT earlier.

query with OPTIONAL

Three new object properties were there!!! But, they don’t have comments for them. That’s what OPTIONAL do. It returns all the answers which may or may not exist answers to the query/s inside the optional clause. which means it’s NULL values are also allowed.

LIMIT , OFFSET and ORDER BY keywords.

Usage is very similar to that of MYSQL queries. LIMIT keyword is used to get a specific number of results. OFFSET keyword allows to skip certain number of results and show the result starting from the next value. Finally ORDER BY is used to order the results according to a specified order of certain field.

As a simple example for LIMIT

query with LIMIT

As you can see this is the same example without DISTINCT. If you remember we got a really long list of results for that. Here by using the LIMIT keyword result set is limited to only 10 results.

Similarly OFFSET is used to skip specified amount of results.

query with OFFSET

If you compare with the previous result you can see that, 7 results of hasIngredient class have been returned. Then, when we use OFFSET 6 , 6 of those results have been skipped and returned starting from the 7th result.

ORDER BY keyword is use to order the returned results in a specified order (Ascending/Descending) of a certain field. The default order is ascending and if we want to order in descending order we have to specify explicitly.

query with ORDER BY

This query is ordered by the ?value of the datatype property hasCalorificContentValue you can see the output is ordered in ascending order of the integer values.

Same example when ordered in descending value..

ordered in descending order

Up to now we have ordered by the integer value, If we order a string using ORDER BY it consider the alphabetical order of the ?value

ordered in alphabetical order

Hope this article helped to enhance your knowledge. In the next part of I’ll pay attention to the FILTER, NOTEXISTS, MINUS and UNION keywords.

--

--

No responses yet