Select Records From A Table, Which Don't Exist In Another Table
I have 2 tables TABLE1 and TABLE2 in sqlite DB. TABLE 2 has some of the records of the TABLE1. What I want is to select all the records from TABLE 1, which don't exist in TABLE2. S
Solution 1:
Check for NULLs in second table like:
SELECT TABLE1.name, TABLE1.surname, TABLE1.id
FROM TABLE1
LEFTJOIN TABLE2 ON TABLE1.id = TABLE2.id
WHERE TABLE2.id ISNULLAlternate solution with NOT EXISTS:
SELECT TABLE1.name, TABLE1.surname, TABLE1.id
FROM TABLE1
WHERENOTEXISTS(SELECT*FROM TABLE2 WHERE TABLE1.id = TABLE2.id)
One more with NOT IN:
SELECT TABLE1.name, TABLE1.surname, TABLE1.id
FROM TABLE1
WHERE TABLE1.id NOTIN(SELECT id FROM TABLE2)
Solution 2:
Use subquery to return all id of table 2, now include those ids in table1 which don't exist in table 2.
Select name, surname, idfrom TABLE1 where idnotin (Select idfrom TABLE2)
Post a Comment for "Select Records From A Table, Which Don't Exist In Another Table"