SQL: difference between inner and outer join
2011-01-18 15:04
295 查看
Assuming you're joining on columns with no duplicates, which is by far the most common case:
An inner join of A and B gives the result of A intersect B, i.e. the inner part of a venn diagram intersection.
An outer join of A and B gives the results of A union B, i.e. the outer parts of a venn diagram union.
Examples
Suppose you have two Tables, with a single column each, and data as follows:
inner join
An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common.
A left outer join will give all rows in A, plus any common rows in B.
A full outer join will give you the union of A and B, i.e. All the rows in A and all the rows in B. If something in A doesn't have a corresponding datum in B, then the B portion is null, and vice versa.
An inner join of A and B gives the result of A intersect B, i.e. the inner part of a venn diagram intersection.
An outer join of A and B gives the results of A union B, i.e. the outer parts of a venn diagram union.
Examples
Suppose you have two Tables, with a single column each, and data as follows:
A B - - 1 3 2 4 3 5 4 6Note that (1,2) are unique to A, (3,4) are common, and (5,6) are unique to B.
inner join
An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common.
select * from a INNER JOIN b on a.a = b.b; select a.*,b.* from a,b where a.a = b.b; a | b --+-- 3 | 3 4 | 4left outer join
A left outer join will give all rows in A, plus any common rows in B.
select * from a LEFT OUTER JOIN b on a.a = b.b; select a.*,b.* from a,b where a.a = b.b(+); a | b --+----- 1 | null 2 | null 3 | 3 4 | 4full outer join
A full outer join will give you the union of A and B, i.e. All the rows in A and all the rows in B. If something in A doesn't have a corresponding datum in B, then the B portion is null, and vice versa.
select * from a FULL OUTER JOIN b on a.a = b.b; a | b -----+----- 1 | null 2 | null 3 | 3 4 | 4 null | 6 null | 5
相关文章推荐
- what is the difference between inner join and outer join
- Difference between natural join and inner join
- In SQL, what is the difference between a left join and a left outer join?
- In SQL, what’s the difference between a full join and an inner join?
- What is the difference between left join and left outer join?
- Difference between INNER and OUTER joins?
- SQL inner join and outer join 祥解
- SQL中的JOIN类型解释(CROSS, INNER,OUTER),关键字ON,USING
- Mixing inner join and outer join
- 【SQL】 left join、right join and inner join的区别
- PostgreSQL difference between filter and join filter
- 【sql调优之执行计划】nested loops join and nested loop join outer
- Difference between LINQ to SQL and LINQ to Entity(DataContext and DbContext)
- sql内连接,等值连接,左连接和右连接的使用技巧(inner join and left join and right join)
- Sql 中having 和where的区别 SQL hardest question What is the difference between the WHERE and HAVING claus
- 图解SQL inner join、left join、right join、full outer join、union、union all的区别
- 对于SQL的Join,在学习起来可能是比较乱的。我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚。Coding Horror上有一篇文章,通过文氏图 Venn diagrams 解释了SQL的Join。我觉得清楚易懂,转过来。 交差集" cross join 《Linux命令行与shell脚本编程大全
- What is the difference between LINQ to SQL and LINQ to Entities?
- The difference between join and union
- 图解SQL inner join、left join、right join、full outer join、union、union all的区别