Wednesday, November 9, 2011

Types of Joins

Inner joins: return matched rows in both tables.
select *
from TableA A Table B B
where A.key=B.key;

Outer joins include the left joins, right joins and full joins.
Left joins: return matched rows in both tables and non-matched rows in the left table.
select *
from TableA A left join TableB B
on A.key=B.key;

select *
from TableA A left join TableB B
on A.key=B.key
where B.KEy is NULL;

Right joins: return matched rows in both tables and non-matched rows in the right table.
select *
from TabelA A right join TableB B
on A.key=B.key;

select *
from TabelA A right join TableB B
on A.key=B.key
where A.Key is NULL;

Full joins: return matched, plus all non-matched rows from both tables.
select *
from TableA A full outerjoin TableB B
on A.key=B.key;

select *
from TableA A full outerjoin TableB B
on A.key=B.key
where A.Key is NULL or B.Key is NULL; 

No comments:

Post a Comment

Blog Archive