MySQL基础练习题:习题456

这部分主要是为了帮助大家回忆回忆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基础练习题:创建数据库

相关推荐
菲兹园长1 小时前
表的设计(MYSQL)
数据库·mysql
Java Fans1 小时前
MySQL数据库常用命令大全(完整版——表格形式)
数据库·mysql
白萝卜弟弟1 小时前
【MySQL】MySQL函数之JSON_EXTRACT
数据库·mysql·json
gjh12081 小时前
MySQL常见面试题
数据库·mysql
我的K84092 小时前
Flink整合Hive、Mysql、Hbase、Kafka
hive·mysql·flink
little_kid_pea3 小时前
MySQL Workbench导入数据比mysql命令行慢
数据库·mysql
苹果醋33 小时前
C语言 strlen 函数 - C语言零基础入门教程
java·运维·spring boot·mysql·nginx
爪哇学长4 小时前
解锁数据世界:从基础到精通的数据库探索之旅
数据库·mysql·oracle
码哥字节4 小时前
重生之从零设计 MySQL 架构
数据库·mysql·架构
地图之家家长9 小时前
19.(开发工具篇mysql库)mysql锁表问题解决
数据库·mysql