I have two tables, in which table 1 contains 4 columns while table 2 contains 8 columns. I have two columns in table1 that I want to compare them with two columns in table2.
Except shows the difference between two tables (the Oracle guys use minus instead of except and the syntax and use is the same). It is used to compare the differences between two tables. For example, let's see the differences between the two tables
SELECT * FROM
table1
EXCEPT
SELECT * FROM
table2
NOT EXISTS
is a "null safe" version of NOT IN
.
If you mean the combination column1 AND column2 not in same row in table2:
select *
from table1
where NOT EXISTS (select 1 from table2
where table1.column1 = table2.column6
and table1.column2 = table2.column7)
Or if you mean just column1 and column2 values can't even be in different rows in table2:
select *
from table1
where NOT EXISTS (select 1 from table2
where table1.column1 = table2.column6)
and NOT EXISTS (select 1 from table2
where table1.column2 = table2.column7)
SELECT * FROM table1 t1
RIGHT JOIN table2 t2
WHERE
t1.c1 = t2.c6 AND
t1.c2 = t2.c7
The query with the least comparisions I can think of is
Select t1.*
from table1 t1
left join table2 t2 on t1.column1 in (t2.column6, t2.column7)
or t1.column2 in (t2.column6, t2.column7)
where t2.column6 is null
select * from table1 where column1 not in(select column 6 from table2) or column2 not in(select column7 from table2)
This will give you rows from table1 where there are differences between col1 and col6 or col2 and col7
Hope this helps
Try a minus statement. This will give you any results from the first select statement of table1 that aren't in the second select statement on table2.
select column1, column2 from table1
minus
select column6, column7 from table2