I have a query like this which i am supposed to refactor:
SELECT *
FROM a a1
,b common_alias
,b common_alias1
WHERE a1.id = common_alias.id(+)
AND a1.id = common_alias1.id(+)
AND common_alias.name = 'XYZ'
AND common_alias1.name = 'XYZ'
UNION
SELECT *
FROM a a1
,b common_alias
,b common_alias1
WHERE a1.id = common_alias.id(+)
AND a1.id = common_alias1.id(+)
AND common_alias.name = 'XYZ'
AND common_alias1.name = 'PQR'
It seems to me that i can remove the first query completely. Is there any case where removing the first query will make a difference?
Your outer join condition on
common_alias1.idshould be reviewed, it has no effect, as you don’t outer join oncommon_alias1.nameas well. See Oracle outer join with filter condition on the second table.But back to your question: no, you can’t remove the first part, where would you get the XYZ|XYZ row? See http://sqlfiddle.com/#!4/c7dfa/12 for an example.
But you can refactor to