Comparing two sets in SPARQL to find instances in Set 1 that do not exist in Set 2

Viewed 37

I am trying to create two sets in SPARQL, and return the number of instances in ?set1 that do not exist in ?set2. I am trying to do this all in SPARQL, and I was not able to find a good resource on how to do this through FILTER, as well as using MINUS, but not having any luck.



SELECT COUNT(?set1)
WHERE {
?S ?P ?set1 .
?Q ?R ?set2 .
FILTER (member in ?set1 doesn't exist in ?set2
}

1 Answers

You could use FILTER NOT EXISTS:

SELECT (COUNT(?electronic) AS ?count)
WHERE {
  ?employee :usesElectronic ?electronic .
  
  FILTER NOT EXISTS { ?company :ownsElectronic ?electronic . }
}

This counts the devices that

  • are used by any employee, and
  • are not owned by any company.

If you just need to know if such a case exists, and you are not interested in the count, you could use an ASK query instead, which returns TRUE or FALSE. For that, replace the whole SELECT line with ASK.

Related