Select Outer Join (Left, Right)
By: Student Kim
Tags: MySQL

Outer Join — selecting unmatched tuples from different tables.

Hey y’all! Continue from the last time, I’ll talk about the outer join today. And I’ll give you some basic practice as well like the last time. Let’s do this guys!

[Outer Join]

Like I told you on last time, Inner Join is the clause that shows you combined tuples that have same value of certain columns from different tables. Outer join shows tuples that inner joined + that haven’t inner joined. There are two type of Outer Join, Left and right.

(1) Left Outer Join

To help you understand it better, I will show you the query first.

select table1.column1, table2.column2

from table1 left outer join table2

on table1.column=table2.column;

So here, left means you’re selecting unmatched tuples from table1, because it’s on the left side from left outer join . It’s ok if you don’t get it completely. I’ll show you some examples at the end.

(2) Right Outer Join

The query is just same as left join but this time you write right instead.

select table1.column1, table2.column2

from table1 right outer join table2

on table1.column=table2.column;

Then it means you’re selecting table2, cause it’s on the right side of the right outer join .

[Exercise Questions]

Just like the last time, I’ll make a shop that has member and member’s car information.

create table member(

id int primary key,

name varchar(8),

addr varchar(7),

point int

); create table car(

id int,

carNum varchar(4),

foreign key(id) references member(id)

); insert into member values

(1,'Nina','seoul',100),

(2,'Tom','seoul',200),

(3,'Amy','busan',400),

(4,'Jake','naju',800),

(5,'Lisa','incheon',500); insert into car values

(1,'1111'),

(2,'2222'),

(3,'3333'),

(1,'4444'),

(3,'5555');

Q1. Select each member’s id, name, car number, and also show the members who don’t have car.

Answer : mysql> select m.id, m.name, c.carNum

-> from member m

-> left outer join car c

-> on m.id=c.id;

+----+------+--------+

| id | name | carNum |

+----+------+--------+

| 1 | Nina | 1111 |

| 1 | Nina | 4444 |

| 2 | Tom | 2222 |

| 3 | Amy | 3333 |

| 3 | Amy | 5555 |

| 4 | Jake | NULL |

| 5 | Lisa | NULL |

+----+------+--------+

7 rows in set (0.00 sec)

I used the left outer join, because I want to choose people who don’t have car from the member table. And I put the nicknames m for member and c for car like the last time. If you’re not familiar with this concept, checkout my last post!

But wait, by using outer join you can’t select the people who don’t have car only. Then what should we do for that?

Q2. Select id, name and the carNum column as well of member who doesn’t have car only.

This is when you need to use the where clause.

mysql> select m.id, m.name, c.carNum

-> from member m

-> left outer join car c

-> on m.id=c.id

-> where c.id is null;

+----+------+--------+

| id | name | carNum |

+----+------+--------+

| 4 | Jake | NULL |

| 5 | Lisa | NULL |

+----+------+--------+

2 rows in set (0.00 sec)

You see I put c.id is null , cause people who don’t have car, are not on the car table at all. So their information must be null on the car table.

Alrighty, it was bit more complicated than the inner join, but I’m sure you could do this. From the next post I’ll bring some more clause that you can use on select statement. Thank you for reading guys!