I have to clean a database with a lot of orphaned entries, on this case i have a table 3 tables
- 'Email' (69529 entries)
- 'ServiceHasEmail' (5782 entries)
- 'UserHasEmail' (26254 entries)
The two last tables reference the 'Email' table, so this table should have 26254 + 5782 (32036) entries.
I built a query to select all the entries on the 'Email' table which are not referenced on the UserHasEmail and ServiceHasEmail tables:
SELECT * FROM Email e
WHERE e.EML_Id NOT IN (SELECT EML_Id FROM ServiceHasEmail)
AND e.EML_Id NOT IN (SELECT EML_Id FROM UserHasEmail)
But this query returns me 40383 entries in place of 37493 (69529 - (26254 + 5782))
What i am missing here?