The Difference Between SQL's ON Condition and WHERE Condition
Category Programming Techniques
When a database returns records by joining two or more tables, it generates an intermediate temporary table, which is then returned to the user.
The difference between the on and where conditions when using left join is as follows:
The on condition is used when generating the temporary table. It returns records from the left table regardless of whether the condition in on is true or not.
The where condition is used to filter the temporary table after it has been generated. At this point, the meaning of left join (which is to return records from the left table) is no longer present; all records that do not meet the condition are filtered out.
Assuming there are two tables:
Table 1: tab1
id size
1 10
2 20
3 30
Table 2: tab2
size name
10 AAA
20 BBB
20 CCC
Two SQL statements:
1. select * from tab1 left join tab2 on tab1.size = tab2.size where tab2.name='AAA'
2. select * from tab1 left join tab2 on tab1.size = tab2.size and tab2.name='AAA'
The process of the first SQL:
- Intermediate table
on condition:
tab1.size = tab2.size
tab1.id tab1.size tab2.size tab2.name
1 10 10 AAA
2 20 20 BBB
2 20 20 CCC
3 30 (null) (null)
- Then filter the intermediate table
where condition:
tab2.name='AAA'
tab1.id tab1.size tab2.size tab2.name
1 10 10 AAA
The process of the second SQL:
- Intermediate table
on condition:
tab1.size = tab2.size and tab2.name='AAA'
(The left table's records are returned even if the condition is not true) tab1.id tab1.size tab2.size tab2.name
1 10 10 AAA
2 20 (null) (null)
3 30 (null) (null)
The key reason for the results above is the special nature of left join, right join, full join.
Regardless of whether the condition in the on is true or not, the records from the left or right table are returned, and full join has the union of the characteristics of left and right.
However, inner join does not have this special nature, so the result set returned is the same whether the condition is placed in the on or where.