Both are literally different.
The first query does the filtering of table t2
before the joining of tables take place. So the results will then be join on table t1
resulting all the records of t1
will be shown on the list.
The second one filters from the total result after the joining the tables is done.
Here's an example
Table1
ID Name
1 Stack
2 Over
3 Flow
Table2
T1_ID Score
1 10
2 20
3 30
In your first query, it looks like this,
SELECT a.*, b.Score
FROM Table1 a
LEFT JOIN Table2 b
ON a.ID = b.T1_ID AND
b.Score >= 20
What it does is before joining the tables, the records of table2
are filtered first by the score. So the only records that will be joined on table1 are
T1_ID Score
2 20
3 30
because the Score
of T1_ID
is only 10. The result of the query is
ID Name Score
1 Stack NULL
2 Over 20
3 Flow 30
While the second query is different.
SELECT a.*, b.Score
FROM Table1 a
LEFT JOIN Table2 b
ON a.ID = b.T1_ID
WHERE b.Score >= 20
It joins the records first whether it has a matching record on the other table or not. So the result will be
ID Name Score
1 Stack 10
2 Over 20
3 Flow 30
and the filtering takes place b.Score >= 20
. So the final result will be
ID Name Score
2 Over 20
3 Flow 30