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 ...
随机推荐
- Java 大小写转换
Java 大小写转换 public class CaseConversion { /** * @param character: a character * @return: a character ...
- Http 请求 GET和POST的区别
GET和POST还有一个重大区别,简单的说: GET产生一个TCP数据包;POST产生两个TCP数据包. 长的说: 对于GET方式的请求,浏览器会把http header和data一并发送出去,服务器 ...
- thinkphp5.0模块设计
5.0版本对模块的功能做了灵活设计,默认采用多模块的架构,并且支持单一模块设计,所有模块的命名空间均以app作为根命名空间(可配置更改). 目录结构 标准的应用和模块目录结构如下: ├─applica ...
- crontab定时运行python脚本访问MySQL遇到问题
最近写了一个python脚本来定时备份MySQL数据库.具体实现如下: 1)python脚本中使用os.system("mysqldump -h127.0.0.1 -uroot -ppass ...
- CodeForces - 1009D Relatively Prime Graph
题面在这里! 直接暴力找点对就行了,可以证明gcd=1是比较密集的,所以复杂度略大于 O(N log N) #include<bits/stdc++.h> #define ll long ...
- Flask请求上下文源码讲解,简单的群聊单聊web
请求上下文流程图 群聊html代码 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- JxBrowser概述与简单应用
Q:JxBrowser是什么? JxBrowser是一个跨平台的Java库,允许将基于Google Chromium的Web浏览器组件集成到Java Swing / AWT / JavaFX应用程序中 ...
- Ajax 跨域问题(JSONP && Access-Control-Allow-Origin)
1.使用jsonp跨域请求 2.通过设置服务端头部跨域请求 3.设置nginx/apach 使用jsonp跨域请求 什么是Jsonp JSONP(JSON with Padding)是一个非官方的协议 ...
- MySQL -- 性能优化的最佳20+条经验
FROM:http://www.cnblogs.com/shlhm/p/3235848.html ,学习学习~ 今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库 ...
- hdu 5210 delete 水题
Delete Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5210 D ...