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

相关推荐
Java水解13 小时前
Mysql查看执行计划、explain关键字详解(超详细)
后端·mysql
知其然亦知其所以然16 小时前
MySQL 社招必考题:如何优化查询过程中的数据访问?
后端·mysql·面试
DemonAvenger18 小时前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
程序新视界18 小时前
如何在MySQL中创建聚集索引?
mysql
AAA修煤气灶刘哥1 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
程序新视界1 天前
学习MySQL绕不开的两个基础概念:聚集索引与非聚集索引
mysql
RestCloud1 天前
跨境数据传输:ETL如何处理时区与日期格式差异
mysql·api
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
xiaok2 天前
mysql中怎么创建一个可控权限数据库账号密码给到开发者
mysql
ByteBlossom2 天前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试