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.

Constructing new triples using aggregate data

We have already discussed a SELECT query that returns aggregate data by summing the durations of tracks in each album.

You may recall that such a query uses the AS keyword in the expression following SELECT, to introduce a variable name for the aggregate value – in this case, the album duration. In the context of a CONSTRUCT query we therefore have a problem: how to introduce this new variable for the aggregate?

The solution used in SPARQL is to allow a sub-query after the keyword WHERE, in place of the usual graph pattern. This is achieved by the following rather convoluted syntax:

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

CONSTRUCT { ?album mo:duration ?album_duration }
SELECT ?album (SUM(?track_duration) AS ?album_duration)
{ dbpedia:The_Beatles foaf:made ?album .
  ?album mo:record ?record .
  ?record mo:track ?track .
  ?track mo:duration ?track_duration . 
GROUP BY ?album 
HAVING (?album_duration > 3600000) 

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.