这部分主要是为了帮助大家回忆回忆MySQL的基本语法,数据库来自于MySQL的官方简化版,题目也是网上非常流行的35题。这些基础习题基本可以涵盖面试中需要现场写SQL的问题。
创建数据库
在开始练习之前,我默认你的电脑上是没有本系列练习题需要的数据库。
第一步,准备数据
我这里写好一个.sql文件,放在了网盘里面
链接:pan.baidu.com/s/19r4ap0RB... 提取码:aigo
大小仅仅2KB,下载应该会很快。
我将这个文件放在了 /usr/src/ 下面
csharp
[09:16:13 root@MySQL-server src]#ls
data.sql debug kernels
[09:16:15 root@MySQL-server src]#
那么该文件要如何使用呢?
第二步,创建数据库
接下来登陆MySQL,
less
(root@localhost) [(none)]>show databases;
+--------------------+
| Database |
+--------------------+
| book |
| information_schema |
| mysql |
| performance_schema |
| springbootdb |
| sys |
+--------------------+
6 rows in set (0.03 sec)
(root@localhost) [(none)]>
那么我需要建一个数据库,就叫employees,一系列命令如下。
less
(root@localhost) [(none)]>create database employees;
Query OK, 1 row affected (0.10 sec)
(root@localhost) [(none)]>use employees;
Database changed
(root@localhost) [employees]>show tables;
Empty set (0.14 sec)
(root@localhost) [employees]>
第三步,导入数据
很简单且朴实无华的一条命令
bash
source /usr/src/data.sql
执行过后
sql
(root@localhost) [employees]>show tables;
+---------------------+
| Tables_in_employees |
+---------------------+
| dept |
| emp |
| salgrade |
+---------------------+
3 rows in set (0.00 sec)
该数据库中就出现了三张表
那么分别对三张表执行select *
yaml
(root@localhost) [employees]>select * from dept;
+--------+-------------+----------+
| deptno | dname | loc |
+--------+-------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCHING | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+-------------+----------+
4 rows in set (0.03 sec)
(root@localhost) [employees]>
(root@localhost) [employees]>select * from salgrade;
+-------+-------+-------+
| grade | losal | hisal |
+-------+-------+-------+
| 1 | 700 | 1200 |
| 2 | 1201 | 1400 |
| 3 | 1401 | 2000 |
| 4 | 2001 | 3000 |
| 5 | 3001 | 5000 |
+-------+-------+-------+
5 rows in set (0.05 sec)
(root@localhost) [employees]>
(root@localhost) [employees]>select * from emp;
+-------+--------+-----------+------+------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7369 | SIMITH | 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 | 2450.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | NULL | 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 | 1300.00 | NULL | 10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.00 sec)
(root@localhost) [employees]>
简单练习一下
那今天就先做一道简单的练习题
取得每个部门最高薪水的人员名称
首先分析一下题目,该题目需要返回的是人员名称,其次该人员是该部门薪资最高的人。 人员名称,部门以及薪资都在emp一个表里,那么最多也就是自己join自己
拆解
第一步,我就去看看每个部门最高的薪水。
less
(root@localhost) [employees]>select deptno,max(sal) as maxsal from emp group by deptno;
+--------+---------+
| deptno | maxsal |
+--------+---------+
| 20 | 3000.00 |
| 30 | 2850.00 |
| 10 | 5000.00 |
+--------+---------+
3 rows in set (0.03 sec)
(root@localhost) [employees]>
第二步,自己join自己,条件为 薪资=最大薪资 且 部门相同
less
(root@localhost) [employees]>select a.ename,a.deptno,a.sal from emp a join(select deptno,max(sal) as maxsal from emp group by deptno) b on a.sal=b.maxsal and a.deptno=b.deptno;
+-------+--------+---------+
| ename | deptno | sal |
+-------+--------+---------+
| BLAKE | 30 | 2850.00 |
| SCOTT | 20 | 3000.00 |
| KING | 10 | 5000.00 |
| FORD | 20 | 3000.00 |
+-------+--------+---------+
4 rows in set (0.07 sec)
(root@localhost) [employees]>
总结
今天主要是帮助大家建库建表,以及简单地练习一道题。希望能帮助到读者老爷。