Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPARQL different operator

Tags:

sparql

SPARQL Query

I have some SPARQL query shown below:

SELECT DISTINCT ?name1 
    WHERE {

        GRAPH <blabla>
        {
            ?k swrc:author ?x .
            ?x foaf:name ?name1 . 
        } .

        GRAPH <blabla2>
        {   
            ?l swrc:author ?y .
            ?y foaf:name ?name2 .
        } .

        FILTER(?x != ?y) . 
    }

I want to get the names that exist only in the first graph blabla.

Problem

Counter intuitively I get some names that actually belong to the intersection. This happens because b (of set A) = b (of set B)?

Question

What are exactly the semantics of != ? How can I surpass this problem?

like image 380
Paramar Avatar asked Jun 06 '26 17:06

Paramar


1 Answers

The semantics of != are exactly that its left argument is not equal to its right argument. But a FILTER is evaluated for every possible combination of values - so the query as you formulated it will return all name-values of ?x for which some value of ?y is not equal to it.

If you want to get back only name-values of ?x for which all values of ?y are not equal to it, you should be using a NOT EXISTS clause:

SELECT DISTINCT ?name1 
WHERE {
 GRAPH <blabla>
 {
   ?k swrc:author ?x.
   ?x foaf:name ?name1. 
 }
 FILTER NOT EXISTS { 
     GRAPH <blabla2>
     {   
       ?l swrc:author ?x.
     }
  }

}

Note that using this approach you can actually get rid of variable ?y altogether: you change the condition to just check that author ?x as found in the first graph does not also occur in the second graph.

like image 95
Jeen Broekstra Avatar answered Jun 10 '26 08:06

Jeen Broekstra