Check if two rows of the table are equal

There are two tables with the same structure.
Suppose the number of rows in both is equal.
how would you check if all rows are equal? Is there a faster way than comparing each column value of a given row with the same identifier in both tables?

+8
sql sql-server
source share
2 answers

Try the following:

SELECT * FROM table1 EXCEPT SELECT * FROM table2 

If something returns, then they are not equal.

+19
source share

Abe's answer is correct, but only if they have the same number of lines. (I misunderstood the question when I wrote my initial answer, "condemning" his answer.) If table1 can be a subset of the (larger) table2, or vice versa, I would try:

 select ( not exists ( select * from table1 except select * from table2 ) and not exists ( select * from table2 except select * from table1 ) ) 

This gives true if they are the same, and false if they are different.

+9
source share

All Articles