Monday, 27 July 2026

SPARQL DISTINCT: Eliminating Duplicate Results from Query Results

  

When writing SPARQL queries, it is common to encounter duplicate results. This is not a bug or a problem with SPARQL—it is simply how graph pattern matching works.

 

A query returns one result for every matching pattern in the graph. If multiple triples match the same value, that value may appear multiple times in the result set.

 

In such situations, the DISTINCT keyword helps us eliminate duplicate values and return only unique results.

 

Let's see how this works using our Marvel RDF dataset.

 

Example 1: Which villains have Marvel teams fought against?

 

Query without distinct

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

SELECT ?villainName
WHERE {
    ?team :foughtAgainst ?villain .
    ?villain :name ?villainName .
}

Output

villainName
Loki
Thanos
Thanos

Notice that Thanos appears twice because two teams fought him.

 

Query With DISTINCT

 

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

SELECT DISTINCT ?villainName
WHERE {
    ?team :foughtAgainst ?villain .
    ?villain :name ?villainName .
}

   

Output

 

villainName
Loki
Thanos

This is a very natural use case for DISTINCT.

 

Example 2: How many unique villains have been fought?

 

Without DISTINCT:

 

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

SELECT (COUNT(?villain) AS ?count)
WHERE {
    ?team :foughtAgainst ?villain .
}

Output

count
3

   

But there are only two unique villains. Let's use DISTINCT

 

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

SELECT (COUNT(DISTINCT ?villain) AS ?count)
WHERE {
    ?team :foughtAgainst ?villain .
}

   

Output

 

count
2

   

This example perfectly demonstrates:

 

·      Why duplicates occur

·      Difference between COUNT and COUNT(DISTINCT)

·      A realistic business question

 

Example 3: Which movies feature Marvel characters?

 

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

SELECT ?title
WHERE {
    ?character :appearsIn ?movie .
    ?movie :title ?title .
}

   

Output

 

title
Avengers: Endgame
Avengers: Endgame
Avengers: Endgame
Avengers: Infinity War
Avengers: Infinity War
Avengers: Infinity War

Because Tony Stark, Steve Rogers, and Thanos all appear in those movies.

 

Using:

 

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

SELECT DISTINCT ?title
WHERE {
    ?character :appearsIn ?movie .
    ?movie :title ?title .
}

   

Output

title
Avengers: Endgame
Avengers: Infinity War

In summary, Duplicate results are a natural consequence of SPARQL's graph pattern matching model. SPARQL returns one row for every successful match, which can cause the same value to appear multiple times.

 

Whenever you need unique values, use DISTINCT. When counting unique values, use COUNT(DISTINCT ?variable) rather than COUNT(?variable).

 

In practice, DISTINCT is one of the most frequently used SPARQL keywords and is often worth considering whenever duplicate results are possible.

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment