MySQL基础练习题:习题456

时间:2024-04-10 16:05:25

这部分主要是为了帮助大家回忆回忆MySQL的基本语法,数据库来自于MySQL的官方简化版,题目也是网上非常流行的35题。这些基础习题基本可以涵盖面试中需要现场写SQL的问题。上期帮助大家完成了热身运动,接下来让我们继续练习。

给出最高薪水(给出三种解决方案)

Solution1 :

mysql> select ename,max(sal) from emp;
+--------+----------+
| ename  | max(sal) |
+--------+----------+
| SIMITH |  5000.00 |
+--------+----------+

Solution2:
按照薪水降序排列,取第一个

mysql> select ename,sal from emp order by sal desc limit 1;
+-------+---------+
| ename | sal     |
+-------+---------+
| KING  | 5000.00 |
+-------+---------+

Solution3:
最大值不小于表中任何一个工资,所以不会出现在此临时表中

mysql> select ename,sal from emp where sal not in 
(select a.sal from emp a join emp b on a.sal<b.sal);
+-------+---------+
| ename | sal     |
+-------+---------+
| KING  | 5000.00 |
+-------+---------+

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

解法一

1,先取得每个部门的平均薪水

mysql> select deptno, avg(sal) as avgsal from emp group by deptno;
+--------+-------------+
| deptno | avgsal      |
+--------+-------------+
|     20 | 2175.000000 |
|     30 | 1566.666667 |
|     10 | 2916.666667 |
+--------+-------------+

2,再取得最高的平均薪水

mysql> select avg(sal) as avgsal from emp group by deptno order by avgsal desc limit 1; 
+--------+-------------+
| deptno | avgsal      |
+--------+-------------+
|     20 | 2175.000000 |
|     30 | 1566.666667 |
|     10 | 2916.666667 |
+--------+-------------+

3,然后取得和最高平均薪水相同的部门(因为可能有多个部门并列最高)

mysql> select deptno,avg(sal) as avgsal from emp group by deptno having avgsal=
    -> (select avg(sal) as avgsal from emp group by deptno order by avgsal desc limit 1);
+--------+-------------+
| deptno | avgsal      |
+--------+-------------+
|     10 | 2916.666667 |
+--------+-------------+

解法二 : 使用max()函数

mysql> select deptno,avg(sal) as avgsal 
from emp group by deptno having avgsal=(select max(t.avgsal) from (select avg(sal) as avgsal from emp group by deptno) t);
+--------+-------------+
| deptno | avgsal      |
+--------+-------------+
|     10 | 2916.666667 |
+--------+-------------+

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

解法一

第一步获取每个部门的平均薪资,

(root@localhost) [employees]>select a.deptno, avg(a.sal) from emp a group by a.deptno;
+--------+-------------+
| deptno | avg(a.sal)  |
+--------+-------------+
|     20 | 2175.000000 |
|     30 | 1566.666667 |
|     10 | 2916.666667 |
+--------+-------------+
3 rows in set (0.16 sec)

(root@localhost) [employees]>

再双表联查(emp和dept),获得每个部门的平均薪资,部门编号和部门名称

(root@localhost) [employees]>select b.dname,a.deptno, avg(a.sal) from emp a join dept b on a.deptno = b.deptno group by a.deptno;
+-------------+--------+-------------+
| dname       | deptno | avg(a.sal)  |
+-------------+--------+-------------+
| RESEARCHING |     20 | 2175.000000 |
| SALES       |     30 | 1566.666667 |
| ACCOUNTING  |     10 | 2916.666667 |
+-------------+--------+-------------+
3 rows in set (0.03 sec)

(root@localhost) [employees]>

最后降序排列,只取第一个

mysql> select b.dname,avg(a.sal) as avgsal from emp a join dept b on a.deptno=b.deptno group by b.dname having avgsal=(select max(t.avgsal) from (select avg(sal) as avgsal from emp group by deptno) t);
+------------+-------------+
| dname      | avgsal      |
+------------+-------------+
| ACCOUNTING | 2916.666667 |
+------------+-------------+

解法二 : 使用max()

原理和上一题第二问大同小异。

mysql> select b.dname,avg(a.sal) as avgsal from emp a join dept b on a.deptno=b.deptno group by b.dname having avgsal=(select max(t.avgsal) from (select avg(sal) as avgsal from emp group by deptno) t);
+------------+-------------+
| dname      | avgsal      |
+------------+-------------+
| ACCOUNTING | 2916.666667 |
+------------+-------------+

往期文章

MySQL基础练习题:习题456

MySQL基础练习题:习题2-3

MySQL基础练习题:创建数据库

在这里插入图片描述