PREFIX : SELECT * { # Use UNION to generate both possibilities in the same query. { OPTIONAL { :a :b ?x } . OPTIONAL { :d :e ?x } } ## UNION ## { OPTIONAL { :d :e ?y } . OPTIONAL { :a :b ?y } } }