SQL Tutorial - How to compare multiple columns in SQL

Learn how to compare multiple columns in SQL, in this video I cover comparing multiple columns in the WHERE clause, using CHECKSUM, BINARY_CHECKSUM and HASHBYTES. I also discuss the advantages and disadvantages of using CHECKSUM, BINARY_CHECKSUM and HASHBYTES and what is my preferred approach for comparing multiple columns. An important point when using CHECKSUM, BINARY_CHECKSUM or HASHBYTES to compare multiple columns is that the column list show always be in the same order to get an accurate comparison. When using the HASHBYTES function, always add a separator that does not exist in the data such as a pipe. If you would like more detail on the functions used in this video to compare multiple columns in SQL then take a look at the links below: The SQL queries used in this video are available below: DROP TABLE [dbo].[CustomerStaging]; CREATE TABLE [dbo].[CustomerStaging] ( [SourceKey] [int] NULL, [Title] [varchar](10) NULL, [FirstName] [varchar](50) NULL, [MiddleName] [varchar](50) NULL, [LastName] [varchar](50) NULL, [DOB] [date] NULL, [AddressLine1] [varchar](50) NULL, [AddressLine2] [varchar](50) NULL, [City] [varchar](50) NULL, [PostCode] [varchar](10) NULL ); INSERT INTO (SourceKey, Title, FirstName, MiddleName, LastName, DOB, AddressLine1, AddressLine2, City, PostCode) VALUES (1000, ’Mrs’, ’Deborah’, NULL, ’Wilkinson’, ’19960513’, ’1 The Barns’, NULL, ’London’, ’NE1 1RS’), (2000, ’Dr’, ’Siobhan’, ’Mary’, ’Jones’, ’19840822’, ’Portobello House’, ’Yarn Street’, ’Birmingham’, ’B16 1PZ’), (3000, ’Mr’, ’Karl’, NULL, ’Smith’, ’19790906’, ’5 Main Street’, NULL, ’Manchester’, ’M1 1PQ’); DROP TABLE [dbo].[Customer]; CREATE TABLE [dbo].[Customer] ( [CustomerKey] [int] NOT NULL IDENTITY(1, 1), [SourceKey] [int] NULL, [Title] [varchar](10) NULL, [FirstName] [varchar](50) NULL, [MiddleName] [varchar](50) NULL, [LastName] [varchar](50) NULL, [DOB] [date] NULL, [AddressLine1] [varchar](50) NULL, [AddressLine2] [varchar](50) NULL, [City] [varchar](50) NULL, [PostCode] [varchar](10) NULL ); INSERT INTO (SourceKey, Title, FirstName, MiddleName, LastName, DOB, AddressLine1, AddressLine2, City, PostCode) VALUES (1000, ’Miss’, ’Deborah’, NULL, ’Austin’, ’19960513’, ’1 The Barns’, NULL, ’London’, ’NE1 1RS’), (2000, ’Dr’, ’Siobhan’, ’Mary’, ’Jones’, ’19840822’, ’Portobello House’, ’Yarn Street’, ’Birmingham’, ’B16 1PZ’), (3000, ’Mr’, ’Karl’, NULL, ’Smith’, ’19790906’, ’250 Wood Road’, NULL, ’Manchester’, ’M25 1HH’); -- CHECKSUM SELECT * FROM AS A LEFT OUTER JOIN AS B ON = WHERE CHECKSUM( , , , , , , , , ) [replace with not equal to] CHECKSUM( , , , , , , , , ) -- BINARY_CHECKSUM SELECT * FROM AS A LEFT OUTER JOIN AS B ON = WHERE BINARY_CHECKSUM( , , , , , , , , ) [replace with not equal to] BINARY_CHECKSUM( , , , , , , , , ) -- HASHBYTES SELECT * FROM AS A LEFT OUTER JOIN AS B ON = WHERE HASHBYTES(’SHA2_512’, CONCAT( , , , , , , , , ) ) [replace with not equal to] HASHBYTES(’SHA2_512’, CONCAT( , , , , , , , , ) )
Back to Top