MYSQL34道练习题

PS:代码在最下面,题目转自北京动力节点
1、取得每个部门最高薪水的人员名称

2、哪些人的薪水在部门的平均薪水之上

3、取得部门中(所有人的)平均的薪水等级,如下:

4、不准用组函数(Max),取得最高薪水

5、取得平均薪水最高的部门的部门编号

6、取得平均薪水最高的部门的部门名称

7、求平均薪水的等级最低的部门的部门名称

8、取得比普通员工(员工代码没有在mgr字段上出现的)的最高薪水还要高的领导人姓名

9、取得薪水最高的前五名员工

10、取得薪水最高的第六到第十名员工

11、取得最后入职的5名员工

12、取得每个薪水等级有多少员工

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.找出没选过“黎明”老师的所有学生姓名。
即:

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

问题3:即学过1号课程又学过2号课所有学生的姓名。

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%.

/*1*/
select e.ename,e.sal,e.deptno
from 
	(select deptno,max(sal) maxsal
	from emp
	group by deptno)t
join emp e
on e.sal=t.maxsal and e.deptno=t.deptno;

/*2*/
select ename,sal
from emp e
join (select deptno,avg(sal) avgsal from emp group by deptno)t
on e.sal> t.avgsal and e.deptno=t.deptno;

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

/*4*/
select sal
from emp
order by sal desc
limit 1;

/*5*/
select t.deptno
from (select deptno,avg(sal) avgsal from emp group by deptno)t
order by t.avgsal desc
limit 1;

/*6*/
select d.dname
from (select deptno,avg(sal) avgsal from emp group by deptno)t
join dept d 
on t.deptno=d.deptno
order by t.avgsal desc
limit 1;

/*7*/
select dname
from (select deptno,avg(sal) avgsal from emp group by deptno) t
join dept d
on d.deptno=t.deptno
join salgrade s
on t.avgsal between s.losal and s.hisal
order by s.grade
limit 1;

/*8*/
select ename ,sal
from emp
where empno in (select distinct mgr from emp where mgr is not null)
and sal> (select max(sal) from emp where empno not in (select distinct mgr from emp where mgr is not 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*/
select ename,hiredate
from emp
order by hiredate desc
limit 5;

/*12*/
select grade,count(*)
from emp e
join salgrade s
on e.sal between s.losal and s.hisal
group by grade;

/*13*/

/*1)*/
select sname
from s
where sno not in (
	select sno
	from sc s 
	join c c 
	on s.cno=c.cno
	where c.cteacher='黎明'
 );

/*2)*/
select t.ssname,avg(sc.scgrade)
from (
	select s.sname ssname,s.sno ssno
	from s s
	join sc sc
	on s.sno = sc.sno
	where sc.scgrade<60
	group by sc.sno,s.sname
	having count(*)>=2
)t
join sc sc
on t.ssno=sc.sno;

/*3)*/
select sname
from s s
join (select sno from sc where cno='1')t
on s.sno=t.sno
join (select sno from sc where cno='2')tt
on t.sno=tt.sno;

/*14*/
select e.ename,e1.ename leadername
from emp e
left join emp e1
on e.mgr=e1.empno;

/*15*/
select e.empno,e.ename,d.dname
from emp e
join emp e1
on e.mgr=e.empno
join dept d
on d.deptno=e.deptno
where e.hiredate<=e1.hiredate;

/*16*/
select d.dname,e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,e.deptno
from emp e
right join dept d
on d.deptno=e.deptno
order by dname;

/*17*/
select d.dname,count(*)
from emp e
join dept d
on e.deptno=d.deptno
group by d.dname
having count(*)>=5;

/*18*/
select *
from emp
where sal>(select sal from emp where ename='SMITH');

/*19*/
select e.ename,d.dname,t.cc
from emp e 
join dept d
on e.deptno=d.deptno
join (select deptno,count(*) cc from emp group by deptno)t
on e.deptno=t.deptno
where e.job='CLERK';

/*20*/
select job,count(*)
from emp
group by job
having min(sal)>1500;

/*21*/
select e.ename
from emp e
join dept d
on e.deptno=d.deptno
where d.dname='SALES';

/*22*/
select e.ename as '姓名',d.dname as '部门名称',e1.ename as '上级领导',s.grade as '工资等级' 
from emp e
left join emp e1
on e.mgr=e1.empno
join dept d 
on d.deptno=e.deptno
join salgrade s
on e.sal between s.losal and s.hisal
where e.sal>(select avg(sal) from emp);

/*23*/
select e.ename,d.dname
from emp e
join dept d
on e.deptno=d.deptno
where e.job = (select job from emp where ename='SCOTT') and e.ename !='SCOTT';

/*24*/
select ename,sal
from emp
where sal in (select sal from emp where deptno=30) and deptno!=30;

/*25*/
select e.ename,e.sal,d.dname
from emp e
join dept d
on e.deptno=d.deptno
where sal>(select max(sal) from emp where deptno=30) and e.deptno!=30;

/*26*/
select d.dname,count(e.empno),avg(e.sal),avg(date_format(now(),'%Y')-date_format(e.hiredate,'%Y')) avgdate
from emp e
join dept d
on e.deptno=d.deptno
group by d.dname;

/*27*/
select e.ename,d.dname,e.sal
from emp e
join dept d
on e.deptno=d.deptno;

/*28*/
select d.*,ifnull(t.c,0) '人数'
from dept d
left join (select deptno,count(*) c from emp group by deptno) t
on d.deptno=t.deptno;

/*29*/
select e.*
from emp e
join (select job,min(sal) minsal from emp group by job)t
on t.job = e.job and t.minsal=e.sal;

/*30*/
select deptno,min(sal)
from emp
where job='MANAGER'
group by deptno;

/*31*/
select ename,(sal+ifnull(comm,0))*12 income
from emp
order by income;

/*32*/
select e.ename,e1.ename as leadername
from emp e
left join emp e1
on e.mgr=e1.empno
where e1.sal>3000;

/*33*/
select d.dname,ifnull(sum(e.sal),0) sumsal,count(e.empno) 
from emp e
right join dept d
on d.deptno=e.deptno
where d.dname like '%S%'
group by d.dname;

/*34*/
update emp
set sal=sal*1.1
where (date_format(now(),'%Y')-date_format(hiredate,'%Y'))>30

你可能感兴趣的:(MYSQL)