After reading it, this is not a duplicate of Explicit vs Implicit SQL Joins. The answer may be related (or even the same) but the question is different.
What is the difference and what should go in each?
If I understand the theory correctly, the query optimizer should be able to use both interchangeably.
In SQL, the 'WHERE' and 'ON' clause,are kind of Conditional Statemants, but the major difference between them are, the 'Where' Clause is used in Select/Update Statements for specifying the Conditions, whereas the 'ON' Clause is used in Joins, where it verifies or checks if the Records are Matched in the target and source tables, before the Tables are Joined
For Example: - 'WHERE'
For Example: - 'ON'
*There are two tables employee and employee_details, the matching columns are employee_id.*
Hope I have answered your Question.Revert back for clarifications.
On
INNER JOIN
s they are interchangeable, and the optimizer will rearrange them at will.On
OUTER JOIN
s, they are not necessarily interchangeable, depending on which side of the join they depend on.I put them in either place depending on the readability.
for better performance tables should have a special indexed column to use for JOINS .
so if the column you condition on is not one of those indexed columns then i suspect it is better to keep it in WHERE .
so you JOIN using the indexed columns, then after JOIN you run the condition on the none indexed column .
They are not the same thing.
Consider these queries:
and
The first will return an order and its lines, if any, for order number
12345
. The second will return all orders, but only order12345
will have any lines associated with it.With an
INNER JOIN
, the clauses are effectively equivalent. However, just because they are functionally the same, in that they produce the same results, does not mean the two kinds of clauses have the same semantic meaning.For an inner join,
WHERE
andON
can be used interchangeably. In fact, it's possible to useON
in a correlated subquery. For example:This is (IMHO) utterly confusing to a human, and it's very easy to forget to link
table1
to anything (because the "driver" table doesn't have an "on" clause), but it's legal.There is great difference between where clause vs. on clause, when it comes to left join.
Here is example:
There fid is id of table t2.
Query on "on clause" :
Query on "where clause":
It is clear that, the first query returns a record from t1 and its dependent row from t2, if any, for row t1.v = 'K'.
The second query returns rows from t1, but only for t1.v = 'K' will have any associated row with it.