MSSQL: Checking for Equality of Tables (Hints only, please)

  • Thread starter Thread starter WWGD
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around methods to determine if two tables, T1 and T2, in MSSQL2012 are equal. Participants explore various approaches, hints, and considerations related to SQL queries, particularly focusing on the use of the "EXCEPT" operator, handling duplicates, and comparing table structures.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests using the "EXCEPT" operator to check for equality by querying both tables and expecting an empty result if they are equal.
  • Another participant raises concerns about the necessity of a 'WHERE' clause and the potential for primary key differences or other discrepancies like timestamps.
  • A suggestion is made for a correlated subquery approach, questioning the effectiveness of selecting entries based on conditions.
  • One participant proposes inserting a logical field in T2 to track matching records, indicating a manual method to identify discrepancies.
  • There is a discussion about the implications of duplicate rows in the tables, with suggestions to count occurrences or sort tables before comparison.
  • Another participant mentions using the UNION operator to compare row counts between the two tables as a potential method.
  • Hints are provided to check documentation for the EXCEPT operator and considerations regarding schema differences and edge cases.
  • Participants discuss the importance of context in determining the appropriate method for comparison, including the purpose of the comparison and potential differences between the tables.

Areas of Agreement / Disagreement

Participants express multiple competing views on the best approach to determine table equality, with no consensus reached on a single method. Concerns about handling duplicates and the need for additional checks indicate unresolved aspects of the discussion.

Contextual Notes

Participants note limitations regarding assumptions about table normalization and the presence of duplicate records, which complicate the equality check. There are also mentions of potential edge cases that may affect the results of the proposed methods.

Who May Find This Useful

This discussion may be useful for database administrators, SQL developers, or anyone interested in comparing data integrity between tables in MSSQL, particularly in scenarios involving theoretical challenges or practical applications in data management.

WWGD
Science Advisor
Homework Helper
Messages
7,785
Reaction score
13,076
Hi all,
I am trying to find a method to determine if two tables T1, T2 (of any size) in MSSQL2012 are equal.
Please just give hints

My idea is to use the "Except" operator this way:

Select * from T1
Except
(Select * from T2)

And I should get an empty table with columns in both, but with no values entered.
Does this work? Any other hints?
 
Technology news on Phys.org
Only unspecific thoughts, since MSSQL isn't the version I used very often:

Beside automatic PK do you have another one to specify single entries? I can't imagine this will work without 'where' clause plus 'not in'.
Maybe not only PK differ but also timestamps or the spelling of char entries. (Runtime might get a problem, too.)
 
Never mind, Fresh - Prinz_42 of Bel_ Deutschair , I think a correlated subquery using >< should do. Do you agree?
 
Last edited:
WWGD said:
Hi all,
I am trying to find a method to determine if two tables T1, T2 (of any size) in MSSQL2012 are equal.
Please just give hints

My idea is to use the "Except" operator this way:

Select * from T1
Except
(Select * from T2)

And I should get an empty table with columns in both, but with no values entered.
Does this work? Any other hints?
Hints:
  1. Look up the documentation and you will see that that is exactly what EXCEPT is for.
  2. Do you think that you will always get the same results from
    Code:
    SELECT * FROM t2 EXCEPT SELECT * FROM t1
  3. You don't need the ()s.
  4. Look at the documentation to see treatment of schema differences and edge cases.
  5. You could also look at the command line utility tablediff.
  6. Use capitals for reserved words.
Edited: forget to hint about asymmetry
 
  • Like
Likes   Reactions: WWGD
WWGD said:
Never mind, Fresh - Prinz_42 of Bel_ Deutschair , I think a correlated subquery using >< should do. Do you agree?
Depends on how you join them. If you're "lucky" you will select to each A.entry_m all B.entry_n with n≠m. What speaks against select A.entry where A.entry not in (select B.entry where A.entry = B.entry); beside runtime?
 
  • Like
Likes   Reactions: WWGD
WWGD said:
I am trying to find a method to determine if two tables T1, T2 (of any size) in MSSQL2012 are equal.
I tend to do it the hard way. Insert a logical field in T2, set them all to "false" and use a query to set the field to "true" in all matching records. The ones left out are the records where the field value is still "false".
 
  • Like
Likes   Reactions: WWGD
MrAnchovy said:
Hints:
  1. Look up the documentation and you will see that that is exactly what EXCEPT is for.
  2. Do you think that you will always get the same results from
    Code:
    SELECT * FROM t2 EXCEPT SELECT * FROM t1
  3. You don't need the ()s.
  4. Look at the documentation to see treatment of schema differences and edge cases.
  5. You could also look at the command line utility tablediff.
  6. Use capitals for reserved words.
Edited: forget to hint about asymmetry
Thanks, Anchovy, but does your answer assume the tables are normalized? I am thinking I have the same n distinct records in each, but these n records may be
repeated a different number of times in each table, say record_1 appears 7 times in table 1, and appears 8 times in table 2 , etc. How do I account for this; should I select count for each record := record _i and then compare the counts for each?
 
WWGD said:
Thanks, Anchovy, but does your answer assume the tables are normalized? I am thinking I have the same n distinct records in each, but these n records may be
repeated a different number of times in each table, say record_1 appears 7 times in table 1, and appears 8 times in table 2 , etc. How do I account for this; should I select count for each record := record _i and then compare the counts for each?
Sorry for the delay, I haven't been around for a while - but then there was even more of a lag for your question!

It's not going to work with duplicate rows, but then you asked for hints not a complete solution! If there are (or could be) duplicates then yes you are going to need to come up with a different solution. Counting the occurrences for each distinct row in each table and compare the counts could work. Or sort both tables by the same complete ordering, add a sequence column and then use EXCEPT. Or write a script in your language of choice to iterate over sorted recordsets. Or use tablediff. It all depends on the context - why do you want to compare these tables, why might they be different and what do you want to do if they are?
 
  • Like
Likes   Reactions: WWGD
MrAnchovy said:
Sorry for the delay, I haven't been around for a while - but then there was even more of a lag for your question!

It's not going to work with duplicate rows, but then you asked for hints not a complete solution! If there are (or could be) duplicates then yes you are going to need to come up with a different solution. Counting the occurrences for each distinct row in each table and compare the counts could work. Or sort both tables by the same complete ordering, add a sequence column and then use EXCEPT. Or write a script in your language of choice to iterate over sorted recordsets. Or use tablediff. It all depends on the context - why do you want to compare these tables, why might they be different and what do you want to do if they are?
Thanks, it is a theoretical thing, just a challenge someone gave me to help me sharpen my SQL skills. I am allowed to get hints but not full answers.
 
  • #10
I think you can check UNION of both tables and find the rowcount which should equal the rowcount from table T1 as well as table T2. o_O
 
  • Like
Likes   Reactions: WWGD
  • #11
Thanks all, from my reading I think it may be a good idea to use rank or dense rank .

EDIT: I appreciate your suggestions and probably there are a few better than mine but this
is the one I am most familiar with at this point.
 
Last edited:
  • #12
WWGD said:
Thanks, it is a theoretical thing, just a challenge someone gave me to help me sharpen my SQL skills. I am allowed to get hints but not full answers.

To first compare the structures of 2 tables you can use the system view information_schema.columns.:cool:
 
  • Like
Likes   Reactions: WWGD

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 7 ·
Replies
7
Views
5K
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 7 ·
Replies
7
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K