文章目录

  • 一、数据库概述练习题
  • 二、MySQL 环境搭建练习题
  • 三、查询练习


MySQL 数据库练习题(包含前5章所有知识点及答案)

一、数据库概述练习题

1、说说你了解的常见的数据库。
2、谈谈你对MySQL历史、特点的理解。
3、说说你对 DB、DBMS、SQL 的理解。
4、你知道哪些非关系型数据库的类型呢?
5、表与表的记录之间存在哪些关联关系?

二、MySQL 环境搭建练习题

1、安装好 MySQL 之后在 Windows 和 Linux 系统中哪些位置能看到 MySQL?
2、Windows 卸载 MySQL 主要卸载哪几个位置的内容?
3、能够独立完成 MySQL8.0、MySQL5.7 版本的下载、安装、配置 (掌握)。
4、MySQL5.7 在配置完以后,如何修改配置文件?
5、熟悉常用的数据库管理和操作的工具。

三、查询练习

1、查询员工12个月的工资总和,并起别名为 ANNUAL SALARY。

SELECT employee_id,last_name,salary * 12 AS "ANNUAL SALARY" FROM employees;
SELECT employee_id,last_name,salary * 12 * (1 + IFNULL(commission_pct,0)) "ANNUAL SALARY" FROM employees;

2、查询 employees 表中去除重复的 job_id 以后的数据。

SELECT DISTINCT job_id FROM employees;

3、查询工资大于 12000 的员工姓名和工资。

mysql> SELECT last_name,salary FROM employees WHERE salary>12000;

4、查询员工号为 176 的员工的姓名和部门号。

mysql> SELECT last_name,department_id FROM employees WHERE employee_id=176;
+-----------+---------------+
| last_name | department_id |
+-----------+---------------+
| Taylor    |            80 |
+-----------+---------------+
1 row in set (0.00 sec)

5、显示表 departments 的结构,并查询其中的全部数据。

mysql> DESC departments;
+-----------------+-------------+------+-----+---------+-------+
| Field           | Type        | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| department_id   | int         | NO   | PRI | 0       |       |
| department_name | varchar(30) | NO   |     | NULL    |       |
| manager_id      | int         | YES  | MUL | NULL    |       |
| location_id     | int         | YES  | MUL | NULL    |       |
+-----------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM departments;

6、选择工资不在 5000 到 12000 的员工的姓名和工资。

mysql> SELECT last_name,salary FROM employees WHERE NOT salary BETWEEN 5000 AND 12000;
mysql> SELECT last_name,salary FROM employees WHERE salary < 5000 OR salary > 12000;

7、选择在20或50号部门工作的员工姓名和部门号。

SELECT last_name,department_id FROM employees WHERE department_id IN(20, 50);
SELECT last_name,department_id FROM employees WHERE department_id = 20 OR department_id = 50;

8、选择公司中没有管理者的员工姓名及job_id。

mysql> SELECT last_name,job_id FROM employees WHERE manager_id <=> NULL;
+-----------+---------+
| last_name | job_id  |
+-----------+---------+
| King      | AD_PRES |
+-----------+---------+
1 row in set (0.00 sec)

mysql> SELECT last_name,job_id FROM employees WHERE manager_id IS NULL;
+-----------+---------+
| last_name | job_id  |
+-----------+---------+
| King      | AD_PRES |
+-----------+---------+
1 row in set (0.00 sec)

mysql> SELECT last_name,job_id FROM employees WHERE ISNULL(manager_id);
+-----------+---------+
| last_name | job_id  |
+-----------+---------+
| King      | AD_PRES |
+-----------+---------+
1 row in set (0.00 sec)

mysql> SELECT last_name,job_id FROM employees WHERE NOT manager_id IS NOT NULL;
+-----------+---------+
| last_name | job_id  |
+-----------+---------+
| King      | AD_PRES |
+-----------+---------+
1 row in set (0.00 sec)

9、选择公司中有奖金的员工姓名,工资和奖金级别。

mysql> SELECT last_name,salary,commission_pct FROM employees WHERE commission_pct IS NOT NULL;

10、选择员工姓名的第三个字母是a的员工姓名。

mysql> SELECT last_name FROM employees WHERE last_name LIKE '__a%';

11、选择姓名中有字母a和k的员工姓名。

mysql> SELECT last_name FROM employees WHERE last_name LIKE '%a%k%' OR last_name LIKE '%K%a%';
+------------+
| last_name  |
+------------+
| Kochhar    |
| Kaufling   |
| Markle     |
| Atkinson   |
| Philtanker |
| Kumar      |
+------------+

12、显示出表 employees 表中 first_name 以 'e' 结尾的员工信息。

mysql> SELECT first_name FROM employees WHERE first_name LIKE '%e';
mysql> SELECT first_name FROM employees WHERE first_name REGEXP 'e$';

13、显示出表 employees 部门编号在 80-100 之间的姓名、工种。

mysql> SELECT last_name,job_id FROM employees WHERE department_id BETWEEN 80 AND 100;

14、显示出表 employees 的 manager_id 是 100,101,110 的员工姓名、工资、管理者id。

mysql> SELECT last_name,job_id FROM employees WHERE department_id BETWEEN 80 AND 100;

15、查询员工的姓名和部门号和年薪,按年薪降序,按姓名升序显示。

SELECT last_name,department_id,salary * 12 AS "AnnualSalary" FROM employees ORDER BY AnnualSalary DESC, last_name ASC;

16、选择工资不在 8000 到 17000 的员工的姓名和工资,按工资降序,显示第21到40位置的数据。

# 注意初始位置是从0开始的,显示第21条记录之后的数据,初始位置就应该为20
SELECT last_name,salary FROM employees WHERE NOT salary BETWEEN 8000 AND 17000 ORDER BY salary DESC LIMIT 20 OFFSET 20;
SELECT last_name,salary FROM employees WHERE NOT salary BETWEEN 8000 AND 17000 ORDER BY salary DESC LIMIT 20, 20;

17、查询邮箱中包含E的员工姓名,邮箱及部门号,并先按邮箱的字节数降序,再按部门号升序。

SELECT last_name,email,department_id FROM employees WHERE email LIKE '%E%' ORDER BY LENGTH(email) DESC, department_id ASC;
SELECT last_name,email,department_id FROM employees WHERE email REGEXP '[E]' ORDER BY LENGTH(email) DESC, department_id ASC;

至此今天的学习就到此结束了,笔者在这里声明,笔者写文章只是为了学习交流,以及让更多学习数据库的读者少走一些弯路,节省时间,并不用做其他用途,如有侵权,联系博主删除即可。感谢您阅读本篇博文,希望本文能成为您编程路上的领航者。祝您阅读愉快!