I have two tables in a SQL Server 2008 database in which I want to find:
- values present in both tables (with all columns present in both tables)
- values present in first table but not in second table
- values present in second table but not in first table
Code:
CREATE TABLE #_temp
(ATM INT, Fault INT)
CREATE TABLE #_temp1
(ATM INT, Fault INT)
INSERT INTO #_temp VALUES (10,101), (11,101), (12,101), (12,101), (10,105), (13,101)
INSERT INTO #_temp1 VALUES (10,102), (11,101), (12,103), (12,100), (10,105), (13,101)
/* My Try
SELECT * FROM #_temp t RIGHT JOIN #_temp1 t1 ON t.ATM=t1.ATM AND t.Fault=t.Fault AND t.ATM IS NULL AND t.Fault IS NULL
SELECT * FROM #_temp t JOIN #_temp1 t1 ON t.ATM=t1.ATM AND t.Fault=t.Fault
*/
DROP Table #_temp
DROP Table #_temp1
To find values that exist in one table and not another you should use a where clause to determine the nulls: