An introduction to RDF querying in SPARQL

Zwifi
3,566 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Identifying graph pattern you don't want

The 'old' way: negation by optionality

What are the movies without a release date?
PREFIX yaco: <https://www.irit.fr/recherches/MELODI/ontologies/cinema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?movie ?year
WHERE {
?movie rdf:type yaco:Movie.
OPTIONAL {
?movie yaco:releaseYear ?year
}
FILTER(!bound(?year))
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Be careful with the scope:

What are the movies that were not released in 1997?
PREFIX yaco: <https://www.irit.fr/recherches/MELODI/ontologies/cinema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?movie ?year
WHERE {
?movie rdf:type yaco:Movie.
OPTIONAL {
?movie yaco:releaseYear ?year
}
FILTER(?year != 1997)
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
What are the movies that were not released in 1997?
PREFIX yaco: <https://www.irit.fr/recherches/MELODI/ontologies/cinema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?movie ?year
WHERE {
?movie rdf:type yaco:Movie.
OPTIONAL {
?movie yaco:releaseYear ?year
FILTER(?year != 1997)
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Negating with NOT EXISTS

What are the movies without Tom Hanks?
PREFIX yaco: <https://www.irit.fr/recherches/MELODI/ontologies/cinema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX : <https://www.irit.fr/recherches/MELODI/ontologies/cinema-catalog#>
SELECT ?movie
WHERE {
?movie rdf:type yaco:Movie.
FILTER NOT EXISTS{
:TomHanks yaco:isFeaturedIn ?movie.
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content