select e.last_name, e.hire_date
from employees e join employees m
on (m.last_name = \'Davies\')
and (e.hire_date > m.hire_date);
select e.last_name, e.hire_date
The main difference is when you are using different joins.
Typically you should see the same result if you were to use inner joins, but once you start using LEFT joins the results will change.
Have a look at the following example
And have a look at the following article (very explanatory)
EDIT for @ShannonSeverance
Schema and Test data
CREATE TABLE Table1 (
ID INT,
Val VARCHAR(20)
);
INSERT INTO Table1 VALUES (1,'a');
INSERT INTO Table1 VALUES (2,'a');
CREATE TABLE Table2 (
ID INT,
Val VARCHAR(20)
);
INSERT INTO Table2 VALUES (1,'a');
and Tests
SELECT t1.ID,
t1.Val,
t2.ID ID2,
t2.Val Val2
FROM Table1 t1 INNER JOIN
Table2 t2 ON t1.ID = t2.ID AND t1.Val = t2.Val;
SELECT t1.ID,
t1.Val,
t2.ID ID2,
t2.Val Val2
FROM Table1 t1,Table2 t2
WHERE t1.ID = t2.ID
AND t1.Val = t2.Val;
SELECT t1.ID,
t1.Val,
t2.ID ID2,
t2.Val Val2
FROM Table1 t1 LEFT JOIN
Table2 t2 ON t1.ID = t2.ID AND t1.Val = t2.Val;
SELECT t1.ID,
t1.Val,
t2.ID ID2,
t2.Val Val2
FROM Table1 t1 LEFT JOIN
Table2 t2 ON t1.ID = t2.ID
WHERE t1.Val = t2.Val;
Using on usually used for querying more than one table. When making that query, tables must have relationship each other, in general the same value in a specific fields.
on will connect that same value, for example:
**table1**:
id_name id_position name
1 1 john
2 2 doe
3 2 tom
4 3 hawkins
**table2**
id_position position
1 system analyst
2 programmer
SELECT t1.id_name, t1.name, t2.position
FROM table1 t1 LEFT JOIN table2 t2
ON t1.id_position = t2.id_position
-- RESULT:
id_name name position
1 john system analyst
2 doe programmer
3 tom programmer
4 hawkins NULL -- NO MATCH IN table 2
as we can see on will connect table1 and table2 that have same value id_position, so it is a little different from what you have been written above.
While where can be used in every query and not depends how many tables in that query. In general where is used for conditional thing that we want.
where is a filter which is applied after rows are selected using the join. It is not always the case that a join ... on condition is sematically equivalent to a where condition. Therefore, yes, there is a particular reason to use a where in table joins: when it does the right thing.
...and by contrast, the
ONcondition executes as the join is being made.ONconditions for joins earlier in multi-table joins can cut off millions of unnecessary joins so are generally preferred if semantically correct
– Bohemian
The difference lies in when the engine performs the filtering. A "where" represents a filter on the computed product of both tables. The "on" keyword specifies how a join is performed. They are not semantically equivalent even if sometimes they both produce the same outcome.
Cheers
The ON clause defines the relationship between the tables.
ON Clause supports all join types.The WHERE clause describes which rows you are interested in.
WHERE Clause only supports for Inner join not for Outer joins like LEFT
JOIN and RIGHT JOIN.