表创建及创建见文章最后
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语言写出答案,方言也行(请说明是使用什么方言)。