Equality of Tables in SQL Part II

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    sql
Click For Summary
SUMMARY

This discussion focuses on determining the equality of two SQL tables using the concept of symmetric difference. The user outlines a method for implementing this in MS SQL, highlighting the use of the 'EXCEPT' and 'INTERSECT' operators for normalized tables, while noting that these do not apply to non-normalized tables with repeated records. The proposed SQL queries utilize UNION and UNION ALL to compare tables, ultimately concluding that if the row count is zero, the tables are equal.

PREREQUISITES
  • Understanding of SQL set operations, specifically UNION, UNION ALL, EXCEPT, and INTERSECT.
  • Familiarity with MS SQL Server syntax and functions.
  • Knowledge of table normalization and its implications on SQL queries.
  • Basic mathematical concepts of set theory, particularly symmetric difference.
NEXT STEPS
  • Research the implementation of SQL set operations in MS SQL Server.
  • Learn about table normalization and its impact on SQL query performance.
  • Explore the differences between UNION and UNION ALL in SQL.
  • Investigate performance optimization techniques for SQL queries involving large datasets.
USEFUL FOR

Database developers, data analysts, and anyone involved in SQL table management or data integrity verification will benefit from this discussion.

WWGD
Science Advisor
Homework Helper
Messages
7,806
Reaction score
13,120
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   Reactions: 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   Reactions: WWGD

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
  • Sticky
  • · Replies 16 ·
Replies
16
Views
13K
  • · Replies 11 ·
Replies
11
Views
3K
Replies
1
Views
2K
  • · Replies 23 ·
Replies
23
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 80 ·
3
Replies
80
Views
8K