Want to keep learning?

This content is taken from the University of Southampton's online course, Introduction to Linked Data and the Semantic Web. Join the course to learn more.

Limits and filters

Sometimes, you don’t want all the results of a query at once, and at others you want results when any variable satisfies a certain conditions - e.g. albums beginning with the letter ‘B’.

Returning results page by page

For some queries, including the previous example, the output table returns too many result at once. In such cases it would be useful if the music portal included a paging facility allowing users to view the information in manageable portions – perhaps ten rows at a time. This can be done through the query engine if you use the keywords LIMIT and OFFSET. To see how this works, try extending our previous query as follows:

PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX mo: <http://purl.org/ontology/mo/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>

WHERE { dbpedia:The_Beatles foaf:made ?album .
        ?album a mo:SignalGroup .
        ?album dc:title ?title
ORDER BY ?title

You should get back the same table, but cut off after the first ten rows; the result will be the same if you just put LIMIT 10 leaving the offset unspecified.

Now try raising the number after OFFSET to 10. You should get back the next ten-row segment of the table, covering rows 11-20.

In general, if LIMIT is L and OFFSET is S, the query will return L rows starting at S+1 and continuing up to S+L.

Watch this 4min 10s a video screencast walkthrough of how to approach this activity.

Using tests to filter the results

We have seen queries in which the WHERE clause contains specific resources (the Beatles) or variables (?album). But what if we want to obtain results for any variable that satisfies a certain condition – e.g., albums beginning with the letter ‘B’, or band members born before 1960?

Such conditions are called ‘filters’, and to illustrate them, let us switch to an example in which our aim is to retrieve tracks (not albums) with a duration between 300 and 400 seconds.

Since in MusicBrainz durations are encoded in milliseconds, the relevant filter condition can be stated as follows:

PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX mo: <http://purl.org/ontology/mo/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>

SELECT ?title ?duration
WHERE { dbpedia:The_Beatles foaf:made ?track .
        ?track a mo:Track .
        ?track dc:title ?title .
        ?track mo:duration ?duration .
        FILTER (?duration>=300000 && ?duration<=400000)
ORDER BY ?duration

The W3C specification at http://www.w3.org/TR/rdf-sparql-query/ contains other operators from which filter conditions can be constructed. This specification can be consulted for further details.

Conceptually, filters define a boolean condition on a graph pattern binding. We have already seen that the graph pattern in a WHERE clause has a set of solutions, each corresponding to a binding of the variables mentioned in the graph pattern.

The filter submits these solutions to a boolean condition, letting through only those variable bindings for which the condition is met. In a naive implementation, the projection would be computed in just this way: first find the set of solutions matching the graph pattern; then select from this set the solutions that satisfy the filter condition, for inclusion in the final result.

In the next step you will be able to practice avoiding duplicates in the results, using the DISTINCT keyword.

This work is a derivative of ‘Using Linked Data Effectively’ by The Open University (2014) and licensed under CC by 4.0 International Licence adapted and used by the University of Southampton. http://www.euclid-project.eu/

Share this article:

This article is from the free online course:

Introduction to Linked Data and the Semantic Web

University of Southampton

Get a taste of this course

Find out what this course is like by previewing some of the course steps before you join: