When we want to select out all the record from two table, no matter it's present at second table or not, we will have to use SQL OUTER JOIN command.

There are 3 type of OUTER JOIN, which is:
LEFT OUTER JOIN
RIGHT OUTER JOIN
FULL OUTER JOIN

SQL OUTER JOIN syntax:

SELECT *
FROM [TABLE 1] OUTER JOIN [TABLE 2]
ON [TABLE 1].[COLUMN NAME 1] = [TABLE 2].[COLUMN NAME 2]

EXAMPLE :

Let's say we got 2 tables containt data like Below:

Table 1: GameScores

PlayerName DepartmentId Scores
Jason 1 3000
Irene 1 1500
Jane 2 1000
David 2 2500
Paul 3 2000
James 4 2000

Table 2: Departments

DepartmentId DepartmentName
1 IT
2 Marketing
3 HR
5 QA

LEFT OUTER JOIN will return all records from the first table,
SQL statement and Result for LEFT OUTER JOIN is :

SELECT * FROM GameScores2
LEFT OUTER JOIN Departments
ON GameScores2.DepartmentId = Departments.DepartmentId

Result:

PlayerName DepartmentId Scores DepartmentId DepartmentName
Jason 1 3000 1 IT
Irene 1 3000 1 IT
Jane 2 3000 2 Marketing
David 2 3000 2 Marketing
Paul 3 3000 3 HR
James 4 2000 NULL NULL

RIGHT OUTER JOIN will return all records from the second table,
SQL statement and Result for RIGHT OUTER JOIN is :

SELECT * FROM GameScores2
RIGHT OUTER JOIN Departments
ON GameScores2.DepartmentId = Departments.DepartmentId

Result:

PlayerName DepartmentId Scores DepartmentId DepartmentName
Jason 1 3000 1 IT
Irene 1 3000 1 IT
Jane 2 3000 2 Marketing
David 2 3000 2 Marketing
Paul 3 3000 3 HR
NULL NULL NULL 5 QA

FULL OUTER JOIN will return all records from all tables,
SQL statement and Result for FULL OUTER JOIN is :

SELECT * FROM GameScores2
FULL OUTER JOIN Departments
ON GameScores2.DepartmentId = Departments.DepartmentId

Result:

PlayerName DepartmentId Scores DepartmentId DepartmentName
Jason 1 3000 1 IT
Irene 1 3000 1 IT
Jane 2 3000 2 Marketing
David 2 3000 2 Marketing
Paul 3 3000 3 HR
James 4 2000 NULL NULL
NULL NULL NULL 5 QA

SQL OUTER JOIN的更多相关文章

  1. SQL中inner join、outer join和cross join的区别

    对于SQL中inner join.outer join和cross join的区别简介:现有两张表,Table A 是左边的表.Table B 是右边的表.其各有四条记录,其中有两条记录name是相同 ...

  2. SQL的inner join、left join、right join、full outer join、union、union all

    主题: SQL的inner join.left join.right join.full outer join.union.union all的学习. Table A和Table B表如下所示: 表A ...

  3. 图解SQL的inner join、left join、right join、full outer join、union、union all的区别

    SQL的Join语法有很多,inner join(等值连接) 只返回两个表中联结字段相等的行,left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录,right join(右 ...

  4. LINQ to SQL系列四 使用inner join,outer join

    先看一个最简单的inner join,在读取Student表时inner join Class表取的对应的Class信息: static void Main(string[] args) { usin ...

  5. 图解SQL的inner join(join)、left join、right join、full outer join、union、union all的区别

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...

  6. SQL 查询条件放在LEFT OUTER JOIN 的ON语句后与放在WHERE中的区别

    这两种条件放置的位置不同很容易让人造成混淆,以致经常查询出莫名其妙的结果出来,特别是副本的条件与主表不匹配时,下面以A,B表为例简单说下我的理解. 首先要明白的是: 跟在ON 后面的条件是对参与左联接 ...

  7. SQL的JOIN语法解析(inner join, left join, right join, full outer join的区别)

    原文链接:http://www.powerxing.com/sql-join/ 总的来说,四种JOIN的使用/区别可以描述为: left join 会从左表(shop)那里返回所有的记录,即使在右表( ...

  8. 对于SQL的Join,在学习起来可能是比较乱的。我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚。Coding Horror上有一篇文章,通过文氏图 Venn diagrams 解释了SQL的Join。我觉得清楚易懂,转过来。

     对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codi ...

  9. 【转载】SQL中inner join、outer join和cross join的区别

    对于SQL中inner join.outer join和cross join的区别很多人不知道,我也是别人问起,才查找资料看了下,跟自己之前的认识差不多, 如果你使用join连表,缺陷的情况下是inn ...

随机推荐

  1. linux java环境变量设置

    下载JRE或者JDK后解压,设置以下环境变量 JAVA_HOME=/home/zm/jdk1.8.0_181JRE_HOME=/home/zm/jdk1.8.0_181/jreCLASSPATH=.: ...

  2. 最新JAVA编程题全集(50题及答案)

    [程序1]题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?    //这是一个菲波拉契数列问题 pu ...

  3. 牛客练习赛9 B - 珂朵莉的值域连续段

    题目描述 珂朵莉给你一个有根树,求有多少个子树满足其内部节点编号在值域上连续 一些数在值域上连续的意思即其在值域上构成一个连续的区间 输入描述: 第一行有一个整数n,表示树的节点数.接下来n–1行,每 ...

  4. 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛 [迭代加深搜索]

    题目传送门 摩天大楼里的奶牛 题目描述 A little known fact about Bessie and friends is that they love stair climbing ra ...

  5. 洛谷P2657 [SCOI2009]windy数 [数位DP,记忆化搜索]

    题目传送门 windy数 题目描述 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B,总共有多少个win ...

  6. Hat's Fibonacci hdu 1250

    Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...

  7. [BZOJ4240]有趣的家庭菜园(贪心+树状数组)

    最后数列一定是单峰的,问题就是最小化最后的位置序列的逆序对数. 从大到小加数,每次贪心看放左边和右边哪个产生的逆序对数更少,树状数组即可. 由于大数放哪对小数不产生影响,所以正确性显然. 注意相同数之 ...

  8. VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland 水题

    B. Making Genome in Berland 题目连接: http://www.codeforces.com/contest/638/problem/B Description Berlan ...

  9. hdu 5206 Four Inages Strategy 判断是否是正方形

    Four Inages Strategy Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem. ...

  10. codeforces 148E Aragorn's Story 背包DP

    Aragorn's Story Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/probl ...