Search code examples
sqloracleoracle-sqldeveloper

union along with a where clause


I want to do something like this, but I know that this would lead to error

select * from t1 a
union 
select * from t2 b
union 
select * from t3 c
where b.col1!=c.col1

What should I be using in order to achieve above?..

Thanks !!


Solution

  • I think that you want NOT EXISTS:

    select * from t1
    union 
    select * from t2
    union 
    select * from t3
    where not exists (
      select 1 from t2
      where t2.col1 = t3.col1
    )
    

    With this query t3's rows will be included in the final result only if the value of col1 does not exist also in any of t2's rows.