Contact FutureLearn for Support
Skip main navigation
We use cookies to give you a better experience, if that’s ok you can close this message and carry on browsing. For more info read our cookies policy.
We use cookies to give you a better experience. Carry on browsing if you're happy with this, or read our cookies policy for more information.

Ordering query results

For some queries you might want the results to be presented in a particular order. For instance, if your music portal retrieves albums made by the Beatles, using the query given previously, you might want to present these in alphabetical order of title.

This can be done using the keywords ORDER BY, as in the following example:

PREFIX dbpedia: <>
PREFIX foaf: <>
PREFIX mo: <>
PREFIX dc: <>

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

The key element in this query is the ORDER BY component at the end, which stipulates that rows should be presented in alphabetical order of the values in the ?title column.

Note also one other change from the previous example, the use of the asterisk shorthand in the SELECT clause, which asks for all variables in the WHERE clause to be tabulated. This shorthand is not always used, since often the selected variables are a strict subset of those mentioned in the WHERE clause.

Try replacing ORDER BY ?title with ORDER BY DESC(?title), which will present the rows in descending rather than ascending order of title (i.e., ascending is the default).

What exactly do these tables show? The columns, as we have seen, correspond to variables in the query for which tabulation is requested after SELECT, either explicitly by name, or implicitly by the ‘*’ option.

The rows give all variable bindings that match the graph pattern in the WHERE clause. A binding is an assignment of identifiers or literals to the variables which, when instantiated in these patterns, will yield a subgraph of the dataset.

Note that when computing these bindings, the query engine makes the key assumption that variables occurring in more than one pattern are bound to the same resource. In this way it avoids returning a row in which an album is paired with the title of a different album.

We can now get our results ordered by a particular variable. But what if – for example – we only wanted to retrieve a small number of results at a time, or only wanted to know about albums beginning with the letter ‘B’?

Share this article:

This article is from the free online course:

Introduction to Linked Data and the Semantic Web

University of Southampton

Course highlights Get a taste of this course before you join:

  • Welcome to the course
    Welcome to the course

    Watch Dr Elena Simperl & Dr Barry Norton explain how this short course on linked data & the semantic web can help you use this technology in your work

  • Developing real world applications
    Developing real world applications

    Watch Dr Barry Norton describing some real world applications that have Linked Data as their underlying technology.

  • Welcome to Week 2
    Welcome to Week 2

    Watch Dr Barry Norton explain what you will learn about SPARQL (the query language) on this course and what you will be practicing.