SQL is not very easy to learn, in particular joins of multiiple tables are difficult to understand at a first glance. On the next pages it's explained for sql beginners how SQL joins work. Venn diagrams are used to explain how the tables are combined. In addition sample tables with sample data and sample joins are used to explain in detail the query input data results. SQL files to create the sample data and execute the queries can be downloaded. The samples are for mySQL and DB2.
If you want to see more complex sql joins in action visit Learning SQL by example for sql join examples and other SQL funktions as views, count, max, subselect and other.
sqj joins better because there exist similar operations.So Venn diagrams are used in the following paragraphs to help to explain the common SQL operations.
+----+-----------+---------+-------+
| id | name | nation | table |
+----+-----------+---------+-------+
| 1 | Albert | Germany | A |
Tabelle B:
mysql> select * from TableB;
+----+--------+--------+-------+
| id | name | nation | table |
+----+--------+--------+-------+
| 1 | Marc | China | B |
| 2 | Albert | France | B |
| 3 | Peter | Japan | B |
| 4 | Paul | Poland | B |
+----+--------+--------+-------+
+----+--------+---------+-------+----+--------+--------+-------+
| id | name | nation | table | id | name | nation | table |
+----+--------+---------+-------+----+--------+--------+-------+
| 1 | Albert | Germany | A | 2 | Albert | France | B |
| 3 | Paul | Spain | A | 4 | Paul | Poland | B |
+----+--------+---------+-------+----+--------+--------+-------+

+----+-----------+---------+-------+------+--------+--------+-------+
| id | name | nation | table | id | name | nation | table |
+----+-----------+---------+-------+------+--------+--------+-------+
| 1 | Albert | Germany | A | 2 | Albert | France | B |
| 2 | Elizabeth | England | A | NULL | NULL | NULL | NULL |
| 3 | Paul | Spain | A | 4 | Paul | Poland | B |
| 4 | Julia | Denmark | A | NULL | NULL | NULL | NULL |
+----+-----------+---------+-------+------+--------+--------+-------+

-> UNION
-> select * from TableA right join TableB on TableA.name = TableB.name;
| id | name | nation | table | id | name | nation | table |
+------+-----------+---------+-------+------+--------+--------+-------+
| 1 | Albert | Germany | A | 2 | Albert | France | B |
| 2 | Elizabeth | England | A | NULL | NULL | NULL | NULL |
| 3 | Paul | Spain | A | 4 | Paul | Poland | B |
| 4 | Julia | Denmark | A | NULL | NULL | NULL | NULL |
| NULL | NULL | NULL | NULL | 1 | Marc | China | B |
| NULL | NULL | NULL | NULL | 3 | Peter | Japan | B |
+------+-----------+---------+-------+------+--------+--------+-------+
+------+-----------+---------+-------+------+--------+--------+-------+
| id | name | nation | table | id | name | nation | table |
+------+-----------+---------+-------+------+--------+--------+-------+
| 1 | Albert | Germany | A | 2 | Albert | France | B |
| 2 | Elizabeth | England | A | NULL | NULL | NULL | NULL |
| 3 | Paul | Spain | A | 4 | Paul | Poland | B |
| 4 | Julia | Denmark | A | NULL | NULL | NULL | NULL |
| NULL | NULL | NULL | NULL | 1 | Marc | China | B |
| 1 | Albert | Germany | A | 2 | Albert | France | B |
| NULL | NULL | NULL | NULL | 3 | Peter | Japan | B |
| 3 | Paul | Spain | A | 4 | Paul | Poland | B |
+------+-----------+---------+-------+------+--------+--------+-------+

+----+-----------+---------+-------+----+--------+--------+-------+
| id | name | nation | table | id | name | nation | table |
+----+-----------+---------+-------+----+--------+--------+-------+
| 1 | Albert | Germany | A | 1 | Marc | China | B |
| 2 | Elizabeth | England | A | 1 | Marc | China | B |
| 3 | Paul | Spain | A | 1 | Marc | China | B |
| 4 | Julia | Denmark | A | 1 | Marc | China | B |
| 1 | Albert | Germany | A | 2 | Albert | France | B |
| 2 | Elizabeth | England | A | 2 | Albert | France | B |
| 3 | Paul | Spain | A | 2 | Albert | France | B |
| 4 | Julia | Denmark | A | 2 | Albert | France | B |
| 1 | Albert | Germany | A | 3 | Peter | Japan | B |
| 2 | Elizabeth | England | A | 3 | Peter | Japan | B |
| 3 | Paul | Spain | A | 3 | Peter | Japan | B |
| 4 | Julia | Denmark | A | 3 | Peter | Japan | B |
| 1 | Albert | Germany | A | 4 | Paul | Poland | B |
| 2 | Elizabeth | England | A | 4 | Paul | Poland | B |
| 3 | Paul | Spain | A | 4 | Paul | Poland | B |
| 4 | Julia | Denmark | A | 4 | Paul | Poland | B |
+----+-----------+---------+-------+----+--------+--------+-------+
-> WHERE TableB.id is null;
+----+-----------+---------+-------+------+------+--------+-------+
| id | name | nation | table | id | name | nation | table |
+----+-----------+---------+-------+------+------+--------+-------+
| 2 | Elizabeth | England | A | NULL | NULL | NULL | NULL |
| 4 | Julia | Denmark | A | NULL | NULL | NULL | NULL |
+----+-----------+---------+-------+------+------+--------+-------+

-> UNION
-> select * from TableA right join TableB on TableA.name = TableB.name where TableA.id is null;
+------+-----------+---------+-------+------+-------+--------+-------+
| id | name | nation | table | id | name | nation | table |
+------+-----------+---------+-------+------+-------+--------+-------+
| 2 | Elizabeth | England | A | NULL | NULL | NULL | NULL |
| 4 | Julia | Denmark | A | NULL | NULL | NULL | NULL |
| NULL | NULL | NULL | NULL | 1 | Marc | China | B |
| NULL | NULL | NULL | NULL | 3 | Peter | Japan | B |
+------+-----------+---------+-------+------+-------+--------+-------+


