Ever get confused by the difference between an INNER JOIN or an OUTER JOIN?
How about a LEFT OUTER JOIN, a RIGHT OUTER JOIN or a FULL OUTER JOIN?
Here's a simple set of tables and data to help you understand!
create table employee
(employee_id int
,employee_name varchar(30)
,department_id int
)
create table department
(department_id int
,department_name varchar(30)
)
Note the complete lack of referential integrity in the table definitions. :(
insert into department values (1,'Crew')
insert into department values (2,'Passengers')
insert into department values (3,'Rescuers')
insert into employee values (1,'Gilligan',1)
insert into employee values (2,'Skipper',1)
insert into employee values (3,'Mary Ann',2)
insert into employee values (4,'Ginger',2)
insert into employee values (5,'Surfer',4)
Note that no employees are in department 3, Rescuers.
Note that there is no department 4.
Here come some sample queries:
This will miss departments 3 and 4, plus the Surfer:
select
e.employee_id, e.employee_name, e.department_id
,d.department_id, d.department_name
from employee e
inner join department d on e.department_id = d.department_id
1 Gilligan 1 1 Crew
2 Skipper 1 1 Crew
3 Mary Ann 2 2 Passengers
4 Ginger 2 2 Passengers
This will miss Department 3.
select
e.employee_id, e.employee_name, e.department_id
,d.department_id, d.department_name
from employee e
left outer join department d on e.department_id = d.department_id
1 Gilligan 1 1 Crew
2 Skipper 1 1 Crew
3 Mary Ann 2 2 Passengers
4 Ginger 2 2 Passengers
5 Surfer 4 NULL NULL
This will miss the Surfer (and therefore Department 4).
select
e.employee_id, e.employee_name, e.department_id
,d.department_id, d.department_name
from employee e
right outer join department d on e.department_id = d.department_id
1 Gilligan 1 1 Crew
2 Skipper 1 1 Crew
3 Mary Ann 2 2 Passengers
4 Ginger 2 2 Passengers
NULL NULL NULL 3 Rescuers
This will miss nothing.
select
e.employee_id, e.employee_name, e.department_id
,d.department_id, d.department_name
from employee e
full outer join department d on e.department_id = d.department_id
1 Gilligan 1 1 Crew
2 Skipper 1 1 Crew
3 Mary Ann 2 2 Passengers
4 Ginger 2 2 Passengers
5 Surfer 4 NULL NULL
NULL NULL NULL 3 Rescuers
This will miss everything but Department 4 and the Surfer:
select
e.employee_id, e.employee_name, e.department_id
,d.department_id, d.department_name
from employee e
left outer join department d on e.department_id = d.department_id
where d.department_id is null
5 Surfer 4 NULL NULL
This kind of experiment only takes a few minutes to do. With a bit of care in creating reasonable test data (taking into account things that can be null or the possibility of bad data), it's often very easy to prove that you are (or just as important, are not!) getting the correct results with your test queries.
If you are still stuck on how to write the statement you need, here are a couple of important points to consider when you ask for help:
- Include sample create table statements. That way, we don't have to guess. :)
- For best results, include a set of insert statements that load sample data.
- Specify which rows in the sample output are the ones that should be returned from the query
-
Give us your best shot. We're all much more likely to help you out if we see you've been doing some work! Otherwise, we might mistake you for a lazy college student trying to get us to do their homework for them - and we don't like to do that. (That's because they graduate, get a job with our company, and then we have to do even more of their work!)
The above process removes huge amounts of ambiguity and makes it very fast for someone to help out. ;)
And for you to be sure they were right!
If this answered your question, be sure to mark it as the answer. That way, everybody after you will know it's the answer also!