MySQL练习题目 及答案

发布时间 2023-03-30 19:51:48作者: Alexhee

表创建及创建见文章最后

1、取得每个部门最高薪水的人员名称
select 
  t.deptno,t.maxsal,e.ename 
from 
  emp e 
join 
  (select max(sal) as maxsal,deptno from emp group by deptno) t 
on 
  e.sal = t.maxsal and e.deptno = t.deptno;
2、哪些人的薪水在部门的平均薪水之上
select 
  e.* 
from 
  emp e 
join 
  (select avg(sal) as avgsal,deptno from emp  group by deptno) t 
on 
  e.sal>t.avgsal and e.deptno = t.deptno order by deptno;
3、取得部门中(所有人的)平均的薪水等级,如下:

select 
  e.deptno,avg(s.grade) 
from 
  emp e 
join 
  salgrade s 
on 
  e.sal between s.losal and s.hisal group by e.deptno;

4、不准用组函数(Max),取得最高薪水
select 
  * 
from 
  emp 
order by sal desc limit 0,1;
5、取得平均薪水最高的部门的部门编号
1.
select 
  deptno 
from 
  emp 
group by 
  deptno 
order by avg(sal) desc limit 0,1;
2.
select 
  avg(s.sal) as avgsal,s.deptno 
from 
  emp s 
group by 
  s.deptno 
having 
avgsal=(select max(e.avgsal) as maxavgsal from (select avg(sal) as avgsal,deptno from emp group by deptno) e) ;
6、取得平均薪水最高的部门的部门名称
select
 se.deptno,d.dname 
from 
(select avg(s.sal) as avgsal,s.deptno from emp s group by s.deptno having avgsal=(select max(e.avgsal) as maxavgsal from (select avg(sal) as avgsal,deptno from emp group by deptno) e) )se 
join 
dept d 
on se.deptno = d.deptno; 
7、求平均薪水的等级最低的部门的部门名称
select 
  d.dname 
from 
  dept d 
where d.deptno = (select asal.deptno from (select avg(sal) as avgsal,deptno from emp group by deptno )asal join salgrade ag on asal.avgsal between ag.losal and ag.hisal  order by ag.grade limit 0,1);
8、取得比普通员工(员工代码没有在mgr字段上出现的)的最高薪水还要高的领导人姓名
select 
  ename,sal 
from 
  emp 
where 
  sal>(select max(sal) from emp where empno not in(select distinct mgr from emp where mgr is not null)) and empno in(select distinct mgr from emp where mgr is not null);
9、取得薪水最高的前五名员工
select 
  * 
from 
  emp 
order by sal desc limit 0,5;
10、取得薪水最高的第六到第十名员工
select 
  * 
from 
  emp 
order by sal desc limit 5,5;
11、取得最后入职的5名员工
select 
  *  
from 
  emp 
order by hiredate limit 0,5;
12、取得每个薪水等级有多少员工
select 
  count(sg.grade),sg.grade 
from 
  emp e 
join 
  salgrade sg on e.sal between sg.losal and sg.hisal group by sg.grade;
13、面试题

有3个表S(学生表),C(课程表),SC(学生选课表)
S(SNO,SNAME)代表(学号,姓名)
C(CNO,CNAME,CTEACHER)代表(课号,课名,教师)
SC(SNO,CNO,SCGRADE)代表(学号,课号,成绩)
问题:
1,找出没选过“黎明”老师的所有学生姓名。

select 
  * 
from 
  student 
where 
  sno not in (select sno from student_course where cno = (select cno from course where cteacher = '黎明'));

2,列出2门以上(含2门)不及格学生姓名及平均成绩。


3,即学过1号课程又学过2号课所有学生的姓名。
请用标准SQL语言写出答案,方言也行(请说明是使用什么方言)。

14、列出所有员工及领导的姓名
15、列出受雇日期早于其直接上级的所有员工的编号,姓名,部门名称
16、列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门.
17、列出至少有5个员工的所有部门
18、列出薪金比"SMITH"多的所有员工信息.
19、列出所有"CLERK"(办事员)的姓名及其部门名称,部门的人数.
20、列出最低薪金大于1500的各种工作及从事此工作的全部雇员人数.
21、列出在部门"SALES"<销售部>工作的员工的姓名,假定不知道销售部的部门编号.
22、列出薪金高于公司平均薪金的所有员工,所在部门,上级领导,雇员的工资等级.
23、列出与"SCOTT"从事相同工作的所有员工及部门名称.
24、列出薪金等于部门30中员工的薪金的其他员工的姓名和薪金.
25、列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金.部门名称.
26、列出在每个部门工作的员工数量,平均工资和平均服务期限.
27、列出所有员工的姓名、部门名称和工资。
28、列出所有部门的详细信息和人数
29、列出各种工作的最低工资及从事此工作的雇员姓名
30、列出各个部门的MANAGER(领导)的最低薪金
31、列出所有员工的年工资,按年薪从低到高排序
32、求出员工领导的薪水超过3000的员工名称与领导名称
33、求出部门名称中,带'S'字符的部门员工的工资合计、部门人数.
34、给任职日期超过30年的员工加薪10%.