PREFIX rdf: PREFIX : PREFIX dc: SELECT * # ?p ( 2*count(distinct ?t) AS ?cd) { # :x2 has :p = 2 so UNION is asymmetric in what it generates { ?x rdf:type :X . } UNION { ?x rdf:type :X . FILTER (?x != :x2 )} ?x rdf:type ?t . OPTIONAL { ?x :p ?p . } OPTIONAL { ?x :r ?r . } } GROUP BY ?p (1/?p AS ?q) #HAVING (count(*) > 1) ## # ORDER BY ?q ?count