Monday, 27 July 2026

Finding Heroes Who Appeared in Multiple Movies Using FILTER and DISTINCT

  

There are some pretty interesting questions we can ask our RDF graph. For example, suppose we want to find characters who appeared in multiple movies.

 

But what exactly does "appeared in multiple movies" mean?

 

In graph terms, it means that a single character has more than one :appearsIn relationship pointing to different movie resources.

 

Let's write a query to find them. We are using same avengers example for the demo.

 

Example 1: Find characters that appear in at least two different movies, and show me every pair of those movies.

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

SELECT ?character ?movie1 ?movie2
WHERE { 
  ?character :appearsIn ?movie1 .
  ?character :appearsIn ?movie2 .

  FILTER (?movie1 != ?movie2)
}

   

Output

 

character movie1  movie2
<http://example.org/marvel/Steve_Rogers>  <http://example.org/marvel/Infinity_War>  <http://example.org/marvel/Avengers_Endgame>
<http://example.org/marvel/Steve_Rogers>  <http://example.org/marvel/Avengers_Endgame>  <http://example.org/marvel/Infinity_War>
<http://example.org/marvel/Thanos>  <http://example.org/marvel/Infinity_War>  <http://example.org/marvel/Avengers_Endgame>
<http://example.org/marvel/Thanos>  <http://example.org/marvel/Avengers_Endgame>  <http://example.org/marvel/Infinity_War>
<http://example.org/marvel/Tony_Stark>  <http://example.org/marvel/Infinity_War>  <http://example.org/marvel/Avengers_Endgame>
<http://example.org/marvel/Tony_Stark>  <http://example.org/marvel/Avengers_Endgame>  http://example.org/marvel/Infinity_War

   

?character :appearsIn ?movie1 .

This finds all the character movie relationships.

 

?character :appearsIn ?movie2 .

Same like above

 

FILTER (?movie1 != ?movie2)

Remove rows where the two movies are the same.

 

But as you observe the output, the characters Steve_Rogers, Thanos, Tony_Stark appeared twice. We can address this by changing the filter condition like below.

 

FILTER (STR(?movie1) < STR(?movie2))

 

Updated Query

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

SELECT ?character ?movie1 ?movie2
WHERE { 
  ?character :appearsIn ?movie1 .
  ?character :appearsIn ?movie2 .

  FILTER (STR(?movie1) < STR(?movie2))
}

Output

 

character movie1  movie2
<http://example.org/marvel/Steve_Rogers>  <http://example.org/marvel/Avengers_Endgame>  <http://example.org/marvel/Infinity_War>
<http://example.org/marvel/Thanos>  <http://example.org/marvel/Avengers_Endgame>  <http://example.org/marvel/Infinity_War>
<http://example.org/marvel/Tony_Stark>  <http://example.org/marvel/Avengers_Endgame>  <http://example.org/marvel/Infinity_War>

   

Writing a Leaner Query

Remember the idea of writing lean SPARQL queries? In our query, we repeat both the subject variable and the predicate:

 

?character :appearsIn ?movie1 .
?character :appearsIn ?movie2 .

   

Just like Turtle syntax, SPARQL allows us to combine these patterns into a shorter form:

 

?character :appearsIn ?movie1, ?movie2 .

 

Updated Query:

 

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

SELECT ?character ?movie1 ?movie2
WHERE { 
  ?character :appearsIn ?movie1, ?movie2 .

  FILTER (STR(?movie1) < STR(?movie2))
}

This query produces exactly the same result while being more concise and easier to read.

 

Whenever you find yourself repeating the same subject and predicate multiple times, consider using this shorthand notation to keep your SPARQL queries lean and expressive.  

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment