MySQL34道练习题

  1. 哪些人的薪水在部门的平均薪水之上
select
a.ename,a.sal,b.avgsal
from 
emp a 
join
(select avg(sal) as avgsal ,deptno from emp group by deptno) b
on
a.sal > b.avgsal and a.deptno = b.deptno;

±------±--------±------------+
| ename | sal | avgsal |
±------±--------±------------+
| KING | 5000.00 | 2916.666667 |
| JONES | 2975.00 | 2175.000000 |
| SCOTT | 3000.00 | 2175.000000 |
| FORD | 3000.00 | 2175.000000 |
| ALLEN | 1600.00 | 1566.666667 |
| BLAKE | 2850.00 | 1566.666667 |
±------±--------±------------+

  1. 取得部门中(所有人的)平均的薪水等级.
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;

±-------±-------------+
| deptno | avg(s.grade) |
±-------±-------------+
| 10 | 3.6667 |
| 20 | 2.8000 |
| 30 | 2.5000 |
±-------±-------------+

  1. 不准用组函数(Max),取得最高薪水
select 
sal
from 
emp
order by
sal desc
limit 0,1;

±--------+
| sal |
±--------+
| 5000.00 |
±--------+
5. 取得平均薪水最高的部门的部门编号

select 
avg(sal) as avgsal, deptno
from
emp
group by
deptno
order by 
avgsal desc
limit 0,1;

±------------±-------+
| avgsal | deptno |
±------------±-------+
| 2916.666667 | 10 |
±------------±-------+

select
b.deptno
from 
(select avg(sal) as avgsal, deptno 
from emp group by deptno) b
order by
b.avgsal desc
limit 0,1
;

±-------+
| deptno |
±-------+
| 10 |
±-------+

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

select 
d2.dname
from
(select 
e.deptno as d1deptno,s.grade,e.avgsal as d1avgsal
from
(select avg(sal) as avgsal,deptno from emp 
group by deptno order by deptno) e
left join
salgrade s
on
e.avgsal between s.losal and hisal) d1
join
dept d2
on
d1.d1deptno = d2.deptno
order by
d1.d1avgsal asc
limit 0,1
;

±------+
| dname |
±------+
| SALES |
±------+

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

select
distinct b.mgr,b.ename,b.empno,b.sal
from 
emp a
join
emp b
where
a.mgr = b.empno;

±-----±------±------±--------+
| mgr | ename | empno | sal |
±-----±------±------±--------+
| 7566 | FORD | 7902 | 3000.00 |
| 7839 | BLAKE | 7698 | 2850.00 |
| NULL | KING | 7839 | 5000.00 |
| 7839 | JONES | 7566 | 2975.00 |
| 7566 | SCOTT | 7788 | 3000.00 |
| 7839 | CLARK | 7782 | 2450.00 |
±-----±------±------±--------+

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

select 
ename,sal
from
emp
order by
sal desc
limit 0,5;

±------±--------+
| ename | sal |
±------±--------+
| KING | 5000.00 |
| FORD | 3000.00 |
| SCOTT | 3000.00 |
| JONES | 2975.00 |
| BLAKE | 2850.00 |
±------±--------+
10、取得薪水最高的第六到第十名员工

select ename,sal from emp order by sal desc limit 5,5;

±-------±--------+
| ename | sal |
±-------±--------+
| CLARK | 2450.00 |
| ALLEN | 1600.00 |
| TURNER | 1500.00 |
| MILLER | 1300.00 |
| WARD | 1250.00 |
±-------±--------+
11、取得最后入职的5名员工

select
ename,hiredate
from 
emp
order by
hiredate desc
limit 0,5;

±-------±-----------+
| ename | hiredate |
±-------±-----------+
| ADAMS | 1987-05-23 |
| SCOTT | 1987-04-19 |
| MILLER | 1982-01-23 |
| JAMES | 1981-12-03 |
| FORD | 1981-12-03 |
±-------±-----------+

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

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

±------±---------------+
| grade | count(e.ename) |
±------±---------------+
| 1 | 3 |
| 2 | 3 |
| 3 | 2 |
| 4 | 5 |
| 5 | 1 |
±------±---------------+

14、列出所有员工及领导的姓名!!!

select 
a.ename as'员工' ,ifnull(b.ename,'BOSS') as '领导'
from
emp a
left join
emp b
on
a.mgr = b.empno;

15、列出受雇日期早于其直接上级的
所有员工的编号,姓名,部门名称

select 
a.ename as '员工', a.hiredate as '员工入职',
b.ename as'上级',b.hiredate as'上级入职日期',
a.mgr as '员工编号', d.dname as '部门名称'
from
emp a
join
emp b
on
a.mgr = b.empno
join 
dept d
on 
a.deptno = d.deptno
where
a.hiredate < b.hiredate;

16、列出部门名称和这些部门的员工信息,
同时列出那些没有员工的部门.

select 
d.dname,e.*
from
emp e
right join
dept d
on
e.deptno = d.deptno;

17、列出至少有5个员工的所有部门

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

±---------±----------------+
| dname | count(e.deptno) |
±---------±----------------+
| RESEARCH | 5 |
| SALES | 6 |
±---------±----------------+

18、列出薪金比"SMITH"多的所有员工信息.

select
*
from
emp
where
sal >(select
sal
from
emp
where
ename = 'smith');

±------±-------±----------±-----±-----------±--------±--------±-------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
±------±-------±----------±-----±-----------±--------±--------±-------+
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
±------±-------±----------±-----±-----------±--------±--------±-------+

19、列出所有"CLERK"(办事员)的姓名
及其部门名称,部门的人数.

select
count(ename),deptno
from
emp
group by
deptno;
select
e.ename,d.dname
from
emp e
join
dept d
on
e.deptno = d.deptno
where
e.job = 'clerk'
group by
e.ename;
select
e.ename,d.dname,ee.eename
from
emp e
join
(select
count(ename) as eename,deptno
from
emp
group by
deptno) ee
on
ee.deptno = e.deptno
join
dept d
on
e.deptno = d.deptno
where
e.job = 'clerk'
group by
e.ename;

±-------±-----------±-------+
| ename | dname | eename |
±-------±-----------±-------+
| ADAMS | RESEARCH | 5 |
| JAMES | SALES | 6 |
| MILLER | ACCOUNTING | 3 |
| SMITH | RESEARCH | 5 |
±-------±-----------±-------+

20、列出最低薪金大于1500的各种工作
及从事此工作的全部雇员人数.

select
count(e.ename),e.job
from 
emp e
where
e.sal > 1500
group by 
e.job
;

±---------------±----------+
| count(e.ename) | job |
±---------------±----------+
| 2 | ANALYST |
| 3 | MANAGER |
| 1 | PRESIDENT |
| 1 | SALESMAN |
±---------------±----------+

21、列出在部门"SALES"<销售部>工作的员工的姓名
,假定不知道销售部的部门编号.

select deptno from dept where dname = 'sales';
select ename from emp where deptno = 
(select deptno from dept where dname = 'sales');

±-------+
| ename |
±-------+
| ALLEN |
| WARD |
| MARTIN |
| BLAKE |
| TURNER |
| JAMES |
±-------+

22、列出薪金高于公司平均薪金的所有员工,所在部门
,上级领导,雇员的工资等级.

select 
ifnull(b.ename,'BOSS'), a.ename
from
emp a
join
emp b
on
a.mgr = b.empno;


select 
e.ename,d.dname,s.grade
from
emp e
join
dept d
on
e.deptno = d.deptno
join
salgrade s
on
e.sal between s.losal and s.hisal
where 
e.sal > (select 
avg(sal)
from 
emp);

select 
	e.ename,d.dname,s.grade,l.lingdao
from
	emp e
join
	dept d
on
	e.deptno = d.deptno
join
	salgrade s
on
	e.sal between s.losal and s.hisal
left join
	(select ifnull(b.ename,'BOSS') as lingdao,a.ename as aename from emp a join emp b on a.mgr = b.empno) l
on
	l.aename = e.ename
where 
	e.sal > (select avg(sal) from emp);

±------±-----------±------±--------+
| ename | dname | grade | lingdao |
±------±-----------±------±--------+
| JONES | RESEARCH | 4 | KING |
| BLAKE | SALES | 4 | KING |
| CLARK | ACCOUNTING | 4 | KING |
| SCOTT | RESEARCH | 4 | JONES |
| FORD | RESEARCH | 4 | JONES |
| KING | ACCOUNTING | 5 | NULL |
±------±-----------±------±--------+

列出与"SCOTT"从事相同工作的所有员工及部门名称.

select 
	a.ename,d.dname
from
	emp a
join
	(select a.* from emp a where a.ename = 'scott') b
on
	b.job  = a.job
join
	dept d
on
	a.deptno = d.deptno
where
	a.ename <> 'scott';

±------±---------+
| ename | dname |
±------±---------+
| FORD | RESEARCH |
±------±---------+
第二种方法:

select
 e.ename,e.job,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、列出薪金等于部门30中员工的
薪金的其他员工的姓名和薪金.

select
	b.ename, b.sal
from
	emp a
join
	(select * from emp where deptno = 30) b
on
	a.deptno = b.deptno
where 
	a.sal = b.sal and a.ename != b.ename
;

±-------±--------+
| ENAME | SAL |
±-------±--------+
| WARD | 1250.00 |
| MARTIN | 1250.00 |
±-------±--------+
25、列出薪金高于在部门30工作的所有员工的
薪金的员工姓名和薪金.部门名称.

select
	a.ename,a.sal, d.dname
from
	emp a
join
	dept d
on
	a.deptno = d.deptno
where
	a.sal > (select max(sal) from emp where deptno = 30);

±------±--------±-----------+
| ename | sal | dname |
±------±--------±-----------+
| JONES | 2975.00 | RESEARCH |
| SCOTT | 3000.00 | RESEARCH |
| KING | 5000.00 | ACCOUNTING |
| FORD | 3000.00 | RESEARCH |
±------±--------±-----------+

26、列出在每个部门工作的员工数量,
平均工资和平均服务期限.

select
count(ename),avg(sal),avg(2020-5-25-hiredate)
from
emp
group by
deptno;

select 
d.dname,count(e.ename),ifnull(avg(e.sal),0),
ifnull(avg(timestampdiff(year,e.hiredate,now())),0) as avgyear
from
emp e
right join
dept d
on
e.deptno = d.deptno
group by
d.dname
;

±-----------±---------------±---------------------±--------+
| dname | count(e.ename) | ifnull(avg(e.sal),0) | avgyear |
±-----------±---------------±---------------------±--------+
| ACCOUNTING | 3 | 2916.666667 | 38.0000 |
| OPERATIONS | 0 | 0.000000 | 0.0000 |
| RESEARCH | 5 | 2175.000000 | 36.4000 |
| SALES | 6 | 1566.666667 | 38.5000 |
±-----------±---------------±---------------------±--------+
27、列出所有员工的姓名、部门名称和工资。

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

±-------±-----------±--------+
| ename | dname | sal |
±-------±-----------±--------+
| SMITH | RESEARCH | 800.00 |
| ALLEN | SALES | 1600.00 |
| WARD | SALES | 1250.00 |
| JONES | RESEARCH | 2975.00 |
| MARTIN | SALES | 1250.00 |
| BLAKE | SALES | 2850.00 |
| CLARK | ACCOUNTING | 2450.00 |
| SCOTT | RESEARCH | 3000.00 |
| KING | ACCOUNTING | 5000.00 |
| TURNER | SALES | 1500.00 |
| ADAMS | RESEARCH | 1100.00 |
| JAMES | SALES | 950.00 |
| FORD | RESEARCH | 3000.00 |
| MILLER | ACCOUNTING | 1300.00 |
±-------±-----------±--------+
14 rows in set (0.00 sec)

28、列出所有部门的详细信息和人数

select
d.*,count(e.ename)
from
emp e
right join
dept d
on
e.deptno = d.deptno
group by
d.dname;

±-------±-----------±---------±---------------+
| DEPTNO | DNAME | LOC | count(e.ename) |
±-------±-----------±---------±---------------+
| 10 | ACCOUNTING | NEW YORK | 3 |
| 40 | OPERATIONS | BOSTON | 0 |
| 20 | RESEARCH | DALLAS | 5 |
| 30 | SALES | CHICAGO | 6 |
±-------±-----------±---------±---------------+
4 rows in set (0.05 sec)

29、列出各种工作的最低工资及从事此工作的雇员姓名
;

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

±---------±------±----------+
| min(sal) | ename | job |
±---------±------±----------+
| 3000.00 | SCOTT | ANALYST |
| 800.00 | SMITH | CLERK |
| 2450.00 | JONES | MANAGER |
| 5000.00 | KING | PRESIDENT |
| 1250.00 | ALLEN | SALESMAN |
±---------±------±----------+

5 rows in set (0.00 sec)

30、列出各个部门的MANAGER(领导)的最低薪金

select
deptno,min(sal)
from
emp
where
job = 'manager'
group by
deptno;

±-------±---------+
| deptno | min(sal) |
±-------±---------+
| 10 | 2450.00 |
| 20 | 2975.00 |
| 30 | 2850.00 |
±-------±---------+

31、列出所有员工的年工资,按年薪从低到高排序

select
(12*sal)+ifnull(comm,0) as yearsal,ename
from
emp
order by
yearsal asc;

32、求出员工领导的薪水超过3000的员工名称与领导名称

select
a.ename '员工', b.ename'领导'
from
emp a
join
emp b
on
a.mgr = b.empno
where
b.sal > 3000;

±------±-----+
| 员工 | 领导 |
±------±-----+
| JONES | KING |
| BLAKE | KING |
| CLARK | KING |
±------±-----+

33、求出部门名称中,带’S’字符的
部门员工的工资合计、部门人数.

select
count(e.ename),ifnull(sum(sal),0),d.dname
from
emp e
right join
dept d
on
e.deptno = d.deptno
where
d.dname like '%s%'
group by
e.deptno
;

±---------------±---------+
| count(e.ename) | sum(sal) |
±---------------±---------+
| 5 | 10875.00 |
| 6 | 9400.00 |
±---------------±---------+

34、给任职日期超过30年的员工加薪10%.

update emp set sal = sal * 1.1 where timestampdiff(YEAR,hiredate,now())>30;

你可能感兴趣的:(MySQL)