While trying an outer join query I noticed that changing one condition from the where clause to the join clause changes the result. That surprised me but I simplified the tables and the query as below and now I think I understand but I would like to hear a solid explanation.
create table t0 (id int, b int);
create table t1 (id int, b int);
insert into t0 (id, b) values (1, 10), (2, 10);
insert into t1 (id, b) values (1, 2);
select t0.id, t0.b
from t0
left outer join t1 on
t0.id = t1.id
where
t0.b = 10
and
t1.b = 2
;
id | b
----+----
1 | 10
(1 row)
Now I move one of the conditions from the where to the join clause:
select t0.id, t0.b
from t0
left outer join t1 on
t0.id = t1.id
and
t1.b = 2
where
t0.b = 10
;
id | b
----+----
1 | 10
2 | 10
(2 rows)
Do you know how to write a straight reasoning?
The
oncondition of anouter joinonly determines if thejoinsucceeds. If the join fails, the joined columns are filled withnull.On the other hand, a
whereclause filters entire rows out of the result set.To make this more clear, addd
t1.bto the result set. Witht1.b = 2as awherecondition, you get:versus
t1.b = 2as anoncondition::You can see why the
whereclause filters the second row out:null = 2is not true.