Mysql 练习题10道(1-10题)
Posted 枸杞仙人
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql 练习题10道(1-10题)相关的知识,希望对你有一定的参考价值。
练习
- 取得每个部门最高薪水的人员名称
- 哪些人的薪水在部门的平均薪水之上
- 取得部门中(所有人的)平均的薪水等级
- 不准用聚合函数max,取得最高薪水
- 取得平均薪水最高的部门的部门编号
- 取得平均薪水最高的部门的部门名称
- 求平均薪水的等级最低的部门的部门名称
- 取得比普通员工(员工代码没有在mgr 字段上出现的) 的最高薪水还要高的领导人姓名
- 取得薪水最高的前五名员工
- 取得薪水最高的第六到第十名员工
取得每个部门最高薪水的人员名称
- 第一步:
取得每个部门最高薪水(按照部门编号分组,找出每一组最大值)
select max(sal) maxsal from emp group by deptno;
+---------+
| maxsal |
+---------+
| 3000.00 |
| 2850.00 |
| 5000.00 |
+---------+
- 第二步:
将以上结果当做一张临时表和emp连接
select e.ename,e.sal,e.deptno from emp e join (select max(sal) maxsal from emp group by deptno) t on e.sal = t.maxsal;
+-------+---------+--------+
| ename | sal | deptno |
+-------+---------+--------+
| BLAKE | 2850.00 | 30 |
| SCOTT | 3000.00 | 20 |
| KING | 5000.00 | 10 |
| FORD | 3000.00 | 20 |
+-------+---------+--------+
哪些人的薪水在部门的平均薪水之上
第一步:取得每个部门的平均薪水:
select deptno,avg(sal) avgsal from emp group by deptno;
+--------+-------------+
| deptno | avgsal |
+--------+-------------+
| 20 | 2175.000000 |
| 30 | 1566.666667 |
| 10 | 2916.666667 |
+--------+-------------+
第二步:将以上结果当做一张临时表和emp连接:
select e.ename,e.sal,e.deptno,t.* 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;
+-------+---------+--------+--------+-------------+
| ename | sal | deptno | deptno | avgsal |
+-------+---------+--------+--------+-------------+
| ALLEN | 1600.00 | 30 | 30 | 1566.666667 |
| JONES | 2975.00 | 20 | 20 | 2175.000000 |
| BLAKE | 2850.00 | 30 | 30 | 1566.666667 |
| SCOTT | 3000.00 | 20 | 20 | 2175.000000 |
| KING | 5000.00 | 10 | 10 | 2916.666667 |
| FORD | 3000.00 | 20 | 20 | 2175.000000 |
+-------+---------+--------+--------+-------------+
取得部门中(所有人的)平均的薪水等级
第一步:找出每一个人的薪水等级
select t.grade,e.ename,e.sal,e.deptno from emp e join salgrade t on e.sal between t.losal and t.hisal;
+-------+--------+---------+--------+
| grade | ename | sal | deptno |
+-------+--------+---------+--------+
| 1 | SMITH | 800.00 | 20 |
| 3 | ALLEN | 1600.00 | 30 |
| 2 | WARD | 1250.00 | 30 |
| 4 | JONES | 2975.00 | 20 |
| 2 | MARTIN | 1250.00 | 30 |
| 4 | BLAKE | 2850.00 | 30 |
| 4 | CLARK | 2450.00 | 10 |
| 4 | SCOTT | 3000.00 | 20 |
| 5 | KING | 5000.00 | 10 |
| 3 | TURNER | 1500.00 | 30 |
| 1 | ADAMS | 1100.00 | 20 |
| 1 | JAMES | 950.00 | 30 |
| 4 | FORD | 3000.00 | 20 |
| 2 | MILLER | 1300.00 | 10 |
+-------+--------+---------+--------+
第二步:将以上结果按照dept分组获得等级平均值
select e.deptno,avg(t.grade) from emp e join salgrade t on e.sal between t.losal and t.hisal group by deptno;
+--------+--------------+
| deptno | avg(t.grade) |
+--------+--------------+
| 20 | 2.8000 |
| 30 | 2.5000 |
| 10 | 3.6667 |
+--------+--------------+
不准用聚合函数max,取得最高薪水
第一种方法:sal降序+limit
select ename,sal,deptno from emp order by sal desc limit 0,1;
+-------+---------+--------+
| ename | sal | deptno |
+-------+---------+--------+
| KING | 5000.00 | 10 |
+-------+---------+--------+
第二种方法:表的自连接
select ename,sal from emp where sal not in(select distinct a.sal from emp a join emp b on a.sal < b.sal);
+-------+---------+--------+
| ename | sal | deptno |
+-------+---------+--------+
| KING | 5000.00 | 10 |
+-------+---------+--------+
取得平均薪水最高的部门的部门编号
方法一:
第一步:取得各部门平均薪水
select deptno,avg(sal) avgsal from emp group by deptno;
+--------+-------------+
| deptno | avgsal |
+--------+-------------+
| 20 | 2175.000000 |
| 30 | 1566.666667 |
| 10 | 2916.666667 |
+--------+-------------+
第二步:将以上结果进行自连接取得部门编号
select deptno,avg(sal) avgsal from emp group by deptno order by avgsal limit 1;
+--------+-------------+
| deptno | avgsal |
+--------+-------------+
| 10 | 2916.666667 |
+--------+-------------+
方法二:
第一步:取得各部门平均薪水
select deptno,avg(sal) avgsal from emp group by deptno;
+--------+-------------+
| deptno | avgsal |
+--------+-------------+
| 20 | 2175.000000 |
| 30 | 1566.666667 |
| 10 | 2916.666667 |
+--------+-------------+
第二步:获得平均薪水最大值并用having进行二次筛选
select deptno,avg(sal) avgsal from emp group by deptno having avgsal = (select max(t.avgsal) from (select deptno,avg(sal) avgsal from emp group by deptno) t);
+--------+-------------+
| deptno | avgsal |
+--------+-------------+
| 10 | 2916.666667 |
+--------+-------------+
取得平均薪水最高的部门的部门名称
和上一题相似,只不过需要对结果再进行一次连接查询:
select d.dname,e.deptno,avg(e.sal) avgsal from emp e join dept d on d.deptno=e.deptno group by deptno order by avgsal desc limit 1;
+------------+--------+-------------+
| dname | deptno | avgsal |
+------------+--------+-------------+
| ACCOUNTING | 10 | 2916.666667 |
+------------+--------+-------------+
求平均薪水的等级最低的部门的部门名称
方法一:
第一步:找出每个部门的平均薪水
select deptno,avg(sal) as avgsal from emp group by deptno;
+--------+-------------+
| deptno | avgsal |
+--------+-------------+
| 10 | 2916.666667 |
| 20 | 2175.000000 |
| 30 | 1566.666667 |
+--------+-------------+
第二步:找出每个部门的平均薪水的等级
第一步的t表和salgrade表连接,条件:t.avgsal between s.losal and s.hisal
select t.*,s.grade from salgrade s join (select d.dname,avg(e.sal) as avgsal from emp e join dept d on e.deptno = d.deptno group by d.dname) t on t.avgsal between s.losal and s.hisal;
+------------+-------------+-------+
| dname | avgsal | grade |
+------------+-------------+-------+
| RESEARCH | 2175.000000 | 4 |
| SALES | 1566.666667 | 3 |
| ACCOUNTING | 2916.666667 | 4 |
+------------+-------------+-------+
第三步:加上where条件
select t.*,s.grade from salgrade s join (select d.dname,avg(e.sal) as avgsal from emp e join dept d on e.deptno = d.deptno group by d.dname) t on t.avgsal between s.losal and s.hisal where s.grade = (select grade from salgrade where (select avg(sal) avgsal from emp group by deptno order by avgsal asc limit 1) between losal and hisal);
+-------+-------------+-------+
| dname | avgsal | grade |
+-------+-------------+-------+
| SALES | 1566.666667 | 3 |
+-------+-------------+-------+
取得比普通员工(员工代码没有在mgr 字段上出现的) 的最高薪水还要高的领导人姓名
第一步:取得普通员工的最高薪水
select max(sal) maxsal from emp where empno not in (select mgr from emp where mgr is not null);
+---------+
| maxsal |
+---------+
| 1600.00 |
+---------+
第二步:取得比maxsal高的领导名字
select ename,sal from emp where sal>1600 and empno in (select mgr from emp where mgr is not null);
+-------+---------+
| ename | sal |
+-------+---------+
| JONES | 2975.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
| FORD | 3000.00 |
+-------+---------+
取得薪水最高的前五名员工
select ename,sal from emp order by sal desc limit 5;
+-------+---------+
| ename | sal |
+-------+---------+
| KING | 5000.00 |
| SCOTT | 3000.00 |
| FORD | 3000.00 |
| JONES | 2975.00 |
| BLAKE | 2850.00 |
+-------+---------+
取得薪水最高的第六到第十名员工
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 |
+--------+---------+
以上是关于Mysql 练习题10道(1-10题)的主要内容,如果未能解决你的问题,请参考以下文章