CDA持证人阿涛哥

2022-10-31   阅读量: 329

Mysql

mysql学习20-列子查询

扫码加入数据分析学习群

-- 34 列子查询:

-- 34.1 查询普通员工(非领导)的工资等级:empno,ename,sal,grade

use test2;

# 1,找出找出领导的员工编号, mgr字段

select distinct mgr from emp where mgr is not null;

# 2,查询全体员工工资等级

select *

from emp left join salgrade

on sal >=losal and sal <= hisal;

# 3 ,前面两步结合

select *

from emp left join salgrade

on sal >=losal and sal <= hisal

where empno not in (select distinct mgr from emp where mgr is not null);


-- 练习, 查询领导的工资等级:empno ,ename,job,mgr,sal,grade

##第1步,找出领导的员工编号

select distinct mgr from emp where mgr is not null;

## 2,查询全体员工工资等级

select *

from emp left join salgrade

on sal >=losal and sal <= hisal;

## 3,把第一步的结果做一个where条件筛选子查询

select empno 领导的员工编号 ,ename 姓名 ,job 职位,mgr 直属领导 ,sal 工资 ,grade 工资等级

from emp left join salgrade

on sal >=losal and sal <= hisal

where empno in (select distinct mgr from emp where mgr is not null);


-- 34.2 查询基本工资高于30号部门任意员工的其他部门员工信息 (高于其中一个即可)(不包含30号部门)

#a=4, 判断 a >any(1,3,5) ,结果true

#a=4, 判断 a >all (1,3,5) ,结果False

#练习

#a=7, 判断 a >any(1,3,7,9) ,结果是

#a=12, 判断 a >all(1,3,7,10),结果是

# 第1步 查询30号部门任意员工的工资,作为一个集合

select sal from emp where deptno=30;

# 第2步,查询所有员工的信息 (含工资信息)

select * from emp ;

# 第3步,前两步合起来

select *

from emp where sal > any(查询30号部门任意员工的工资组成的集合)

and deptno !=30;

-- 练习, 查询基本工资高于30号部门所有员工的其他部门员工信息

#思路1 ,> all(30号部门工资集合)

select * from emp where sal >all(select sal from emp where deptno=30);

#思路2,大于30号部门最高工资

#第1步,查询30号部门最高工资

select max(sal) from emp where deptno=30;

#第2步,查询所有员工工资信息

select * from emp;

#第3步,前两步结合, where sal > 查询30号部门最高工资(子查询)

select * from emp

where sal >(select max(sal) from emp where deptno=30)

and deptno !=30;


添加CDA认证专家【维克多阿涛】,微信号:【cdashijiazhuang】,提供数据分析指导及CDA考试秘籍。已助千人通过CDA数字化人才认证。欢迎交流,共同成长!
120.0000 1 0 关注作者 收藏

评论(0)


暂无数据

推荐课程