SQL
Compare Two Tables to Find Unique Rows (EXCEPT/MINUS)
Learn how to compare two tables and retrieve rows present in one table but not in the other using the SQL EXCEPT (or MINUS) operator for data reconciliation.
-- Find rows in TableA that are NOT in TableB
SELECT col1, col2, col3 FROM TableA
EXCEPT
SELECT col1, col2, col3 FROM TableB;
-- Find rows in TableB that are NOT in TableA
-- SELECT col1, col2, col3 FROM TableB
-- EXCEPT
-- SELECT col1, col2, col3 FROM TableA;
How it works: The EXCEPT operator (known as MINUS in Oracle) returns all distinct rows that are present in the result set of the first SELECT statement but not in the result set of the second SELECT statement. This is highly useful for identifying data discrepancies or unique entries between two tables with compatible column structures, such as comparing audit logs or synchronizing datasets.