When building SPARQL queries, it's common to combine graph patterns to retrieve exactly the data you need. So far we've seen several ways to combine patterns:
· Conjunction (.) — retrieve data that satisfies multiple conditions.
· OPTIONAL — retrieve additional information when it exists.
· UNION — combine results from alternative graph patterns.
There is one more useful construct: MINUS. The MINUS clause allows us to remove solutions that match a specific graph pattern from our result set. While similar results can often be achieved using FILTER or FILTER NOT EXISTS, MINUS provides a clear and declarative way to express exclusion.
Let's explore this using our Marvel RDF dataset.
Suppose we want to retrieve the names of all superheroes in our Marvel knowledge graph.
Query Without MINUS
PREFIX : <http://example.org/marvel/> SELECT ?heroName WHERE { ?hero a :Superhero ; :name ?heroName . } ORDER BY ?heroName
Output
heroName Black Widow Captain America Hulk Iron Man Star-Lord Thor
Now imagine we want the same list but we do not want to include Hulk.
SPARQL provides a dedicated construct for exclusion: MINUS.
PREFIX : <http://example.org/marvel/> SELECT ?heroName WHERE { ?hero a :Superhero ; :name ?heroName . MINUS { ?hero a :Superhero ; :name "Hulk" . } } ORDER BY ?heroName
Output
heroName Black Widow Captain America Iron Man Star-Lord Thor
Previous Next Home
No comments:
Post a Comment