两表连接各种Join图示,SQL及查询结果
按:SQL术语Join在中文对应的翻译是“连接”还是“联结”说法不一,下文将统一采用“连接”的译法。
开局一张图:

前奏/准备工作:
Emp表结构:
create table emp(
empid number(4,0),
empname nvarchar2(20),
deptno number(4,0)
)
Emp充值语句:
insert into emp(empid,empname,deptno) values('','Andy','');
insert into emp(empid,empname,deptno) values('','Bill','');
insert into emp(empid,empname,deptno) values('','Cindy','');
insert into emp(empid,empname,deptno) values('','Douglas','');
insert into emp(empid,empname,deptno) values('','张三','');
insert into emp(empid,empname,deptno) values('','李四','');
insert into emp(empid,empname,deptno) values('','王五','');
Dept表结构:
create table dept(
deptid number(4,0),
deptname nvarchar2(20)
)
Dept充值语句:
insert into dept(deptid,deptname) values('','研发');
insert into dept(deptid,deptname) values('','销售');
insert into dept(deptid,deptname) values('','市场');
insert into dept(deptid,deptname) values('','管理');
insert into dept(deptid,deptname) values('','公关');
insert into dept(deptid,deptname) values('','咨询');
正文:
内连接 :

SQL语句:
select emp.*,dept.* from emp inner join dept on emp.deptno=dept.deptid
查询结果:
SQL> select emp.*,dept.* from emp inner join dept on emp.deptno=dept.deptid;
EMPID EMPNAME DEPTNO DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
2 Bill 1 1 研发
4 Douglas 2 2 销售
3 Cindy 2 2 销售
5 张三 4 4 管理
已用时间: 00: 00: 00.01
左连接:

SQL语句:
select emp.*,dept.* from emp left join dept on emp.deptno=dept.deptid
查询结果:
SQL> select emp.*,dept.* from emp left join dept on emp.deptno=dept.deptid;
EMPID EMPNAME DEPTNO DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
2 Bill 1 1 研发
4 Douglas 2 2 销售
3 Cindy 2 2 销售
5 张三 4 4 管理
6 李四 6
7 王五 7
已选择6行。
已用时间: 00: 00: 00.00
右连接:

SQL:
select emp.*,dept.* from emp right join dept on emp.deptno=dept.deptid
查询结果:
SQL> select emp.*,dept.* from emp right join dept on emp.deptno=dept.deptid;
EMPID EMPNAME DEPTNO DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
2 Bill 1 1 研发
3 Cindy 2 2 销售
4 Douglas 2 2 销售
5 张三 4 4 管理
5 公关
8 咨询
3 市场
已选择7行。
已用时间: 00: 00: 00.01
左连接去除内连

SQL:
select emp.*,dept.* from emp left join dept on emp.deptno=dept.deptid where dept.deptid IS NULL
查询结果:
SQL> select emp.*,dept.* from emp left join dept on emp.deptno=dept.deptid where dept.deptid IS NULL;
EMPID EMPNAME DEPTNO DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
6 李四 6
7 王五 7
已用时间: 00: 00: 00.00
右连接去除内连

SQL:
select emp.*,dept.* from emp right join dept on emp.deptno=dept.deptid where emp.deptno IS NULL
查询结果:
SQL> select emp.*,dept.* from emp right join dept on emp.deptno=dept.deptid where emp.deptno IS NULL;
EMPID EMPNAME DEPTNO DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
5 公关
8 咨询
3 市场
已用时间: 00: 00: 00.01
全连接

SQL:
select emp.*,dept.* from emp full join dept on emp.deptno=dept.deptid
查询结果:
SQL> select emp.*,dept.* from emp full join dept on emp.deptno=dept.deptid ;
EMPID EMPNAME DEPTNO DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
2 Bill 1 1 研发
4 Douglas 2 2 销售
3 Cindy 2 2 销售
3 市场
5 张三 4 4 管理
5 公关
8 咨询
6 李四 6
7 王五 7
已选择9行。
已用时间: 00: 00: 00.01
全连接去除内连接,这种查询适合比较两个结果集的差异,具体请见https://www.cnblogs.com/xiandedanteng/p/12239597.html

SQL:
select emp.*,dept.* from emp full join dept on emp.deptno=dept.deptid where emp.deptno IS NULL or dept.deptid IS NULL
查询结果:
SQL> select emp.*,dept.* from emp full join dept on emp.deptno=dept.deptid where emp.deptno IS NULL or dept.deptid IS NULL;
EMPID EMPNAME DEPTNO DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
3 市场
5 公关
8 咨询
6 李四 6
7 王五 7
已用时间: 00: 00: 00.00
全外连接去除内连接

SQL:
select emp.*,dept.* from emp full outer join dept on emp.deptno=dept.deptid where emp.deptno IS NULL or dept.deptid IS NULL
查询结果:
SQL> select emp.*,dept.* from emp full outer join dept on emp.deptno=dept.deptid where emp.deptno IS NULL or dept.deptid IS NULL;
EMPID EMPNAME DEPTNO DEPTID DEPTNAME
---------- ---------------------------------------- ---------- ---------- ----------------------------------------
3 市场
5 公关
8 咨询
6 李四 6
7 王五 7
已用时间: 00: 00: 00.00
IN半连接:

SQL:
select emp.* from emp where emp.deptno in (select deptid from dept)
查询结果:
SQL> select emp.* from emp where emp.deptno in (select deptid from dept);
EMPID EMPNAME DEPTNO
---------- ---------------------------------------- ----------
2 Bill 1
4 Douglas 2
3 Cindy 2
5 张三 4
已用时间: 00: 00: 00.00
EXISTS半连接:

SQL:
select emp.* from emp where exists (select NULL from dept where dept.deptid=emp.deptno)
查询结果:
SQL> select emp.* from emp where exists (select NULL from dept where dept.deptid=emp.deptno);
EMPID EMPNAME DEPTNO
---------- ---------------------------------------- ----------
2 Bill 1
4 Douglas 2
3 Cindy 2
5 张三 4
已用时间: 00: 00: 00.01
IN反连接:

SQL:
select emp.* from emp where emp.deptno not in (select deptid from dept)
查询结果:
SQL> select emp.* from emp where emp.deptno not in (select deptid from dept);
EMPID EMPNAME DEPTNO
---------- ---------------------------------------- ----------
6 李四 6
7 王五 7
已用时间: 00: 00: 00.00
EXISTS反连接:

SQL:
select emp.* from emp where not exists (select NULL from dept where dept.deptid=emp.deptno)
查询结果:
SQL> select emp.* from emp where not exists (select NULL from dept where dept.deptid=emp.deptno);
EMPID EMPNAME DEPTNO
---------- ---------------------------------------- ----------
6 李四 6
7 王五 7
已用时间: 00: 00: 00.00
参考网页:https://docs.oracle.com/cd/E11882_01/server.112/e41084/queries006.htm#SQLRF30046
实验环境:
| # | 类别 | 版本 |
| 1 | 操作系统 | Win10 |
| 2 | 数据库 | Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production |
| 3 | 硬件环境 | T440p |
| 4 | 内存 | 8G |
--2020年2月6日--
两表连接各种Join图示,SQL及查询结果的更多相关文章
- 9、SQL基础整理(两表连接exists,join on,union)
exists的用法 select *from haha where exists (select *from bumen where bumen.code = haha.bumen and bumen ...
- sql表连接left join,right join,inner join三者之间的区别
sql表连接left join,right join,inner join区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 (以左表数据为基准,不足补为NULL) ...
- {MySQL的逻辑查询语句的执行顺序}一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析
MySQL的逻辑查询语句的执行顺序 阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SEL ...
- oracle 表连接 - hash join 哈希连接
一. hash 连接(哈希连接)原理 指的是两个表连接时, 先利用两表中记录较少的表在内存中建立 hash 表, 然后扫描记录较多的表并探測 hash 表, 找出与 hash 表相匹配的行来得到结果集 ...
- hibernate两表连接查询
1.两表的关联关系为一对一 2.库存表Stock与商品信息表Product 3.库存表查询商品表里的商品名称,商品编号 库存表字段: private String id; private ...
- SqlServer 多表连接、聚合函数、模糊查询、分组查询应用总结(回归基础)
--exists 结合 if else 以及 where 条件来使用判断是否有数据满足条件 select * from Class where Name like '%[1-3]班' if (not ...
- sql两表连接
一直以来认为exists比in效率高的说法是不准确的.如果查询的两个表大小相当,那么用in和exists差别不大.如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:例 ...
- 从两表连接看Oracle sql优化器的效果
select emp.*,dept.* from tb_emp03 emp,tb_dept03 dept where emp.deptno=dept.id -- 不加hint SQL> sele ...
- c# DataTable join 两表连接
转:https://www.cnblogs.com/xuxiaona/p/4000344.html JlrInfodt和dtsource是两个datatable,通过[姓名]和[lqry]进行关联 v ...
随机推荐
- MySQL 插入或更新
MySQL DML 记录 数据操纵语言DML(Data Manipulation Language),用户通过它可以实现对数据库的基本操作. 记录一些遇到的语法,以便随时查阅. 插入或更新 ON DU ...
- CentOS7安装MinIO教程,并在C#客户端WPF中实现监控上传进度
MinIO的详细介绍可以参考官网(https://min.io/product/overview). 简单来说它是一个实现了AWS S3标准的100%开源的,可商用的( Apache V2 licen ...
- 微信商户H5支付申请不通过被驳回,拒绝原因提示:网站存在不实内容或不安全信息
一.H5支付简介及使用场景说明 H5支付是指商户在微信客户端外的移动端网页展示商品或服务,用户在前述页面确认使用微信支付时,商户发起本服务呼起微信客户端进行支付.主要用于触屏版的手机浏览器请求微信支付 ...
- gym102586 部分题解
目录 Evacuation Sum Modulo Count Modulo 2 Robots Construct Points Amidakuji Yosupo's Algorithm link 出于 ...
- 学习Hibernate5这一篇就够了
配套资料,免费下载 链接:https://pan.baidu.com/s/1i_RXtOyN1e4MMph6V7ZF-g 提取码:8dw6 复制这段内容后打开百度网盘手机App,操作更方便哦 第一章 ...
- 神经网络实现fashion数据集
import tensorflow as tf import numpy as np fashion=tf.keras.datasets.fashion_mnist (x_train,y_train) ...
- DB2根据报错代码查看表与字段信息
select * from syscat.tables where tbspaceid='?' and tableid='?' select * from syscat.columns where t ...
- 12 种使用 Vue 的最佳做法
随着 VueJS 的使用越来越广泛,出现了几种最佳实践并逐渐成为标准. 1.始终在 v-for 中使用 :key 在需要操纵数据时,将key属性与v-for指令一起使用可以让程序保持恒定且可预测. 这 ...
- 第四周:卷积神经网络 part 3
第四周:卷积神经网络 part 3 视频学习 语义分割中的自注意力机制和低秩重建 语义分割(Semantic Segmentation) 概念:语义分割是在像素级别上的分类,属于同一类的像素都要被归为 ...
- 遍历数组,对象和JSON
遍历数组 var arr2 = [3,4,5,6,7,8]; //第一种方法 for(var i =0;i<arr.length;i++){ console.log(arr2[i]); } // ...