To compare the two tables and returning only those records present in either of the tables, you can use any one of the below approaches:
Using “Union ALL” and “Except” clause -
SELECT *
FROM (
SELECT FirstName, LastName, Product
FROM TableA
EXCEPT
SELECT FirstName, LastName, Product
FROM TableB
UNION ALL
SELECT FirstName, LastName, Product
FROM TableB
EXCEPT
SELECT FirstName, LastName, Product
FROM TableA
) AS Discrepancies;
SELECT FirstName, LastName, Product
FROM TableA
EXCEPT
SELECT FirstName, LastName, Product
FROM TableB
This part finds all the records present in Table A but not in B
SELECT FirstName, LastName, Product
FROM TableB
EXCEPT
SELECT FirstName, LastName, Product
FROM TableA
This part finds all records present in Table B but not in A.
Union ALL combines the results of both the queries and give the desired output.
Using Full Outer join
SELECT A.FirstName AS A_FirstName, A.LastName AS A_LastName, A.Product AS A_Product,
B.FirstName AS B_FirstName, B.LastName AS B_LastName, B.Product AS B_Product
FROM A
FULL JOIN B
ON A.FirstName = B.FirstName
AND A.LastName = B.LastName
AND A.Product = B.Product
WHERE A.FirstName IS NULL OR B.FirstName IS NULL
OR A.LastName IS NULL OR B.LastName IS NULL
OR A.Product IS NULL OR B.Product IS NULL;
Full join combines rows from A and B and the where clause filters the rows when there is a discrepancy, i.e. one table has the data but the other table does not.