0

Lets say I have two tables A and B which have the exact same columns. I want to write a query that selects all rows from A that also exist in B, so I would want to write something like:

select att1, att2, att3 from A 
where (att1, att2, att3) in (select att1, att2, att3 from B)

However, this does not work when my tables are allowed to contain null values, as the in operator checks equality using =, so for example att1 = att1. This won't work on null values as null = ... always results in false.

Whats the best/easiest way to do the same thing as the query above but that also works when there are null values (returns true if the 2 compared attributes are null)

1 Answer 1

4

How about INTERSECT set operator, instead?

select att1, att2, att3 from A 
intersect
select att1, att2, att3 from B
Sign up to request clarification or add additional context in comments.

3 Comments

thank you, this works. Another question: Is there a way to use this in a DELETE statement? So delete all from A that also exist in B ?
DELETE a.*, b.* FROM table1 as a, table2 as b WHERE (A.ATT1, A.ATT2, A.ATT3) IN (select att1, att2, att3 from A intersect select att1, att2, att3 from B);
@Krise . . . Note that this doesn't do exactly what you are requesting, because INTERSECT removes duplicates. That said, it is probably what you want, but worth pointing out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.