sqlserver查询(子查询,全连接,等值连接,自然连接,左右连,交集,并集,差集)

--部门表

create table dept(

   deptno int primary key,--部门编号

   dname nvarchar(30),--部门名

   loc nvarchar(30)--地址

);

--雇员表

create table emp(

   empno int primary key,--雇员号

   ename nvarchar(30),--员工姓名

   job   nvarchar(30),--雇员工作

   mrg int,--雇员上级

   hiredate datetime,--入职时间

   sal numeric(10,2),--薪水

   comm numeric(10,2),--奖金

   deptno int foreign key references dept(deptno)--设置外键

);

insert into dept values (10,'ACCOUNTING','NEW YORK');

insert into dept values (20,'RESEARCH','DALLAS');

insert into dept values (30 ,'SALES','CHICAGO');

insert into dept values (40, 'OPERATIONS','BOSTON');

insert into emp values (7369,'SMITH','CLERK',7902,'1980-12-17',800.00,null,20);

insert into emp values(7499,'ALLEN','SALESMAN',7698,'1981-2-20',1600.00,300.00,30);

insert into emp values(7521,'WARD','SALESMAN',7698,'1981-2-22',1250.00,500.00,30);

insert into emp values(7566,'JONES','MANAGER',7839,'1981-4-2',2975.00,null,20);

insert into emp values(7654,'MARTIN','SALESMAN',7698,'1981-9-28',1250.00,1400.00,30);

insert into emp values(7698,'BLAKE','MANAGER',7839,'1981-5-1',2850.00,null,30);

insert into emp values(7782,'CLARK','MANAGER',7839,'1981-6-9',2450.00,null,10);

insert into emp values(7788,'SCOTT','ANALYST',7566,'1987-4-19',3000.00,null,20);

insert into emp values(7839,'KING','PRESIDENT',null,'1981-11-17',5000.00,null,10);

insert into emp values(7844,'TURNER','SALESMAN',7698,'1981-9-8',1500.00,0.00,30);

insert into emp values(7876,'ADAMS','CLERK',7788,'1987-5-23',1100.00,null,20);

insert into emp values(7900,'JAMES','CLERK',7698,'1981-12-3',950.00,null,30);

insert into emp values(7902,'FORD','ANALYST',7566,'1981-12-3',3000.00,null,20);

insert into emp values(7934,'MILLER','CLERK',7782,'1982-1-23',1300.00,null,10);

子查询

■什么是子查询

子查询是指嵌入在其它sql语句中的select语句,也叫嵌套查询

单行子查询

单行子查询是指只返回一行数据的子查询语句

请思考:如何显示与SMITH同一部门的所有员工?

select * from emp where deptno=(select deptno from emp where ename='SMITH');

多行子查询

多行子查询指返回多行数据的子查询

请思考:如何查询和部门的工作相同的雇员的名字、岗位、工资、部门号

1,先查询10 号部门有哪些岗位

select distinct job from emp where deptno=10;

2,显示和他的岗位有一个相同的员工

select ename,job,sal,deptno from emp where job in(select distinct job from emp where deptno=10)

全连接

select * from emp,dept;

自然查询

自然连接:将等值连接中的重复列去掉

select student.sno,sname,ssex,sage,sdept,cno,grade from student,sc where student.sno=sc.sno;

左连接和右连接

左连接:left on, 依次遍历左边这个表,查询在右表中是否有对应的记录,如果有对应记录,则匹配,否则显示null

select student.sno,sname,ssex,sage,sdept,cno,grade from student left join sc on(student.sno=sc.sno);

右连接:rigth on,以右边的表为参照

select student.sno,sname,ssex,sage,sdept,cno,grade from student right join sc on(student.sno=sc.sno);

union并集

该操作符用于取得两个结果集的并集。当使用该操作符时,会自动去掉结果集中重复行。

select ename,sal,job from emp where sal>2500

union

select ename,sal,job from emp where job='MANAGER';
select * from student where sage>20

union

select * from student where sage<22

sqlserver查询(子查询,全连接,等值连接,自然连接,左右连,交集,并集,差集)

对两个结果集进行”union”,”intersecrt”,”except”运算这两个结果集的列数必须相同.

intersect交集

使用该操作符用于取得两个结果集的交集。

select ename,sal,job from emp where sal>2500

intersect

select ename,sal,job from emp where job='manager';
select * from student where sage>20

intersect

select * from student where sage<22

sqlserver查询(子查询,全连接,等值连接,自然连接,左右连,交集,并集,差集)

except差集

使用该操作符用于取得两个结果集的差集,它只会显示存在第一个集合中,而不存在第二个集合中的数据。

select ename,sal,job from emp where sal>2500

minus

select ename,sal,job from emp where job='manager';
select * from student where sage>20

except

select * from student where sage>22

sqlserver查询(子查询,全连接,等值连接,自然连接,左右连,交集,并集,差集)

Original: https://www.cnblogs.com/yijieyufu/p/11985996.html
Author: Aquiet
Title: sqlserver查询(子查询,全连接,等值连接,自然连接,左右连,交集,并集,差集)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/581830/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Centos7 离线安装K3s

    1、安装前准备 github地址:https://github.com/k3s-io/k3s/releases k3s二进制文件:k3s下载地址:github地址 / 百度网盘地址…

    Linux 2023年6月7日
    0139
  • shell判断文件是否存在

    shell判断文件,目录是否存在或者具有权限 !/bin/sh 3. myPath=”/var/log/httpd/” myFile=”/var…

    Linux 2023年5月28日
    098
  • 有道云笔记迁移到为知笔记

    背景 &#x4E4B;&#x524D;&#x4E00;&#x76F4;&#x7528;&#x7684;&#x6709;&am…

    Linux 2023年6月14日
    0106
  • shell 同时执行多任务下载视频

    本文为博主原创,转载请注明出处: shell 脚本不支持多线程,但我们需要用shell 脚本同时跑多个任务时怎么让这些任务并发同时进行,可以采用在每个任务 后面 添加一个 &amp…

    Linux 2023年5月28日
    0107
  • 系统初始化

    一般系统安装好后,按照自己习惯定义 csharp;gutter:true;</p> <h1>alias</h1> <p>echo &…

    Linux 2023年6月7日
    057
  • Nginx禁止ip加端口访问

    使用 iptables 限制对应端口,再利用Nginx将80端口转发到对应端口 CentOS7默认的防火墙是 firewalle,先看看服务器中有没有安装 iptables [ro…

    Linux 2023年5月27日
    091
  • Linux安装管理

    Linux系列 包管理工具 单个软件包 管理工具 RedHat系列 Redhat Centos Fedora yum rpm .rpm Debian系列 Ubuntu apt-ge…

    Linux 2023年6月8日
    098
  • 数据库_存储过程简介(oracle版)

    应朋友要求,写个存储过程说明,本篇比较简单,适合新接触存储过程的同学 先来个简单的 begin dbms_output.put_line(‘my first execute’); …

    Linux 2023年6月6日
    089
  • Grafana+Prometheus 搭建 JuiceFS 可视化监控系统

    作为承载海量数据存储的分布式文件系统,用户通常需要直观地了解整个系统的容量、文件数量、CPU 负载、磁盘 IO、缓存等指标的变化。 JuiceFS 没有重复造轮子,而是通过 Pro…

    Linux 2023年6月14日
    090
  • 一文聊透 Netty 核心引擎 Reactor 的运转架构

    本系列Netty源码解析文章基于 4.1.56.Final版本 本文笔者来为大家介绍下Netty的核心引擎Reactor的运转架构,希望通过本文的介绍能够让大家对Reactor是如…

    Linux 2023年6月6日
    090
  • 不可不知的软件架构模式

    什么是系统架构(Architecture) 设计不仅仅指的是外观和感觉,它还包括运作方式。—— 史蒂夫·乔布斯 系统架构(System Architecture),软件架构(Sof…

    Linux 2023年6月14日
    079
  • Linux C/C++ 获取进程号、线程号和设置线程名

    在Linux开发过程中,设计多线程开发时可以将进程和线程的 id 打印出来,方便开发调试和后期查问题使用,同时也包括设置线程名。 2.1 进程ID #include <uni…

    Linux 2023年6月7日
    0126
  • 请求方式

    题目如下 题目描述为请求方式,HTTP的请求方式一共有八种,读者自行去查 打开靶场如下 题目的意思需要以CTF**B为请求方式,由于平台名为CTFHUB,于是试了一下 接着抓包,推…

    Linux 2023年6月7日
    0106
  • Linux 目录挂载服务

    Linux 服务器挂载文件目录通常有三种形式,手动挂载、自动挂载、Autofs 自动挂载,下面对这三个挂载做一下介绍,接受一下这三个区别以及使用场景: 准备服务器和客户端: ser…

    Linux 2023年6月6日
    096
  • Docker搭建Redis Cluster集群及扩容和收容

    上一篇文章讲解了Redis集群原理及搭建,由于工作中使用docker较多,本文主要讲解使用docker搭建集群及对集群的扩展收容。环境:Centos7.6Docker:20.10….

    Linux 2023年6月13日
    079
  • CentOS7.4安装docker

    1、安装环境 此处在Centos7进行安装,可以使用以下命令查看CentOS版本 lsb_release -a 在 CentOS 7安装docker要求系统为64位、系统内核版本为…

    Linux 2023年5月27日
    091
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球