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基础练习题:创建数据库

相关推荐
Ciderw11 分钟前
MySQL日志undo log、redo log和binlog详解
数据库·c++·redis·后端·mysql·面试·golang
~Yogi32 分钟前
新版Tomcat MySQL IDEA 安装配置过程遇到的问题
mysql·tomcat·intellij-idea
隔壁老王1561 小时前
tidb实时同步到mysql
数据库·mysql·tidb
听封1 小时前
✨ 索引有哪些缺点以及具体有哪些索引类型
数据库·mysql
ChinaRainbowSea2 小时前
1. Linux下 MySQL 的详细安装与使用
linux·数据库·sql·mysql·adb
致奋斗的我们2 小时前
Nginx反向代理及负载均衡
linux·运维·mysql·nginx·负载均衡·shell·openeluer
小林熬夜学编程3 小时前
【MySQL】第八弹---全面解析数据库表的增删改查操作:从创建到检索、排序与分页
linux·开发语言·数据库·mysql·算法
风月歌3 小时前
基于springboot校园健康系统的设计与实现(源码+文档)
java·spring boot·后端·mysql·毕业设计·mybatis·源码
MYF_124 小时前
window安装MySQL5.7
mysql
RainbowSea4 小时前
4. MySQL 逻辑架构说明
数据库·sql·mysql