Equality of Tables in SQL Part II

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    sql
AI Thread Summary
The discussion centers on determining the equality of two SQL tables using the concept of symmetric difference. The key formula presented is that two sets A and B are equal if their symmetric difference is empty. In MS SQL, the suggested approach involves using the 'EXCEPT' or 'INTERSECT' commands for normalized tables, but these methods are inadequate for non-normalized tables with duplicate records. A proposed query utilizes a combination of 'UNION' and 'NOT IN' to identify differences between the two tables. The discussion also touches on performance considerations, suggesting that using 'UNION ALL' may be more efficient, especially with a smaller number of columns. Ultimately, the mathematical representation for checking table equality is provided, indicating that if the count of the union of both tables minus their intersection is greater than zero, the tables are not equal.
WWGD
Science Advisor
Homework Helper
Messages
7,678
Reaction score
12,360
Hi all,
I think I found the answer to determine whether two SQL tables are equal. Symmetric difference of sets:

A(Symmetric Difference) B = ## A \cup B - (A\cap B) ##. One can show that sets A,B are equal iff
A (Symmetric Difference) B =A (=B, of course)

How does one implement this in the language of MS SQL?

For normalized tables, one can use 'EXCEPT' or 'INTERSECT' , but this does not work for non-normalized tables with repeated records.
 
Technology news on Phys.org
If you do the following query:
Code:
SELECT pkey FROM tableA WHERE  pkey in (SELECT pkey FROM tableB where 1)
it should give you (A∩B)

and since [A - (A∩B)] U [B -(A∩B)] is equal to AUB -(A∩B)

Code:
SELECT pkey FROM tableA WHERE pkey NOT IN (SELECT pkey FROM tableA WHERE  pkey in (SELECT pkey FROM tableB where 1)) UNION
SELECT pkey FROM tableB WHERE pkey NOT IN (SELECT pkey FROM tableA WHERE  pkey in (SELECT pkey FROM tableB where 1))
Should give you what you're looking for.

I'm sure there will be easier ways however :)

*EDIT* that's MySQL not MS SQL. Not sure what the difference would be :) I can't read apparently
 
  • Like
Likes WWGD and Pepper Mint
You may prefer use of UNION or UNION ALL as they both look common to ## \cup ## and ##\cap ## in maths. Something like this

SELECT*FROM (
SELECT * FROM TABLE A
UNION ALL
SELECT * FROM TABLE B
);
IF@@ROWCOUNT=0
BEGIN
PRINT 'Equal'
END

Variations of this use of union include checks for EXISTS or NOT EXISTS of column data, which may work better in terms of performance and practicality only in case the number of columns is small.

So the mathematical formula to check for 2 MSSQL tables's equality in my case would be
|R|=|A## \cup ## B| where |R| denotes the number of elements in R
if |R|>0 then A!=B or A=B otherwise.
 
  • Like
Likes WWGD
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top