create table #t1 (id int)
create table #t2 (id int)
insert into #t1 values (1)
insert into #t1 values (2)
insert into #t1 values (3)
insert into #t2 values (1)
ins
In query 1 we have b.id is null in join criteria, which because being LEFT JOIN on A returns all rows of A irrespective of JOIN criteria and therefore returns 3 rows for table A.
In query 2 first LEFT JOIN on A returns 3 rows and then where b.id is null is applied on these 3 rows which leaves the third row and results in only 1 rows for the third row for id=3 .
Further explanation:
Your comment
Though in Query1 how come b.id is null ??
requires detailed explanation
as I said in Query 1 for each row of A there is a row returned in Left join irrespective of criteria of JOIN
so your join criteria is actually
a.id = b.id and b.id is null
a logical And between two criteria which cannot be true simultaneously as if b.id is null is true then a.id=null is being matched which is basically null
output is given here in this fiddle: http://sqlfiddle.com/#!3/c20ba/1
and is like
id | id
________
1 | null
2 | null
3 | null
Another point to note: Do note that in SQL id=NULL is evaluated as NULL. Also note that when doing logical operations like AND OR in sql query, NULL behaves quite peculiarly. See the msdn documentation on this behavior
null and true = null
null and false=false
null and null =null
null or null =null
null or true= true
null or false=null