-- 1.取得每个部门最高薪水的人员名称
select ename ,max(sal) as sal,deptno from emp group by deptno
-- 2. 哪些人的薪水在部门的平均薪水之上
select ename, sal from emp join (select empno,deptno,avg(sal) as avg_sal from emp group by deptno)as d on emp.deptno=d.deptno where emp.sal>d.avg_sal
select empno,deptno,avg(sal) from emp group by deptno
-- 3. 取得部门中(所有人的)平均的薪水等级
select empno,deptno,avg(sal) from emp group by deptno
select deptno,avg(grade) from salgrade join (select deptno,sal from emp ) as d on d.sal between salgrade.losal and salgrade.hisal group by deptno
-- 4. 不准用组函数(Max),取得最高薪水(给出两种解决方案)
select sal from emp order by sal desc limit 1
/*1.获取非最高员工的薪水,自己的薪水小于其他人的薪水2.获取薪水不在1中的薪水*/
select sal from emp where sal not in (select distinct e1.sal from emp as e1 inner join emp as e2 on e1.sal(select max(sal) as max_sal from emp where empno not in (select mgr from emp where mgr is not null))
select max(sal) as max_sal from emp where empno not in (select mgr from emp where mgr is not null)
select * from emp where empno not in (select distinct mgr from emp )/*以上语句无法查询到结果,因为not in不会自动忽略null,需要程序员手动排除null*/
-- 9. 取得薪水最高的前五名员工
select ename,sal from emp order by sal desc limit 5
-- 10.取得薪水最高的第六到第十名员工
select ename,sal from emp order by sal desc limit 5,5
-- 11.取得最后入职的5名员工
select ename,hiredate from emp order by hiredate desc limit 5
-- 12.取得每个薪水等级有多少员工
select e3.grade as grade,count(*) from
(select e2.grade from emp as e1 join salgrade as e2 on e1.sal between e2.losal and e2.hisal) as e3 group by e3.grade
-- 13.面试题
/*
有3个表S(学生表),C(课程表),SC(学生选课表)
S(SNO,SNAME)代表(学号,姓名)
C(CNO,CNAME,CTEACHER)代表(课号,课名,教师)
SC(SNO,CNO,SCGRADE)代表(学号,课号,成绩)
问题:
1,找出没选过“黎明”老师的所有学生姓名。
2,列出2门以上(含2门)不及格学生姓名及平均成绩。
3,即学过1号课程又学过2号课所有学生的姓名。
请用标准SQL语言写出答案,方言也行(请说明是使用什么方言)。
*/
CREATE TABLE SC
(
SNO VARCHAR(200),
CNO VARCHAR(200),
SCGRADE VARCHAR(200)
);
CREATE TABLE S
(
SNO VARCHAR(200 ),
SNAME VARCHAR(200)
);
CREATE TABLE C
(
CNO VARCHAR(200),
CNAME VARCHAR(200),
CTEACHER VARCHAR(200)
);
INSERT INTO C ( CNO, CNAME, CTEACHER ) VALUES ( '1', '语文', '张');
INSERT INTO C ( CNO, CNAME, CTEACHER ) VALUES ( '2', '政治', '王');
INSERT INTO C ( CNO, CNAME, CTEACHER ) VALUES ( '3', '英语', '李');
INSERT INTO C ( CNO, CNAME, CTEACHER ) VALUES ( '4', '数学', '赵');
INSERT INTO C ( CNO, CNAME, CTEACHER ) VALUES ( '5', '物理', '黎明');
commit;
INSERT INTO S ( SNO, SNAME ) VALUES ( '1', '学生1');
INSERT INTO S ( SNO, SNAME ) VALUES ( '2', '学生2');
INSERT INTO S ( SNO, SNAME ) VALUES ( '3', '学生3');
INSERT INTO S ( SNO, SNAME ) VALUES ( '4', '学生4');
commit;
INSERT INTO SC ( SNO, CNO, SCGRADE ) VALUES ( '1', '1', '40');
INSERT INTO SC ( SNO, CNO, SCGRADE ) VALUES ( '1', '2', '30');
INSERT INTO SC ( SNO, CNO, SCGRADE ) VALUES ( '1', '3', '20');
INSERT INTO SC ( SNO, CNO, SCGRADE ) VALUES ( '1', '4', '80');
INSERT INTO SC ( SNO, CNO, SCGRADE ) VALUES ( '1', '5', '60');
INSERT INTO SC ( SNO, CNO, SCGRADE ) VALUES ( '2', '1', '60');
INSERT INTO SC ( SNO, CNO, SCGRADE ) VALUES ( '2', '2', '60');
INSERT INTO SC ( SNO, CNO, SCGRADE ) VALUES ( '2', '3', '60');
INSERT INTO SC ( SNO, CNO, SCGRADE ) VALUES ( '2', '4', '60');
INSERT INTO SC ( SNO, CNO, SCGRADE ) VALUES ( '2', '5', '40');
INSERT INTO SC ( SNO, CNO, SCGRADE ) VALUES ( '3', '1', '60');
INSERT INTO SC ( SNO, CNO, SCGRADE ) VALUES ( '3', '3', '80');
commit;
-- 1. 找出没选过“黎明”老师的所有学生姓名
select distinct SC.*,C.* from C join SC on c.cno=sc.cno where c.cteacher<>'黎明'
select sname from s join (select distinct SC.sno from C join SC on c.cno=sc.cno where c.cteacher<>'黎明') as t on s.sno=t.sno
-- 2. 列出2门以上(含2门)不及格学生姓名及平均成绩????
select sno,avg(scgrade) as avgscore from sc group by sno;
select sc.sno,s.sname ,avg(sc.scgrade) from sc join s on sc.sno=s.sno where sc.scgrade<60 having count(*)>2
select t1.sname,t2.avgscore from (select sc.sno,s.sname ,avg(sc.scgrade) from sc join s on sc.sno=s.sno where sc.scgrade<60 having count(*)>2
) as t1 join (select sno,avg(scgrade) as avgscore from sc group by sno
) as t2 on t1.sno=t2.sno
-- 3. 即学过1号课程又学过2号课所有学生的姓名
select s.sname from s join sc on s.sno=sc.sno where sc.cno =1 and sc.sno in (select sno from sc where cno=2)
-- 14. 列出所有员工及领导的姓名
select e1.empno,e1.ename from emp as e1 join emp as e2 on e1.empno=e2.mgr
select distinct t1.ename,t2.ename from emp as t1 join (select e1.empno as empno,e1.ename as ename from emp as e1 join emp as e2 on e1.empno=e2.mgr
) as t2 on t1.mgr=t2.empno
-- 15.列出受雇日期早于其直接上级的所有员工的编号,姓名,部门名称
select distinct t1.empno as empno,t1.ename as ename ,t1.deptno as deptno from emp as t1 join (select e1.empno as empno,e1.ename as ename,e1.hiredate as hiredate from emp as e1 join emp as e2 on e1.empno=e2.mgr
) as t2 on t1.mgr=t2.empno where t1.hiredate=5
-- 18. 列出薪金比"SMITH"多的所有员工信息
select sal from emp where ename='SIMITH'
select * from emp where sal >(select sal from emp where ename='SIMITH')
-- 19.列出所有"CLERK"(办事员)的姓名及其部门名称,部门的人数.
select e.ename,e.deptno from emp as e where e.job='CLERK'
select e.empno,d.deptno,d.dname,count(*) as cc from dept as d join emp as e on e.deptno=d.deptno group by d.dname
select t1.ename,t2.dname,t2.cc from (select e.ename as ename,e.deptno as deptno from emp as e where e.job='CLERK'
) as t1 join (select e.empno as empno,d.deptno as deptno,d.dname,count(*) as cc from dept as d join emp as e on e.deptno=d.deptno group by d.dname
) as t2 on t1.deptno=t2.deptno
-- 20.、列出最低薪金大于1500的各种工作及从事此工作的全部雇员人数
select e.job ,count(*) as cc from emp as e group by e.job
select distinct t1.job ,t2.cc from emp as t1 join (select e.job as job ,count(*) as cc from emp as e group by e.job
) as t2 on t1.job=t2.job where t1.sal>1500
-- 21.列出在部门"SALES"<销售部>工作的员工的姓名,假定不知道销售部的部门编号.
select ename from emp where deptno=(select deptno from dept where dname="SALES");
-- 22.列出薪金高于公司平均薪金的所有员工,所在部门,上级领导,雇员的工资等级.
select a.ename empname ,d.dname,b.ename leadername,s.grade from emp a join dept d on a.deptno=d.deptno join emp b
on a.mgr = b.empno join salgrade s on a.sal between s.losal and s.hisal where a.sal >(select avg(sal) from emp);
-- 23. 列出与"SCOTT"从事相同工作的所有员工及部门名称.
select job from emp where ename='SCOTT'
select e.ename,d.dname from emp as e join dept as d on e.deptno=d.deptno where e.job=(select job from emp where ename='SCOTT' )
-- 24. 列出薪金等于部门30中员工的薪金的其他员工的姓名和薪金
select sal from emp where deptno=30
select e1.ename,e1.sal from emp as e1 where e1.sal in (select sal from emp where deptno=30)
-- 25. 列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金.部门名称.
select e.ename,e.sal,d.dname from emp as e join dept as d on e.deptno=d.deptno and e.sal>(select max(sal) from emp where deptno=30)
-- 26. 列出在每个部门工作的员工数量,平均工资和平均服务期限.
select d.deptno,count(e.ename),ifnull(avg(e.sal),0) as avgsal ,avg(ifnull((to_days(now())-to_days(e.hiredate))/365,0)) from emp as e right join dept as d on e.deptno=d.deptno group by e.deptno
-- 27.列出所有员工的姓名、部门名称和工资。
select e.ename,d.dname,e.sal from emp as e join dept as d on e.deptno=d.deptno
-- 28. 列出所有部门的详细信息和人数
select d.*,count(e.ename) from emp as e right join dept as d on d.deptno=e.deptno group by e.deptno
-- 29.列出各种工作的最低工资及从事此工作的雇员姓名
select min(sal) as minsal,job from emp group by job
select t.*,e.ename from emp as e join (select min(sal) as minsal,job from emp group by job) as t on t.job=e.job and t.minsal=e.sal
-- 30.列出各个部门的MANAGER(领导)的最低薪金
select deptno,min(sal) from emp where job ='MANAGER' group by deptno;
-- 31. 列出所有员工的年工资,按年薪从低到高排序
select ename,((sal+ifnull(comm,0))*12) as income from emp order by income asc
-- 32.求出员工领导的薪水超过3000的员工名称与领导名称
select a.ename empname,a.sal,b.ename leadername,b.sal from emp a join emp b on a.mgr=b.empno where b.sal>3000
-- 33.求出部门名称中,带'S'字符的部门员工的工资合计、部门人数
select d.dname,count(e.ename),ifnull(sum(e.sal),0) from emp as e right join dept as d on e.deptno=d.deptno where d.dname like "%S%" group by d.dname
-- 34.给任职日期超过30年的员工加薪10%.
select ifnull((to_days(now())-to_days(e.hiredate))/365,0) as severYear from emp as e
drop table if exists emp_bak;
create table emp_bak as select * from emp;
update emp_bak set sal=sal*1.1 where (to_days(now())-to_days(hiredate))/365>30