What’s the difference between the following two SQLs:
select ...
from table1 t1
left join table2 t2 on t2.col1=t1.id and t2.col2='xxx'
where t1.col2='xxx'
select ...
from table1 t1
left join table2 t2 on t2.col1=t1.id
where t1.col2='xxx' and t2.col2='xxx'
I found when the same clause gets put at different positions the result could sometimes be different, even more the sql performance could vary dramatically. What’s the major differences if I put the extra clause right after a left join or to put it after ‘where’?
Can somebody offer an explanation? Thanks in advance.
Let’s rewrite SQLs:
Fill the table with data
t1 (id, col2):
1 xxx
2 xxx
3 jjj
t2 (col1, col2):
1 uuu
2 xxx
3 xxx
Result of first query:
1 NULL
2 2
Result of second query:
2 2
So, if you put any conditions in ‘join’, joining line may not be joined and result of join will be null.
See this link to understand