I could not learn how to use JOIN well. I was always using server side code for such objectives. Is it possible to write join query without
ON? Basically how do these joins differ LEFT JOIN, RIGHT JOIN they do the same things on my examples.
MySQL documentation covers this topic.
Here is a synopsis. When using
inner join, the
on condition is optional. This is different from the ANSI standard and different from almost any other database. The effect is a
cross join. Similarly, you can use an
on clause with
cross join, which also differs from standard SQL.
A cross join creates a Cartesian product — that is, every possible combination of 1 row from the first table and 1 row from the second. The cross join for a table with three rows (‘a’, ‘b’, and ‘c’) and a table with four rows (say 1, 2, 3, 4) would have 12 rows.
In practice, if you want to do a cross join, then use
from A cross join B
is much better than:
from A, B
from A join B -- with no on clause
on clause is required for a right or left outer join, so the discussion is not relevant for them.
If you need to understand the different types of joins, then you need to do some studying on relational databases. Stackoverflow is not an appropriate place for that level of discussion.
See some example in http://www.sitepoint.com/understanding-sql-joins-mysql-database/
You can use ‘USING’ instead of ‘ON’ as in the query
SELECT * FROM table1 LEFT JOIN table2 USING (id);
You might also want to investigate the use of UNION, which combines multiple tables for one output: SQL – Combine two tables for one output