Jiantao Wu
Jiantao Wu

Reputation: 73

The mechanism of "FILTER NOT EXISTS" in SPARQL

Assuming the triples are following:

@prefix  :       <http://example/> .
@prefix  rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix  foaf:   <http://xmlns.com/foaf/0.1/> .

:alice  rdf:type   foaf:Person .
:alice  foaf:name  "Alice" .
:bob    rdf:type   foaf:Person .  

and then we perform 3 queries based on SPARQL 1.1:

Q1:

SELECT ?s 
WHERE 
{
    ?s ?p ?o .
    FILTER NOT EXISTS { ?s foaf:name ?y }
} 

Q2:

SELECT ?s 
WHERE 
{
    ?s ?p ?o .
    FILTER NOT EXISTS { ?x foaf:name ?y }
} 

Q3:

SELECT ?s 
WHERE 
{
    ?s ?p ?o .
    FILTER NOT EXISTS { ?x foaf:mailbox ?y }
} 

These three queries return three different solutions. Could anyone help me figure out why Q2 evaluates to no query solution in contrast to Q1 and Q3? Many thanks in advance :)

Upvotes: 6

Views: 11339

Answers (1)

Jeen Broekstra
Jeen Broekstra

Reputation: 22052

Q2 returns no solution because in your data, there exists a statement that matches ?x foaf:name ?y: ?x = :alice and ?y = "Alice". You've put no further constraints on either ?x or ?y. So no matter what the other variables in your query (?s, ?p and ?o) are bound to, the NOT EXISTS condition will always fail and therefore the query returns no result.

Upvotes: 6

Related Questions