prefix rdfs: prefix : select ?G ?P ?GP from where {{?G a :Parent} UNION {?GP a :Grandparent} UNION {:Grandparent rdfs:subClassOf :Parent}}