Thursday, March 10, 2011

SQL Outer Join Query

Outer Join Query

SQL OUTER JOIN returns all records from both joined tables even there is no matching record found. There are three types of SQL OUTER JOIN:
  • LEFT OUTER JOIN
  • RIGHT OUTER,
  • FULL OUTER JOIN
In Outer Join, preserved table means that all the rows in the rows in preserved table will be returned from the query results with at least one row. Let's use the following two tables to show the different type of outer joins.



Employee table: employee id and name

Emp_idName
100John
110Mary
111Smith
112Bob
113Jim
114Joan
115Jill

Employee_hours_worked table: daily total hours each employee worked.

Emp_idDateTotal_hours_worked
12009-12-041
1002010-01-123
1102010-01-125
1132010-01-132
1112010-01-134
1132010-01-136
1102010-01-132
1. Left Outer Join

Left outer join preserve the table in the left side. Every row in the preserved table is presented in the result set with at least one result row.

select e.emp_id,e.name, w.date,w.total_hours_worked
from employee e
left join employee_hours_worked w
on e.emp_id = w.emp_id

emp_idnamedatetotal_hours_worked
100 John 1/12/20103
110Mary1/12/20105
110Mary1/13/20102
111Smith1/13/20104
112bob
113Jim1/12/20102
113Jim1/13/20106
114Joan
114Jill

Put search predicate (filter) in the WHERE clause in a left outer join query will filter out the result set.

select e.emp_id,e.name, w.date,w.total_hours_worked
from employee e
left join employee_hours_worked w
on e.emp_id = w.emp_id
where w.total_hours_worked <=4

emp_idnamedatetotal_hours_worked
110Joan1/12/20103
110Mary1/13/20102
111Smith1/13/20104
113Jim1/12/20102

Put search predicate in the ON clause in a left outer join query will filter out only the matched records in the non preserved table.

select e.emp_id,e.name, w.date,w.total_hours_worked
from pcdb.employee e
left join pcdb.employee_hours_worked w
on e.emp_id = w.emp_id and w.total_hours_worked <=4
emp_id name date total_hours_worked

emp_idnamedatetotal_hours_worked
100john1/12/20103
110Mary1/13/20102
111Smith1/13/20104
112Bob
113Jim1/12/20102
114Joan
114Jill

2 Right Outer Join

In Right outer join, the preserved table is on the right with is opposite to Left outer Join.

select e.emp_id,e.name, w.date,w.total_hours_worked
from pcdb.employee e
right outer join pcdb.employee_hours_worked w
on e.emp_id = w.emp_id

emp_idnamedatetotal_hours_worked
100John1/12/20103
110Mary1/12/20105
113Jim1/12/20102
111Smith1/13/20104
113Jim1/13/20102
110Mary1/13/20102
12/4/20091

Notice that the result set include the record that has not emp_id and name (last record).

3. Full Outer Join

Full Outer Join preserved both tables

select e.emp_id,e.name, w.date,w.total_hours_worked
from pcdb.employee e
full outer join pcdb.employee_hours_worked w
on e.emp_id = w.emp_id

emp_idnamedatetotal_hours_worded
100John1/12/20103
110Mary1/12/20105
110Mary1/13/20102
111Smith1/13/20104
112Bob
113Jim1/12/20102
113Jim1/13/20106
114Joan
115Jim
12/4/20091

If a database server (like MySQL) dose not support full outer join, the query can be written with an union and a where clasue.

select e.emp_id,e.name, w.date,w.total_hours_worked
from pcdb.employee e
left outer join pcdb.employee_hours_worked w
on e.emp_id = w.emp_id
union
select e.emp_id,e.name, w.date,w.total_hours_worked
from pcdb.employee e
right outer join pcdb.employee_hours_worked w
on e.emp_id = w.emp_id
where e.emp_id is null

The where clause in the second query ,'where e.emp_id is null', keep all the the records that only exists in the right table, employee_hours_worked.

Reference


No comments: