Monday, 27 July 2026

Using FILTER NOT EXISTS in SPARQL: Finding Missing Data in an RDF Graph

  

So far, we've focused on finding information that does exist in an RDF graph.

 

For example, we might ask:

 

·      Which heroes belong to the Avengers?

·      Which villains fought against the Avengers?

·      Which characters appeared in a particular movie?

 

However, real-world data is often incomplete. Sometimes the more interesting question is:

 

"Which resources are missing a particular relationship?"

 

SPARQL provides a powerful way to answer these questions using negation.

 

One of the most common approaches is the FILTER NOT EXISTS construct, which allows us to find resources for which a specific graph pattern does not exist.

 

Let's explore this using our Marvel RDF dataset.

 

Finding Heroes That Appear in Movies

Before looking for missing data, let's first find superheroes that have at least one movie appearance recorded.

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

SELECT ?heroName ?movieTitle
WHERE {
  ?person :alias ?hero ;
          :appearsIn ?movie .

  ?hero :name ?heroName .
  ?movie :title ?movieTitle .
}

   

Output

 

heroName  movieTitle
Captain America Avengers: Endgame
Captain America Avengers: Infinity War
Iron Man  Avengers: Endgame
Iron Man  Avengers: Infinity War

These results make sense because Tony Stark and Steve Rogers have :appearsIn relationships in the graph.


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment