0

I have two tables A & B and i would like to have a query that : return TRUE ONLY if the two tables are the same (i mean every lines in A are present in B & vice versa, no matter the line order)

I have use the keyword EXCEPT but it doesnt work in many cases

Thanks for your help.

3
  • Please post what you've tried, along with a list of columns in tables A and B. Commented Dec 29, 2009 at 10:15
  • how large is your dataset? What problems do you encounter with EXCEPT ? Commented Dec 29, 2009 at 10:16
  • The dataset is very small (less than 20 rows) & the query is used just for debuggin not in production. Commented Dec 29, 2009 at 10:34

1 Answer 1

7
select * from tablea except all select * from tableb 

returns all rows from tablea that do not exist in tableb.

doing it the other way around

select * from tableb except all select * from tablea 

returns all rows in tableb that do not exist in tablea.

so, now we can:

select count(*) from ( select * from tablea except all select * from tableb ) x; 

to get number of "bad" rows in tablea, and:

select count(*) from ( select * from tableb except all select * from tablea ) x; 

to get count of "bad" rows in tableb.

tables are the same if both counts are 0. and since neither count can be less than zero, then we can test if sum of the counts is 0:

select 0 = ( select count(*) from ( select * from tablea except all select * from tableb ) x ) + ( select count(*) from ( select * from tableb except all select * from tablea ) x ); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man !! Except the position of the + operator, it is working fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.