Yesterday, while debugging another developer code, I came across following query (say this query as Q1)
select b.id from b left join a on b.a_id = a.id where a.id is null
Table a has one field:
id primary key
Table b has two fields:
id primary key
a_id foreign key (reference a.id)
When I ran above query (Q1), it gave me results: All b.id where b.a_id was not available in a.id
But I didn't understand above query (Q1), specially "where" clause. If we are checking for a.id is null, how can it match to find those not available in b table.
Also, what is difference between Q1 and Q2 (below query):
select b.id from b where not exists (select * from a where a.id = b.a_id)
I didn't even understand Q2.