MYSQY 学习笔记之 练习题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQY 学习笔记之 练习题相关的知识,希望对你有一定的参考价值。

MYSQY

试题一:取得每个部门最高薪水的人员名单?

分析:

① 先将员工表依据部门编号进行分组并找出每个分组中最高薪水的人员信息

②将最高薪水表与原表进行连接

#员工信息表
mysql> select * from emp;
+-------+--------+-----------+------+------------+---------+---------+--------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 1000.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 1000.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1000.00 | NULL | 10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.00 sec)
#按照部门进行分组并查处每个组中最高薪水
mysql> select deptno ,max(sal) maxsalary from emp group by deptno;
+--------+-----------+
| deptno | maxsalary |
+--------+-----------+
| 20 | 3000.00 |
| 30 | 2850.00 |
| 10 | 1000.00 |
+--------+-----------+
3 rows in set (0.00 sec)
#表连接
mysql> select e.ename,t.* from emp e join (select deptno,max(sal) as maxsal from emp group by deptno ) t on t.deptno =e.deptno and t.maxsal = e.sal;
+--------+--------+---------+
| ename | deptno | maxsal |
+--------+--------+---------+
| SCOTT | 20 | 3000.00 |
| FORD | 20 | 3000.00 |
| BLAKE | 30 | 2850.00 |
| CLARK | 10 | 1000.00 |
| KING | 10 | 1000.00 |
| MILLER | 10 | 1000.00 |
+--------+--------+---------+
6 rows in set (0.00 sec)
试题二:哪些人的薪水在部门的平均薪资之上?

分析:

①按照部门进行分组并找出每个部门的平均薪资(作为t表)

③表连接

      条件如下:I. 两张表的deptno相同 

                      II. 员工表的员工薪水 大于  t表中的平均薪水                         

#按照部门进行分组后然后再求出每个部门的平均薪资
mysql> select deptno, avg(sal) avgsal from emp group by deptno;
+--------+-------------+
| deptno | avgsal |
+--------+-------------+
| 20 | 2175.000000 |
| 30 | 1566.666667 |
| 10 | 1000.000000 |
+--------+-------------+
3 rows in set (0.00 sec)

#表连接
mysql> select t.*,e.ename,e.sal from emp e join (select deptno, avg(sal) avgsal from emp group by deptno) t on e.deptno = t.deptno and e.sal>t.avgsal;
+--------+-------------+-------+---------+
| deptno | avgsal | ename | sal |
+--------+-------------+-------+---------+
| 30 | 1566.666667 | ALLEN | 1600.00 |
| 20 | 2175.000000 | JONES | 2975.00 |
| 30 | 1566.666667 | BLAKE | 2850.00 |
| 20 | 2175.000000 | SCOTT | 3000.00 |
| 20 | 2175.000000 | FORD | 3000.00 |
+--------+-------------+-------+---------+
5 rows in set (0.00 sec)
试题三:取得部门中(所有人的)平均的薪水等级??

分析:

区分: 平均的薪水等级VS 平均薪水的等级

          平均的薪水等级:先求每个员工的薪水等级,然后再求平均值

          平均薪水的等级:先求薪水的平均值,然后再求薪水等级

① 先显示出每个部门中所有员工的基本信息及薪水等级

②薪水等级求平均值


#先显示出每个部门中所有员工的基本信息及薪水等级
#方式一:
mysql> select e.deptno,e.ename,e.sal ,d.grade from salgrade d,emp e where e.sal between d.losal and d.hisal order by e.deptno;
+--------+--------+---------+-------+
| deptno | ename | sal | grade |
+--------+--------+---------+-------+
| 10 | CLARK | 1000.00 | 1 |
| 10 | KING | 1000.00 | 1 |
| 10 | MILLER | 1000.00 | 1 |
| 20 | SMITH | 800.00 | 1 |
| 20 | ADAMS | 1100.00 | 1 |
| 20 | JONES | 2975.00 | 4 |
| 20 | SCOTT | 3000.00 | 4 |
| 20 | FORD | 3000.00 | 4 |
| 30 | JAMES | 950.00 | 1 |
| 30 | WARD | 1250.00 | 2 |
| 30 | MARTIN | 1250.00 | 2 |
| 30 | TURNER | 1500.00 | 3 |
| 30 | ALLEN | 1600.00 | 3 |
| 30 | BLAKE | 2850.00 | 4 |
+--------+--------+---------+-------+
14 rows in set (0.00 sec)
#方式二:通过join...on..的方式实现表的连接
mysql> select e.deptno,e.ename,e.sal ,d.grade from salgrade d join emp e on e.sal between d.losal and d.hisal order by e.deptno;
+--------+--------+---------+-------+
| deptno | ename | sal | grade |
+--------+--------+---------+-------+
| 10 | CLARK | 1000.00 | 1 |
| 10 | KING | 1000.00 | 1 |
| 10 | MILLER | 1000.00 | 1 |
| 20 | SMITH | 800.00 | 1 |
| 20 | ADAMS | 1100.00 | 1 |
| 20 | JONES | 2975.00 | 4 |
| 20 | SCOTT | 3000.00 | 4 |
| 20 | FORD | 3000.00 | 4 |
| 30 | JAMES | 950.00 | 1 |
| 30 | WARD | 1250.00 | 2 |
| 30 | MARTIN | 1250.00 | 2 |
| 30 | TURNER | 1500.00 | 3 |
| 30 | ALLEN | 1600.00 | 3 |
| 30 | BLAKE | 2850.00 | 4 |
+--------+--------+---------+-------+
14 rows in set (0.00 sec)

#薪水等级求平均值
mysql> select e.deptno,e.ename,e.sal ,avg(d.grade) from salgrade d,emp e where e.sal between d.losal and d.hisal group by e.deptno;
+--------+-------+---------+--------------+
| deptno | ename | sal | avg(d.grade) |
+--------+-------+---------+--------------+
| 20 | SMITH | 800.00 | 2.8000 |
| 30 | JAMES | 950.00 | 2.5000 |
| 10 | CLARK | 1000.00 | 1.0000 |
+--------+-------+---------+--------------+
3 rows in set (0.00 sec)
试题四:不准用组函数(Max),取得最高薪水(给出两种方案

分析:

方案一:sal降序,limit1

      ①将表依据薪水的高低进行排序

      ②然后通过limit进行取出最高薪水

方案二:表的自连接

      ①通过自连接生成一薪水表(原表的最高薪水不在其中)

      ②通过not in 的方式查出最高薪水      

#方案一:
#将表按照薪水高低降序排序
mysql> select ename,sal from emp order by sal desc ;
+--------+---------+
| ename | sal |
+--------+---------+
| SCOTT | 3000.00 |
| FORD | 3000.00 |
| JONES | 2975.00 |
| BLAKE | 2850.00 |
| ALLEN | 1600.00 |
| TURNER | 1500.00 |
| WARD | 1250.00 |
| MARTIN | 1250.00 |
| ADAMS | 1100.00 |
| CLARK | 1000.00 |
| KING | 1000.00 |
| MILLER | 1000.00 |
| JAMES | 950.00 |
| SMITH | 800.00 |
+--------+---------+
14 rows in set (0.00 sec)
#通过limit来取出排序表中的第一个记录
mysql> select ename,sal from emp order by sal desc limit 1;
+-------+---------+
| ename | sal |
+-------+---------+
| FORD | 3000.00 |
+-------+---------+
1 row in set (0.00 sec)

#方式二:自连接
mysql> select sal from emp;
+---------+
| sal |
+---------+
| 800.00 |
| 950.00 |
| 1000.00 |
| 1000.00 |
| 1000.00 |
| 1100.00 |
| 1250.00 |
| 1250.00 |
| 1500.00 |
| 1600.00 |
| 2850.00 |
| 2975.00 |
| 3000.00 |
| 3000.00 |
+---------+
14 rows in set (0.00 sec)
#通过自连接得到的结果中只有 3000(最高薪)不满足(distinct是用来去重的)
mysql> select distinct a.sal from emp a join emp b on a.sal < b.sal;
+---------+
| sal |
+---------+
| 800.00 |
| 950.00 |
| 1000.00 |
| 1100.00 |
| 1250.00 |
| 1500.00 |
| 1600.00 |
| 2850.00 |
| 2975.00 |
+---------+
9 rows in set (0.00 sec)

mysql> select sal from emp where sal not in (select distinct a.sal from emp a join emp b on a.sal < b.sal);
+---------+
| sal |
+---------+
| 3000.00 |
| 3000.00 |
+---------+
2 rows in set (0.00 sec)

#去重
mysql> select distinct sal from emp where sal not in (select distinct a.sal from emp a join emp b on a.sal < b.sal);
+---------+
| sal |
+---------+
| 3000.00 |
+---------+
1 row in set (0.00 sec)
试题五:取得平均薪水最高的部门的部门编号(至少给出两种方案)

分析:

方案一:limit

    ①将员工表根据部门进行分组

    ②根据函数(avg)求出每个组中的平均薪水

    ③根据薪水进行排序

    ④然后通过limit选取薪水最高的部门

方案二:max

    ①将员工表根据部门进行分组并求出平均值

    ②通过max函数求出最大的


#方案一
#根据部门进行分组、求出平均值、排序
mysql> select deptno,avg(sal) avgsal from emp group by deptno order by avg(sal) desc;
+--------+-------------+
| deptno | avgsal |
+--------+-------------+
| 20 | 2175.000000 |
| 30 | 1566.666667 |
| 10 | 1000.000000 |
+--------+-------------+
3 rows in set (0.00 sec)

#通过limit选取第一个记录
mysql> select deptno,avg(sal) avgsal from emp group by deptno order by avg(sal) desc limit 1;
+--------+-------------+
| deptno | avgsal |
+--------+-------------+
| 20 | 2175.000000 |
+--------+-------------+
1 row in set (0.00 sec)

mysql> select deptno from emp group by deptno order by avg(sal) desc limit 1;
+--------+
| deptno |
+--------+
| 20 |
+--------+
1 row in set (0.00 sec)

方案二:max

#第一步:求出每个部门的平均薪水
mysql> select deptno,avg(sal) avgsal from emp group by deptno order by avg(sal) desc;
+--------+-------------+
| deptno | avgsal |
+--------+-------------+
| 20 | 2175.000000 |
| 30 | 1566.666667 |
| 10 | 1000.000000 |
+--------+-------------+
3 rows in set (0.00 sec)

#通过max函数找到平均薪水最高的部门
mysql> select t.deptno,max(t.avgsal) maxavgsal from (select deptno,avg(sal) avgsal from emp group by deptno order by avg(sal) desc) t;
+--------+-------------+
| deptno | maxavgsal |
+--------+-------------+
| 20 | 2175.000000 |
+--------+-------------+
1 row in set (0.00 sec)

以上是关于MYSQY 学习笔记之 练习题的主要内容,如果未能解决你的问题,请参考以下文章

Mysql练习题13道(21-33题)

Mysql 练习题10道(11-20题)

Oracle 数据库基础学习 SQL语句综合练习

mysql 查询练习题

学习一门语言必须要有的精神之如何将一道练习题做到极致(Java+云数据库)

MySQL之多表查询练习