SQL OUTER JOIN
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的更多相关文章
- SQL中inner join、outer join和cross join的区别
对于SQL中inner join.outer join和cross join的区别简介:现有两张表,Table A 是左边的表.Table B 是右边的表.其各有四条记录,其中有两条记录name是相同 ...
- 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 ...
- 图解SQL的inner join、left join、right join、full outer join、union、union all的区别
SQL的Join语法有很多,inner join(等值连接) 只返回两个表中联结字段相等的行,left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录,right join(右 ...
- LINQ to SQL系列四 使用inner join,outer join
先看一个最简单的inner join,在读取Student表时inner join Class表取的对应的Class信息: static void Main(string[] args) { usin ...
- 图解SQL的inner join(join)、left join、right join、full outer join、union、union all的区别
对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...
- SQL 查询条件放在LEFT OUTER JOIN 的ON语句后与放在WHERE中的区别
这两种条件放置的位置不同很容易让人造成混淆,以致经常查询出莫名其妙的结果出来,特别是副本的条件与主表不匹配时,下面以A,B表为例简单说下我的理解. 首先要明白的是: 跟在ON 后面的条件是对参与左联接 ...
- SQL的JOIN语法解析(inner join, left join, right join, full outer join的区别)
原文链接:http://www.powerxing.com/sql-join/ 总的来说,四种JOIN的使用/区别可以描述为: left join 会从左表(shop)那里返回所有的记录,即使在右表( ...
- 对于SQL的Join,在学习起来可能是比较乱的。我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚。Coding Horror上有一篇文章,通过文氏图 Venn diagrams 解释了SQL的Join。我觉得清楚易懂,转过来。
对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codi ...
- 【转载】SQL中inner join、outer join和cross join的区别
对于SQL中inner join.outer join和cross join的区别很多人不知道,我也是别人问起,才查找资料看了下,跟自己之前的认识差不多, 如果你使用join连表,缺陷的情况下是inn ...
随机推荐
- Python中__new__()方法的使用和实例化
new()是在新式类中新出现的方法,它作用在构造方法init()建造实例之前,可以这么理解,在Python 中存在于类里面的构造方法init()负责将类的实例化,而在init()调用之前,new()决 ...
- Topcoder Srm 726 Div1 Hard
Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...
- POJ 3553 Light Switching Game 博弈论 nim积 sg函数
http://poj.org/problem?id=3533 变成三维的nim积..前面hdu那个算二维nim积的题的函数都不用改,多nim积一次就过了...longlong似乎不必要但是还是加上了 ...
- hdu 1011 树形dp+背包
题意:有n个房间结构可看成一棵树,有m个士兵,从1号房间开始让士兵向相邻的房间出发,每个房间有一定的敌人,每个士兵可以对抗20个敌人,士兵在某个房间对抗敌人使无法走开,同时有一个价值,问你花费这m个士 ...
- 执行时间 Exercise07_16
import java.util.Arrays; import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:执行时间 * */ pu ...
- hdu 1711
读入优化有3s多. #include <cstdio> #include <cctype> #define maxn 1000010 #define maxm 10010 in ...
- 线段树--codevs 1690 开关灯
codevs 1690 开关灯 USACO 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description YYX家门前的街上有N(2& ...
- scp使用笔记
yum install openssh-clients 就能使用了 上传 microgolds-prodeMacBook-Pro:Desktop mg$ sudo scp /Users/mg/Desk ...
- php uncode 转汉字编码
$test = "%u4E0A%u6D77%u9EC4%u6D66";//$test = '\u5e86\u91cd\u5e86'; //庆重庆$temp = explode('% ...
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) C. Little Artem and Matrix 模拟
C. Little Artem and Matrix 题目连接: http://www.codeforces.com/contest/669/problem/C Description Little ...