MySQL练习题
文章目录
数据库详情
数据库文件
数据库sql文件下载:
链接:https://pan.baidu.com/s/1I62mf34MFE06LqJt8S9c3g?pwd=imot
提取码:imot
数据库分析
SELECT查询练习
【题目】
# 1. 查询员工12个月的工资总和,并起别名为ANNUAL SALARY
# 2. 查询employees表中去除重复的job_id以后的数据
# 3. 查询工资大于12000的员工姓名和工资
# 4. 查询员工号为176的员工的姓名和部门号
# 5. 显示表 departments 的结构,并查询其中的全部数据
运算符练习
【题目】
# 1. 选择工资不在5000到12000的员工的姓名和工资
# 2. 选择在20或50号部门工作的员工姓名和部门号
# 3. 选择公司中没有管理者的员工姓名及job_id
# 4. 选择公司中有奖金的员工姓名,工资和奖金级别
# 5. 选择员工姓名的第三个字母是a的员工姓名
# 6. 选择姓名中有字母a和k的员工姓名
# 7. 显示出表 employees 表中 first_name 以 'e'结尾的员工信息
# 8. 显示出表 employees 部门编号在 80-100 之间的姓名、工种
# 9. 显示出表 employees 的 manager_id 是 100,101,110 的员工姓名、工资、管理者id
排序与分页练习
【题目】
#1. 查询员工的姓名和部门号和年薪,按年薪降序,按姓名升序显示
#2. 选择工资不在 8000 到 17000 的员工的姓名和工资,按工资降序,显示第21到40位置的数据
#3. 查询邮箱中包含 e 的员工信息,并先按邮箱的字节数降序,再按部门号升序
多表查询练习
多表查询-1
【题目】
# 1. 显示所有员工的姓名,部门号和部门名称。
# 2. 查询90号部门员工的job_id和90号部门的location_id
# 3. 选择所有有奖金的员工的 last_name , department_name , location_id , city
# 4. 选择city在Toronto工作的员工的 last_name , job_id , department_id , department_name
# 5. 查询员工所在的部门名称、部门地址、姓名、工作、工资,其中员工所在部门的部门名称为’Executive’
# 6. 选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号,结果类似于下面的格式
employees Emp # manager Mgr#
kochhar 101 king 100
# 7. 查询哪些部门没有员工
# 8. 查询哪个城市没有部门
# 9. 查询部门名为 Sales 或 IT 的员工信息
多表查询-2
储备:建表操作:
CREATE TABLE `t_dept` (
`id` INT ( 11 ) NOT NULL AUTO_INCREMENT,
`deptName` VARCHAR ( 30 ) DEFAULT NULL,
`address` VARCHAR ( 40 ) DEFAULT NULL,
PRIMARY KEY ( `id` )
) ENGINE = INNODB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
CREATE TABLE `t_emp` (
`id` INT ( 11 ) NOT NULL AUTO_INCREMENT,
`name` VARCHAR ( 20 ) DEFAULT NULL,
`age` INT ( 3 ) DEFAULT NULL,
`deptId` INT ( 11 ) DEFAULT NULL,
empno INT NOT NULL,
PRIMARY KEY ( `id` ),
KEY `idx_dept_id` ( `deptId` ) #CONSTRAINT `fk_dept_id` FOREIGN KEY (`deptId`) REFERENCES `t_dept` (`id`)
) ENGINE = INNODB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
INSERT INTO t_dept(deptName,address) VALUES('华山','华山');
INSERT INTO t_dept(deptName,address) VALUES('丐帮','洛阳');
INSERT INTO t_dept(deptName,address) VALUES('峨眉','峨眉山');
INSERT INTO t_dept(deptName,address) VALUES('武当','武当山');
INSERT INTO t_dept(deptName,address) VALUES('明教','光明顶');
INSERT INTO t_dept(deptName,address) VALUES('少林','少林寺');
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('风清扬',90,1,100001);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('岳不群',50,1,100002);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('令狐冲',24,1,100003);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('洪七公',70,2,100004);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('乔峰',35,2,100005);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('灭绝师太',70,3,100006);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('周芷若',20,3,100007);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('张三丰',100,4,100008);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('张无忌',25,5,100009);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES('韦小宝',18,null,100010);
【题目】
#1.所有有门派的人员信息
( A、B两表共有)
#2.列出所有用户,并显示其机构信息
(A的全集)
#3.列出所有门派
(B的全集)
#4.所有不入门派的人员
(A的独有)
#5.所有没人入的门派
(B的独有)
#6.列出所有人员和机构的对照关系
(AB全有)
#MySQL Full Join的实现 因为MySQL不支持FULL JOIN,下面是替代方法
#left join + union(可去除重复数据)+ right join
#7.列出所有没入派的人员和没人入的门派
(A的独有+B的独有)
单行函数练习
【题目】
# 1.显示系统时间(注:日期+时间)
# 2.查询员工号,姓名,工资,以及工资提高百分之20%后的结果(new salary)
# 3.将员工的姓名按首字母排序,并写出姓名的长度(length)
# 4.查询员工id,last_name,salary,并作为一个列输出,别名为OUT_PUT
# 5.查询公司各员工工作的年数、工作的天数,并按工作年数的降序排序
# 6.查询员工姓名,hire_date , department_id,满足以下条件:雇用时间在1997年之后,department_id
为80 或 90 或110, commission_pct不为空
# 7.查询公司中入职超过10000天的员工姓名、入职时间
# 8.做一个查询,产生下面的结果
<last_name> earns <salary> monthly but wants <salary*3>
Dream Salary |
---|
King earns 24000 monthly but want 72000 |
# 9.使用case-when,按照下面的条件:
job grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
产生下面的结果:
Last_name | Job_id | Grade |
---|---|---|
king | AD_PERS | A |
聚合函数练习
题目答案
SELECT查询
-
查询员工12个月的工资总和,并起别名为ANNUAL SALARY
#不包括奖金 SELECT employee_id , last_name,salary * 12 "ANNUAL SALARY" FROM employees; #包括奖金的工资 SELECT employee_id,last_name,salary * 12 * (1 + IFNULL(commission_pct,0)) "ANNUAL SALARY" FROM employees;
-
查询employees表中去除重复的job_id以后的数据
SELECT DISTINCT job_id FROM employees;
-
查询工资大于12000的员工姓名和工资
SELECT last_name, salary FROM employees WHERE salary > 12000;
-
查询员工号为176的员工的姓名和部门号
SELECT last_name, department_id FROM employees WHERE employee_id = 176;
-
显示表 departments 的结构,并查询其中的全部数据
DESC departments; SELECT * FROM departments;
运算符
-
选择工资不在5000到12000的员工的姓名和工资
# 传统写法 SELECT last_name, salary FROM employees WHERE salary < 5000 OR salary > 12000; # between and 写法 SELECT last_name, salary FROM employees WHERE salary NOT BETWEEN 5000 AND 12000;
-
选择在20或50号部门工作的员工姓名和部门号
# 第一种方式 SELECT last_name, department_id FROM employees WHERE department_id = 20 OR department_id = 50; # 第二种方式 SELECT last_name, department_id FROM employees WHERE department_id IN(20, 50);
-
选择公司中没有管理者的员工姓名及job_id
SELECT last_name, job_id FROM employees WHERE manager_id IS NULL;
-
选择公司中有奖金的员工姓名,工资和奖金级别
SELECT last_name, salary, commission_pct FROM employees WHERE commission_pct IS NOT NULL;
-
选员工姓名的第三个字母是a的员工姓名
SELECT last_name FROM employees WHERE last_name LIKE '__a%';
-
选择姓名中有字母a和k的员工姓名
# 使用 OR SELECT last_name FROM employees WHERE last_name LIKE '%a%k%' OR last_name LIKE '%k%a%'; # 使用 AND SELECT last_name FROM employees WHERE last_name LIKE '%a%' AND last_name LIKE '%k%';
-
显示出表 employees 表中 first_name 以 'e’结尾的员工信息
# 使用通配符 SELECT employee_id,first_name,last_name FROM employees WHERE first_name LIKE '%e'; # 使用正则 SELECT employee_id,first_name,last_name FROM employees WHERE first_name REGEXP 'e$';
-
显示出表 employees 部门编号在 80-100 之间的姓名、工种
SELECT last_name,job_id FROM employees WHERE department_id BETWEEN 80 AND 100;
-
显示出表 employees 的 manager_id 是 100,101,110 的员工姓名、工资、 管理者id
SELECT last_name,salary,manager_id FROM employees WHERE manager_id IN (100,101,110);
分页与排序
-
查询员工的姓名和部门号和年薪,按年薪降序 按姓名升序显示
SELECT last_name,department_id,salary * 12 annual_sal FROM employees ORDER BY annual_sal DESC,last_name ASC;
-
选择工资不在 8000 到 17000 的员工的姓名和工资,按工资降序,显示第 21到40位置的数据
SELECT last_name,salary FROM employees WHERE salary NOT BETWEEN 8000 AND 17000 ORDER BY salary DESC LIMIT 20,20;
-
查询邮箱中包含 e 的员工信息,并先按邮箱的字节数降序,再按部门号 升序
SELECT last_name,email,department_id FROM employees WHERE email REGEXP '[e]' ORDER BY LENGTH(email) DESC,department_id ASC;
多表查询
多表查询-1
-
显示所有员工的姓名,部门号和部门名称
SELECT last_name, e.department_id, department_name FROM employees e LEFT OUTER JOIN departments d ON e.`department_id` = d.`department_id`;
-
查询90号部门员工的job_id和90号部门的location_id
SELECT job_id, location_id FROM employees e, departments d WHERE e.`department_id` = d.`department_id` AND e.`department_id` = 90; # 或者 SELECT job_id, location_id FROM employees e JOIN departments d ON e.`department_id` = d.`department_id` WHERE e.`department_id` = 90;
-
选择所有有奖金的员工的 last_name , department_name , location_id , city
SELECT last_name , department_name , d.location_id , city FROM employees e LEFT OUTER JOIN departments d ON e.`department_id` = d.`department_id` LEFT OUTER JOIN locations l ON d.`location_id` = l.`location_id` WHERE commission_pct IS NOT NULL;
-
选择city在Toronto工作的员工的 last_name , job_id , department_id , department_name
# SQL 92 SELECT last_name , job_id , e.department_id , department_name FROM employees e, departments d, locations l WHERE e.`department_id` = d.`department_id` AND d.`location_id` = l.`location_id` AND city = 'Toronto'; # SQL 99 SELECT last_name , job_id , e.department_id , department_name FROM employees e JOIN departments d ON e.`department_id` = d.`department_id` JOIN locations l ON l.`location_id` = d.`location_id` WHERE l.`city` = 'Toronto';
-
查询员工所在的部门名称、部门地址、姓名、工作、工资,其中员工所 在部门的部门名称为’Executive’
SELECT department_name, street_address, last_name, job_id, salary FROM employees e JOIN departments d ON e.department_id = d.department_id JOIN locations l ON d.`location_id` = l.`location_id` WHERE department_name = 'Executive'
-
选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号
SELECT emp.last_name employees, emp.employee_id "Emp#", mgr.last_name manager, mgr.employee_id "Mgr#" FROM employees emp LEFT OUTER JOIN employees mgr ON emp.manager_id = mgr.employee_id;
-
查询哪些部门没有员工
#方式1: SELECT d.department_id FROM departments d LEFT JOIN employees e ON e.department_id = d.`department_id` WHERE e.department_id IS NULL #方式2: SELECT department_id FROM departments d WHERE NOT EXISTS ( SELECT * FROM employees e WHERE e.`department_id` = d.`department_id` )
-
查询哪个城市没有部门
SELECT l.location_id,l.city FROM locations l LEFT JOIN departments d ON l.`location_id` = d.`location_id` WHERE d.`location_id` IS NULL
-
查询部门名为 Sales 或 IT 的员工信息
SELECT employee_id,last_name,department_name FROM employees e,departments d WHERE e.department_id = d.`department_id` AND d.`department_name` IN ('Sales','IT');
多表查询-2
-
所有有门派的人员信息
/* ( A、B两表共有) */ select * from t_emp a inner join t_dept b on a.deptId = b.id;
-
列出所有用户,并显示其机构信息
/* (A的全集) */ select * from t_emp a left join t_dept b on a.deptId = b.id;
-
列出所有门派
/* B的全集 */ select * from t_dept b;
-
所有不入门派的人员
/* (A的独有) */ select * from t_emp a left join t_dept b on a.deptId = b.id where b.id is null;
-
所有没人入的门派
/* (B的独有) */ select * from t_dept b left join t_emp a on a.deptId = b.id where a.deptId is null;
-
列出所有人员和机构的对照关系
/* ( AB全有) #MySQL Full Join的实现 因为MySQL不支持FULL JOIN,下面是替代方法 #left join + union(可去除重复数据)+ right join */ SELECT * FROM t_emp A LEFT JOIN t_dept B ON A.deptId = B.id UNION SELECT * FROM t_emp A RIGHT JOIN t_dept B ON A.deptId = B.id
-
列出所有没入派的人员和没人入的门派
/* (A的独有+B的独有) */ SELECT * FROM t_emp A LEFT JOIN t_dept B ON A.deptId = B.id WHERE B.`id` IS NULL UNION SELECT * FROM t_emp A RIGHT JOIN t_dept B ON A.deptId = B.id WHERE A.`deptId` IS NULL;
单行函数
-
显示系统时间(注:日期+时间)
SELECT NOW() FROM DUAL;
-
查询员工号,姓名,工资,以及工资提高百分之20%后的结果(new salary)
SELECT employee_id, last_name, salary, salary * 1.2 "new salary" FROM employees;
-
将员工的姓名按首字母排序,并写出姓名的长度(length)
SELECT last_name, LENGTH(last_name) FROM employees ORDER BY last_name DESC;
-
查询员工id,last_name,salary,并作为一个列输出,别名为OUT_PUT
SELECT CONCAT(employee_id, ',' , last_name , ',', salary) OUT_PUT FROM employees;
-
查询公司各员工工作的年数、工作的天数,并按工作年数的降序排序
SELECT DATEDIFF(SYSDATE(), hire_date) / 365 worked_years, DATEDIFF(SYSDATE(), hire_date) worked_days FROM employees ORDER BY worked_years DESC
-
查询员工姓名,hire_date , department_id,满足以下条件:雇用时间在 1997年之后,department_id 为80 或 90 或110, commission_pct不为空
SELECT last_name, hire_date, department_id FROM employees #WHERE hire_date >= '1997-01-01' #WHERE hire_date >= STR_TO_DATE('1997-01-01', '%Y-%m-%d') WHERE DATE_FORMAT(hire_date,'%Y') >= '1997' AND department_id IN (80, 90, 110) AND commission_pct IS NOT NULL
-
查询公司中入职超过10000天的员工姓名、入职时间
SELECT last_name,hire_date FROM employees #WHERE TO_DAYS(NOW()) - to_days(hire_date) > 10000; WHERE DATEDIFF(NOW(),hire_date) > 10000;
-
做一个查询,产生下面的结果
-- <last_name> earns `<salary>` monthly but wants <salary*3> -- Dream Salary -- King earns 24000 monthly but wants 72000
SELECT CONCAT(last_name, ' earns ', TRUNCATE(salary, 0) , ' monthly but wants ', TRUNCATE(salary * 3, 0)) "Dream Salary" FROM employees;
-
使用CASE-WHEN,按照下面的条件:
-- job grade -- AD_PRES A -- ST_MAN B -- IT_PROG C -- SA_REP D -- ST_CLERK E -- 产生下面的结果 -- Last_name Job_id Grade -- king AD_PRES A
SELECT last_name Last_name, job_id Job_id, CASE job_id WHEN 'AD_PRES' THEN 'A' WHEN 'ST_MAN' THEN 'B' WHEN 'IT_PROG' THEN 'C' WHEN 'SA_REP' THEN 'D' WHEN 'ST_CLERK' THEN 'E' ELSE 'F' END "grade" FROM employees;