I have 2 tables. I want to get the records from both the table using FULL OUTER JOIN
while filtering out some data from one of the tables.
- 14026 - Total number of common records.
- 8428 - unique records in table 1
- 1512 - unique records in table 2.
The total records that I should get are 23966. However, the following queries return different results.
Query 1 -
SELECT DISTINCT pu.id,
vm.id
FROM table1 vm
FULL OUTER JOIN (SELECT DISTINCT pu.id
FROM table2 pu
WHERE pu.column1 = 'filter1'
AND pu.column2 = 'filter2') pu ON vm.id = pu.id;
Output - 23966 rows (expected).
Query 2 -
SELECT DISTINCT pu.id,
vm.id
FROM table1 vm
FULL OUTER JOIN table2 pu ON vm.id = pu.id AND pu.column1 = 'filter1' AND pu.column2 = 'filter2';
Output - 48804 rows.
Query 3 -
SELECT DISTINCT pu.id,
vm.id
FROM table1 vm
FULL OUTER JOIN table2 pu ON vm.id = pu.id
WHERE (pu.column1 = 'filter1' AND pu.column2 = 'filter2')
OR (pu.column1 IS NULL AND pu.column2 IS NULL);
Output - 21830.
According to my understanding at least the third query should also give me the same result as query 1. Can someone explain me how SQL Server is treating these 3 queries?