Examples of SQL join types (LEFT JOIN, INNER JOIN etc.)
April 26, 2018 [SQL, Tech]I have 2 tables like this:
> SELECT * FROM table_a; +------+------+ | id | name | +------+------+ | 1 | row1 | | 2 | row2 | +------+------+ > SELECT * FROM table_b; +------+------+------+ | id | name | aid | +------+------+------+ | 3 | row3 | 1 | | 4 | row4 | 1 | | 5 | row5 | NULL | +------+------+------+
INNER JOIN cares about both tables
INNER JOIN cares about both tables, so you only get a row if both tables have one. If there is more than one matching pair, you get multiple rows.
> SELECT * FROM table_a a INNER JOIN table_b b ON a.id=b.aid; +------+------+------+------+------+ | id | name | id | name | aid | +------+------+------+------+------+ | 1 | row1 | 3 | row3 | 1 | | 1 | row1 | 4 | row4 | 1 | +------+------+------+------+------+
It makes no difference to INNER JOIN if you reverse the order, because it cares about both tables:
> SELECT * FROM table_b b INNER JOIN table_a a ON a.id=b.aid; +------+------+------+------+------+ | id | name | aid | id | name | +------+------+------+------+------+ | 3 | row3 | 1 | 1 | row1 | | 4 | row4 | 1 | 1 | row1 | +------+------+------+------+------+
You get the same rows, but the columns are in a different order because we mentioned the tables in a different order.
LEFT JOIN only cares about the first table
LEFT JOIN cares about the first table you give it, and doesn't care much about the second, so you always get the rows from the first table, even if there is no corresponding row in the second:
> SELECT * FROM table_a a LEFT JOIN table_b b ON a.id=b.aid; +------+------+------+------+------+ | id | name | id | name | aid | +------+------+------+------+------+ | 1 | row1 | 3 | row3 | 1 | | 1 | row1 | 4 | row4 | 1 | | 2 | row2 | NULL | NULL | NULL | +------+------+------+------+------+
Above you can see all rows of table_a even though some of them do not match with anything in table b, but not all rows of table_b - only ones that match something in table_a.
If we reverse the order of the tables, LEFT JOIN behaves differently:
> SELECT * FROM table_b b LEFT JOIN table_a a ON a.id=b.aid; +------+------+------+------+------+ | id | name | aid | id | name | +------+------+------+------+------+ | 3 | row3 | 1 | 1 | row1 | | 4 | row4 | 1 | 1 | row1 | | 5 | row5 | NULL | NULL | NULL | +------+------+------+------+------+
Now we get all rows of table_b, but only matching rows of table_a.
RIGHT JOIN only cares about the second table
a RIGHT JOIN b gets you exactly the same rows as b LEFT JOIN a. The only difference is the default order of the columns.
> SELECT * FROM table_a a RIGHT JOIN table_b b ON a.id=b.aid; +------+------+------+------+------+ | id | name | id | name | aid | +------+------+------+------+------+ | 1 | row1 | 3 | row3 | 1 | | 1 | row1 | 4 | row4 | 1 | | NULL | NULL | 5 | row5 | NULL | +------+------+------+------+------+
This is the same rows as table_b LEFT JOIN table_a, which we saw in the LEFT JOIN section.
Similarly:
> SELECT * FROM table_b b RIGHT JOIN table_a a ON a.id=b.aid; +------+------+------+------+------+ | id | name | aid | id | name | +------+------+------+------+------+ | 3 | row3 | 1 | 1 | row1 | | 4 | row4 | 1 | 1 | row1 | | NULL | NULL | NULL | 2 | row2 | +------+------+------+------+------+
Is the same rows as table_a LEFT JOIN table_b.
No join at all gives you copies of everything
If you write your tables with no JOIN clause at all, just separated by commas, you get every row of the first table written next to every row of the second table, in every possible combination:
> SELECT * FROM table_b b, table_a; +------+------+------+------+------+ | id | name | aid | id | name | +------+------+------+------+------+ | 3 | row3 | 1 | 1 | row1 | | 3 | row3 | 1 | 2 | row2 | | 4 | row4 | 1 | 1 | row1 | | 4 | row4 | 1 | 2 | row2 | | 5 | row5 | NULL | 1 | row1 | | 5 | row5 | NULL | 2 | row2 | +------+------+------+------+------+