CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Day36-45/code/hrs_with_answer.sql
Views: 729
drop database if exists hrs;1create database hrs default charset utf8mb4;23use hrs;45create table tb_dept6(7dno int not null comment '编号',8dname varchar(10) not null comment '名称',9dloc varchar(20) not null comment '所在地',10primary key (dno)11);1213insert into tb_dept values14(10, '会计部', '北京'),15(20, '研发部', '成都'),16(30, '销售部', '重庆'),17(40, '运维部', '深圳');1819create table tb_emp20(21eno int not null comment '员工编号',22ename varchar(20) not null comment '员工姓名',23job varchar(20) not null comment '员工职位',24mgr int comment '主管编号',25sal int not null comment '员工月薪',26comm int comment '每月补贴',27dno int comment '所在部门编号',28primary key (eno),29foreign key (dno) references tb_dept (dno)30);3132-- alter table tb_emp add constraint pk_emp_eno primary key (eno);33-- alter table tb_emp add constraint uk_emp_ename unique (ename);34-- alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno);35-- alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno);3637insert into tb_emp values38(7800, '张三丰', '总裁', null, 9000, 1200, 20),39(2056, '乔峰', '分析师', 7800, 5000, 1500, 20),40(3088, '李莫愁', '设计师', 2056, 3500, 800, 20),41(3211, '张无忌', '程序员', 2056, 3200, null, 20),42(3233, '丘处机', '程序员', 2056, 3400, null, 20),43(3251, '张翠山', '程序员', 2056, 4000, null, 20),44(5566, '宋远桥', '会计师', 7800, 4000, 1000, 10),45(5234, '郭靖', '出纳', 5566, 2000, null, 10),46(3344, '黄蓉', '销售主管', 7800, 3000, 800, 30),47(1359, '胡一刀', '销售员', 3344, 1800, 200, 30),48(4466, '苗人凤', '销售员', 3344, 2500, null, 30),49(3244, '欧阳锋', '程序员', 3088, 3200, null, 20),50(3577, '杨过', '会计', 5566, 2200, null, 10),51(3588, '朱九真', '会计', 5566, 2500, null, 10);525354-- 查询月薪最高的员工姓名和月薪55select ename, sal from tb_emp where sal=(select max(sal) from tb_emp);5657select ename, sal from tb_emp where sal>=all(select sal from tb_emp);5859-- 查询员工的姓名和年薪((月薪+补贴)*13)60select ename, (sal+ifnull(comm,0))*13 as ann_sal from tb_emp order by ann_sal desc;6162-- 查询有员工的部门的编号和人数63select dno, count(*) as total from tb_emp group by dno;6465-- 查询所有部门的名称和人数66select dname, ifnull(total,0) as total from tb_dept left join67(select dno, count(*) as total from tb_emp group by dno) tb_temp68on tb_dept.dno=tb_temp.dno;6970-- 查询月薪最高的员工(Boss除外)的姓名和月薪71select ename, sal from tb_emp where sal=(72select max(sal) from tb_emp where mgr is not null73);7475-- 查询月薪排第2名的员工的姓名和月薪76select ename, sal from tb_emp where sal=(77select distinct sal from tb_emp order by sal desc limit 1,178);7980select ename, sal from tb_emp where sal=(81select max(sal) from tb_emp where sal<(select max(sal) from tb_emp)82);8384-- 查询月薪超过平均月薪的员工的姓名和月薪85select ename, sal from tb_emp where sal>(select avg(sal) from tb_emp);8687-- 查询月薪超过其所在部门平均月薪的员工的姓名、部门编号和月薪88select ename, t1.dno, sal from tb_emp t1 inner join89(select dno, avg(sal) as avg_sal from tb_emp group by dno) t290on t1.dno=t2.dno and sal>avg_sal;9192-- 查询部门中月薪最高的人姓名、月薪和所在部门名称93select ename, sal, dname94from tb_emp t1, tb_dept t2, (95select dno, max(sal) as max_sal from tb_emp group by dno96) t3 where t1.dno=t2.dno and t1.dno=t3.dno and sal=max_sal;9798-- 查询主管的姓名和职位99-- 提示:尽量少用in/not in运算,尽量少用distinct操作100-- 可以使用存在性判断(exists/not exists)替代集合运算和去重操作101select ename, job from tb_emp where eno in (102select distinct mgr from tb_emp where mgr is not null103);104105select ename, job from tb_emp where eno=any(106select distinct mgr from tb_emp where mgr is not null107);108109select ename, job from tb_emp t1 where exists (110select 'x' from tb_emp t2 where t1.eno=t2.mgr111);112113-- MySQL8有窗口函数:row_number() / rank() / dense_rank()114-- 查询月薪排名4~6名的员工的排名、姓名和月薪115select ename, sal from tb_emp order by sal desc limit 3,3;116117select row_num, ename, sal from118(select @a:=@a+1 as row_num, ename, sal119from tb_emp, (select @a:=0) t1 order by sal desc) t2120where row_num between 4 and 6;121122-- 窗口函数不适合业务数据库,只适合做离线数据分析123select124ename, sal,125row_number() over (order by sal desc) as row_num,126rank() over (order by sal desc) as ranking,127dense_rank() over (order by sal desc) as dense_ranking128from tb_emp limit 3 offset 3;129130select ename, sal, ranking from (131select ename, sal, dense_rank() over (order by sal desc) as ranking from tb_emp132) tb_temp where ranking between 4 and 6;133134-- 窗口函数主要用于解决TopN查询问题135-- 查询每个部门月薪排前2名的员工姓名、月薪和部门编号136select ename, sal, dno from (137select ename, sal, dno, rank() over (partition by dno order by sal desc) as ranking138from tb_emp139) tb_temp where ranking<=2;140141select ename, sal, dno from tb_emp t1142where (select count(*) from tb_emp t2 where t1.dno=t2.dno and t2.sal>t1.sal)<2143order by dno asc, sal desc;144145146