--部门表

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

对两个结果集进行“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

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查询(子查询,全连接,等值连接,自然连接,左右连,交集,并集,差集)的更多相关文章

  1. Python-select 关键字 多表查询 子查询

    sql 最核心的查询语句!!!! 增删改 单表查询 select语句的完整写法 关键字的书写顺序 执行顺序 多表查询 笛卡尔积 内连接 左外连接 右外连接 全外连接 通过合并左外连接和右外连接 子查询 ...

  2. 【知识库】-数据库_MySQL之基本数据查询:子查询、分组查询、模糊查询

    简书作者:seay 文章出处: 关系数据库SQL之基本数据查询:子查询.分组查询.模糊查询 回顾:[知识库]-数据库_MySQL常用SQL语句语法大全示例 Learn [已经过测试校验] 一.简单查询 ...

  3. oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by

    select语句学习 . 创建表 create table user(user varchar2(20), id int); . 查看执行某条命令花费的时间 set timing on: . 查看表的 ...

  4. 【数据库】SQL经典面试题 - 数据库查询 - 子查询应用二

    上节课我们通过子查询,完成了查询的最高分学生的需求,今天我们来学习子查询的分类,以及通过子查询来完成工作中经常遇到一些个性化需求. 子查询概念: 一个SELECT语句嵌套在另一个SELECT语句中,子 ...

  5. ylb: SQL表的高级查询-子查询

    ylbtech-SQL Server: SQL Server- SQL表的高级查询-子查询 SQL Server 表的高级查询-子查询. 1,ylb:表的高级查询-子查询返回顶部 --======== ...

  6. MYSQL 查询方法 统计查询 链接查询 子查询

    mysql表格查询方法: 查询: 1.简单查询 select * from Info --查所有数据select Code,Name from Info --查指定列的数据select Code as ...

  7. MySQL的查询,子查询,联结查询,联合查询

    MySQL的查询,子查询,联结查询,联合查询 一.mysql查询的五种子句where(条件查询).having(筛选).group by(分组).order by(排序).limit(限制结果数) 二 ...

  8. Oracle的查询-子查询

    --子查询 --子查询返回一个值 --查询出工资和scott一样的员工信息 select * from emp where sal in (select sal from emp where enam ...

  9. coding++:mybatis 嵌套查询子查询column传多个参数描述

    mybatis 嵌套查询子查询column传多个参数如下: 2.代码示例 备注:注意,相同颜色的单词都是有关联的 <resultMap id="blogResult" typ ...

  10. sql ,内连接,外连接,自然连接等各种连接

    1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等联接和自然联接. 内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 students和c ...

随机推荐

  1. SpringBoot系列之@Value和@ConfigurationProperties

    继上一篇博客SpringBoot系列之YAML配置用法之后,再写一篇@Value.@ConfigurationProperties的对比博客 这两个主键都是可以获取配置文件属性的,不过是有比较大的区别 ...

  2. 持续集成学习6 jenkins自动化代码构建

    一.实验目标 二.配置 1.配置mvn构建 [root@node1 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3 ...

  3. 【JAVA】可视化计算器

    import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.Actio ...

  4. python全局变量及局部变量

    变量作用域 全局变量(global):在函数外部定义,在整个全局范围都有效 局部变量(local) 在函数内部定义,局部变量在局部范围内使用 数字,字符串,元组,修改其变量值时需要加globle,列表 ...

  5. 深度解密Go语言之 pprof

    目录 什么是 pprof pprof 的作用 pprof 如何使用 runtime/pprof net/http/pprof pprof 进阶 Russ Cox 实战 查找内存泄露 总结 参考资料 相 ...

  6. [专题总结]AC自动机

    其实前面的模板也不是1A,我在题库里提前做过,也不必在意罚时,刚开始我在做别的专题 裸模板我就不说了,各个博客讲解的很明白 void insert(string s){ ,len=s.size(); ...

  7. Function题解

    这个题最优策略一定是向左上走到某一列再往上一直走. n*q在线暴力可做,离线按y排序,单调栈维护凸壳. 具体来说:对于i<j若A[i]>A[j] 即j的斜率小而且纵截距小,一定比i优,并且 ...

  8. 原生JS实现集合结构

    1. 前言 集合是由一组无序且唯一(即不能重复)的项组成的.你可以把集合想象成一个既没有重复元素,也没有顺序概念的数组.在ES6中已经内置了集合这一数据结构--Set.接下来,我们就用原生JS来实现这 ...

  9. P3043 [USACO12JAN]牛联盟(并查集+数学)

    (m<n<=1e5,有重边) 题目表述有问题..... 给定一张图(不一定联通),每条边可以选择连接的两个点之一,剩余的点可以自己成对,问方案数. 一开始是真的被吓到了....觉得可写性极 ...

  10. 演示vsftpd服务匿名访问模式、本地用户模式的配置

    文件传输协议(FTP,File Transfer Protocol) 即能够让用户在互联网中上传.下载文件的文件协议,而FTP服务器就是支持FTP传输协议的主机,要想完成文件传输则需要FTP服务端和F ...