Monday, 27 July 2026

SPARQL Aggregation Functions: Finding Earliest and Latest Movie Releases with MIN and MAX

  

In the previous post, we explored the COUNT aggregation function and saw how SPARQL can help us answer questions such as "How many Avengers are there?" or "How many movies exist in our graph?"

 

However, counting is just one type of aggregation. Often, we need to analyze numerical or date based data and answer questions like:

 

·      What is the earliest movie in our dataset?

·      What is the latest movie released?

·      What is the highest score?

·      What is the lowest value?

 

For these kinds of questions, SPARQL provides the MIN and MAX aggregation functions.

 

Let's see how they work using our Marvel RDF dataset.

 

1. The Question

Suppose we want to answer the following question "What are the earliest and latest movie release years recorded in our Marvel graph?".

 

Looking at our data, we have two movies:

 

Movie Release Year

·      Avengers: Infinity War  2018

·      Avengers: Endgame 2019

 

Instead of manually inspecting the data, we can ask SPARQL to calculate the minimum and maximum release years for us.

 

2. Query

PREFIX : <http://example.org/marvel/>

SELECT
    (MIN(?year) AS ?earliestRelease)
    (MAX(?year) AS ?latestRelease)
WHERE {
    ?movie a :Movie ;
           :releasedIn ?year .
}

   

Output

 

earliestRelease latestRelease
2018  2019

   

3. Understanding the Query

Step 1: Find all movies and their release years

 

?movie a :Movie ;
       :releasedIn ?year .

   

This pattern matches every movie in the graph and binds its release year to the variable ?year. Conceptually, SPARQL first produces:

 

| movie            | year |
| ---------------- | ---- |
| Avengers_Endgame | 2019 |
| Infinity_War     | 2018 |

   

Step 2: Find the smallest year

MIN(?year) examines all values of ?year and returns the smallest one.

 

Step 3: Find the largest year

MAX(?year) examines the same values and returns the largest one.

 

4. Why Are MIN and MAX Useful?

These functions are commonly used for:

 

·      Finding the earliest movie release date

·      Finding the latest movie release date

·      Finding the youngest or oldest character

·      Finding minimum and maximum scores

·      Identifying the first and most recent events in a dataset

 

They are especially valuable when working with large knowledge graphs where manually inspecting the data is impractical.

 

In summary, MIN and MAX are aggregation functions that allow SPARQL to compute summary statistics over a set of values. "MIN(?variable)" returns the smallest value, and MAX(?variable) returns the largest value. Combined together, they provide a quick way to identify the range of values present in your RDF graph without returning every matching record.

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment