Explain Codes LogoExplain Codes Logo

How do I compare two columns for equality in SQL Server?

sql
join
best-practices
performance
Alex KataevbyAlex Kataev·Jan 6, 2025
TLDR

You can match two columns using the equality operator in a SELECT query:

-- Yes, they are twins! SELECT * FROM your_table WHERE column1 = column2;

To find rows where column values differ, use inequality operator <>:

-- They are more like Tom and Jerry! SELECT * FROM your_table WHERE column1 <> column2;

Interested to know the count of matching versus non-matching rows? Use CASE:

-- Is it just me or does everyone <3 binary? SELECT CASE WHEN column1 = column2 THEN 1 ELSE 0 END AS IsMatch FROM your_table;

Comparing columns from two different tables

Comparisons often involve different tables. Use a join operation in such cases:

-- Who knew SQL could play matchmaker? SELECT a.*, b.*, CASE WHEN a.column1 = b.column2 THEN 1 ELSE 0 END AS IsMatch FROM Table1 a INNER JOIN Table2 b ON a.PrimaryKey = b.ForeignKey;

Two words: IIF function

SQL Server 2012 introduced the IIF function as a shorter alternative to CASE:

-- Because who doesn't like snack-sized code, right? SELECT *, IIF(column1 = column2, 1, 0) AS IsMatch FROM your_table;

Remember, backward compatibility issues may arise if you're using an older SQL Server version.

Null is not zero, friends!

While comparing, handle NULL values with utmost care:

-- Nulls are the ninjas of the SQL world. They are elusive! SELECT ISNULL(NULLIF(column1, column2), 1) AS IsNotMatch FROM your_table;

This efficiently returns 1 for non-matching columns, even considering NULLs.

Outer join: the detective of SQL

For robust comparisons encompassing all possible scenarios, apply a FULL OUTER JOIN:

-- Nothing escapes the vigilant eyes of an OUTER JOIN! SELECT a.column1, b.column2, CASE WHEN a.column1 = b.column2 THEN 'Equal' WHEN a.column1 IS NULL OR b.column2 IS NULL THEN 'One Is Null' ELSE 'Not Equal' END AS ComparisonResult FROM Table1 a FULL OUTER JOIN Table2 b ON a.PrimaryKey = b.ForeignKey;

Hidden aspects of column comparison

Collations: the silent influencers

Collations dictate comparison rules - always check they match:

-- SQL version of 'When in Rome, do as the Romans do.' SELECT * FROM Table1 WHERE column1 COLLATE Latin1_General_CI_AS = column2 COLLATE Latin1_General_CI_AS;

Pattern matching? Like!

Comparing string patterns? Here's how:

-- Wanted: column1. Last seen wearing: pattern of column2. SELECT * FROM your_table WHERE column1 LIKE column2; -- '%' and '_' are the Usain Bolts here!

Note: Pattern matching may slow down your query!

Between me, you & the range

For numeric ranges, meet BETWEEN:

-- Play it safe, stay within the boundaries! SELECT * FROM your_table WHERE column1 BETWEEN start_value AND end_value;

Keep no secrets while comparing

Data types: apples & oranges

Comparing unlike data types might fail. Convert or cast as needed:

-- SQL: I can make an apple out of an orange. No big deal! SELECT * FROM your_table WHERE CAST(column1 AS INT) = CAST(column2 AS INT);

Queries: the fast and the furious

Comparisons can slow down your queries. Watch your indexing:

-- Someday, SQL will have its own version of 'Fast & Furious' SELECT * FROM your_table WHERE indexed_column = non_indexed_column;

Analyze your execution plans and fix indexing for speedy retrieval!