Equality of Tables in SQL Part II

  • Thread starter WWGD
  • Start date
  • Tags
    sql
In summary: This can be implemented with the following query in MSSQL SELECT pkey FROM tableA WHERE NOT EXISTS (SELECT * FROM tableB WHERE pkey=pkey FROM tableA)
  • #1
WWGD
Science Advisor
Gold Member
7,019
10,590
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
  • #2
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
  • #3
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

Related to Equality of Tables in SQL Part II

1. What is the purpose of using "EQUALITY JOIN" in SQL?

The purpose of using "EQUALITY JOIN" in SQL is to combine data from two or more tables based on matching values in a specific column. This allows for more efficient and accurate data analysis and retrieval.

2. Can you explain how "EQUALITY JOIN" differs from other types of joins?

"EQUALITY JOIN" is a type of join that only includes rows from both tables where the values in the specified column are equal. This is different from other types of joins, such as "INNER JOIN" or "OUTER JOIN", which may include rows where the values are not equal.

3. How do you write an "EQUALITY JOIN" in SQL?

To write an "EQUALITY JOIN" in SQL, you would use the "ON" keyword and specify the column or columns to match on, followed by the "EQUALS" operator. For example: SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;

4. Can an "EQUALITY JOIN" be performed on more than two tables?

Yes, an "EQUALITY JOIN" can be performed on more than two tables by using multiple "ON" statements. Each "ON" statement would specify the column or columns to match on for that particular table.

5. Does the order of tables matter in an "EQUALITY JOIN"?

Yes, the order of tables does matter in an "EQUALITY JOIN". The first table listed in the query will be considered the "left" table, and the second table will be considered the "right" table. The join will be performed by matching values in the specified column(s) from the left table to the right table.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Computing and Technology
Replies
31
Views
3K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Sticky
  • Science and Math Textbooks
Replies
10
Views
5K
  • Programming and Computer Science
Replies
7
Views
3K
Replies
11
Views
1K
Replies
7
Views
892
Replies
80
Views
4K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
1K
Back
Top