create database hr default charset utf8;

use hr;

show tables;

drop table if exists tb_dept;

create table tb_dept(
deptno tinyint not null comment "部门编号",
dname varchar(10) not null comment "部门名称",
dloc varchar(10) not null comment "部门所在地",
primary key(deptno)

desc tb_dept;

drop table if exists tb_emp;

create table tb_emp(
empno integer not null comment "员工编号",
ename varchar(8) not null comment "员工姓名",
job varchar(10) not null comment "员工职位",
mgr integer not null comment "主管编号",
sal varchar(8) not null comment "员工月薪",
dno tinyint not null comment "所在部门编号",
primary key(empno));

desc tb_emp;
alter table tb_emp add constraint fk_dno foreign key(dno) references tb_dept(deptno);

insert into tb_dept values(10, '会计部', '北京');
insert into tb_dept values(20, '研发部', '成都');
insert into tb_dept values(30, '销售部', '重庆');
insert into tb_dept values(40, '运维部', '深圳');

insert into tb_emp values(7800, '张三丰', '总裁', 0000, 9000, 20);
insert into tb_emp values(2056, '乔峰', '分析师', 7800, 5000, 20);
insert into tb_emp values(3088, '李莫愁', '设计师', 2056, 3500, 20);
insert into tb_emp values(3211, '张无忌', '程序员', 2056, 3200, 20);
insert into tb_emp values(3233, '丘处机', '程序员', 2056, 3400, 20);
insert into tb_emp values(3251, '张翠山', '程序员', 2056, 4000, 20);
insert into tb_emp values(5566, '宋明月', '会计师', 7800, 4000, 10);
insert into tb_emp values(5234, '郭靖', '出纳', 5566, 2000, 10);
insert into tb_emp values(3344, '黄蓉', '销售主管', 7800, 3000, 30);
insert into tb_emp values(1359, '胡一刀', '销售员', 3344, 1800, 30);
insert into tb_emp values(4466, '苗人凤', '销售员', 3344, 2500, 30);
insert into tb_emp values(3244, '欧阳锋', '程序员', 3088, 3200, 20);
insert into tb_emp values(3577, '杨过', '会计师', 5566, 2200, 10);
insert into tb_emp values(3588, '朱九真', '会计师', 5566, 2200, 10);

-- 1.查询薪水最高的员工姓名和工资
select ename as "姓名", sal as " 工资" from tb_emp order by(-sal) limit 1;
-- 2.查询员工的姓名和年薪(月薪*12)
select ename as "姓名", sal*12 as "年薪" from tb_emp order by(-sal);
-- 3.查询有员工的部门的编号和人数
select t1.deptno, t1.dname, ifnull(t2.c, 0) from tb_dept t1 left join (select dno, count(*) as c from tb_emp group by dno) t2 on t1.deptno = t2.dno
-- 4.查询所有部门的名称和人数
select dno as "部门编号", t2.dname as "部门名称", count(*) as "员工数量"  from tb_emp t1 join tb_dept t2 on t1.dno=t2.deptno group by (t2.deptno);
-- 5.查询薪水最高的员工(boss除外)的姓名和工资
select ename as "姓名", sal as " 工资" from tb_emp  where mgr!=0000 order by(-sal) limit 1;
-- 6.查询薪水超过平均薪水的员工的姓名和工资
select ename as "姓名", sal as "工资" from tb_emp t where sal>=(select avg(sal) from tb_emp) order by (-t.sal);
-- 7.查询薪水超过其所在部门的平均薪水的员工的姓名, 部门编号和工资
select * from tb_dept t3
join(select t1.dno, ename, sal, avgsal from tb_emp t1
join(select avg(sal) as avgsal, dno from tb_emp group by dno) t2 on t1.dno = t2.dno and t1.sal > avgsal) t4
on t3.deptno = t4.dno
-- 8.查询部门中薪水最高的人的姓名, 工资和所在部门名称
select * from (select ename, sal, dno from tb_emp b1 join (select max(sal) as c, dno as d from tb_emp t1 join tb_dept t2 on t1.dno=t2.deptno group by(t2.deptno)) b2 on b2.c=b1.sal and b2.d=b1.dno) b3 order by(dno)
-- 9.查询主管的姓名和职位(员工编号在主管编号里)
select ename, job from tb_emp where job = "销售主管";

select * from tb_emp where empno in 
(select distinct mgr from tb_emp where mgr is not null)
-- 10.查询薪水排名前三的员工姓名和工资
select ename, sal from tb_emp order by (-sal) limit 3;
-- 11.求薪水排在第4-8的名的员工
select ename, sal from tb_emp order by (-sal) limit 4 offset 3;