在学习Mysql高级之前又去复习了一遍mysql的基础知识,针对此次的复习也做了一些练习的记载。虽然此次练习的内容跟第一篇mysql的学习记录差别不是特别大,但是有些细节上的东西还是非常值得记录的。
1. 显示表 departments 的结构,并查询其中的全部数据
DESC departments;
SELECT * FROM departments;
2. 显示出表 employees 中的全部 job_id(不能重复)
SELECT DISTINCT job_id FROM employees;
3. 显示出表 employees 的全部列,各个列之间用逗号连接,列头显示成 OUT_PUT
SELECT * FROM employees;
SELECT CONCAT(employee_id,",",first_name,",",last_name,",",email,",
",phone_number,",",job_id,",",salary,",",IFNULL(commission_pct,0),",",
IFNULL(manager_id,0),",",department_id,",",hiredate) out_put FROM employees;
#注意使用concat连接的时候,如果连接的字段中存在NULL的字段,那么连接起来的字符串将会是NULL
4. 查询工资大于 12000 的员工姓名和工资
SELECT last_name,salary FROM employees WHERE salary>12000;
5. 查询员工号为 176 的员工的姓名和部门号和年薪
SELECT last_name,department_id,salary*12 年薪 FROM employees WHERE employee_id=176;
6. 选择工资不在 5000 到 12000 的员工的姓名和工资
SELECT last_name ,salary FROM employees WHERE salary NOT BETWEEN 5000 AND 12000;
7. 选择在 20 或 50 号部门工作的员工姓名和部门号
SELECT last_name,department_id FROM employees WHERE department_id IN(20,50);
8. 选择公司中没有管理者的员工姓名及 job_id
SELECT last_name,job_id FROM employees WHERE manager_id IS NULL;
9. 选择公司中有奖金的员工姓名,工资和奖金级别
SELECT last_name,salary,
CASE
WHEN salary>18000 THEN 'A'
WHEN salary>9000 THEN 'B'
WHEN salary>4800 THEN 'C'
ELSE 'D'
END AS '工资级别'
FROM employees
WHERE commission_pct IS NOT NULL;
10. 选择员工姓名的第三个字母是 a 的员工姓名
SELECT last_name FROM employees WHERE last_name LIKE '__a%';
11. 选择姓名中有字母 a 和 e 的员工姓名
SELECT last_name FROM employees WHERE last_name LIKE '%a%' AND last_name LIKE '%e%';
12. 显示出表 employees 表中 first_name 以 'e'结尾的员工信息
SELECT * FROM employees WHERE first_name LIKE '%e';
13. 显示出表 employees 部门编号在 80-100 之间 的姓名、职位
SELECT last_name,department_id FROM employees WHERE department_id BETWEEN 80 AND 100;
14. 显示出表 employees 的 manager_id 是 100,101,110 的员工姓名、职位
SELECT e.manager_id, e.last_name,e.job_id,d.department_name FROM employees e,departments d WHERE e.manager_id IN (100,101,110) AND e.department_id=d.department_id;
15. 查询员工的姓名和部门号和年薪,按年薪降序 按姓名升序
SELECT last_name,department_id,salary*12 年薪 FROM employees ORDER BY 年薪 DESC,last_name ASC;
16. 选择工资不在 8000 到 17000 的员工的姓名和工资,按工资降序
SELECT last_name,salary FROM employees WHERE salary NOT BETWEEN 8000 AND 17000 ORDER BY salary DESC;
17. 查询邮箱中包含 e 的员工信息,并先按邮箱的字节数降序,再按部门号升序
SELECT * FROM employees WHERE email LIKE '%e%' ORDER BY LENGTH(email) DESC,department_id ASC;
18. 显示系统时间(注:日期+时间)
SELECT DATE_FORMAT(NOW(),'%Y年%m月%d日%H时%i分%s秒') AS 当前时间;
SELECT NOW();
19. 查询员工号,姓名,工资,以及工资提高百分之 20%后的结果(NEW salary)
SELECT employee_id,last_name,salary,salary*1.2 'new salary' FROM employees;
20. 将员工的姓名按 首字母排序,并写出姓名的长度(LENGTH)
SELECT LENGTH(last_name) 长度,SUBSTR(last_name,1,1) 首字母,last_name FROM employees ORDER BY SUBSTR(last_name,1,1) ASC;
21. 做一个查询,产生下面的结果 earns monthly but wants 3>
SELECT CONCAT(last_name,' earns ',salary,' monthly but wants ',salary*3) 结果 FROM employees;
22. 员工工种级别
SELECT last_name,job_id,
CASE
WHEN job_id='AD_PRES' THEN 'A'
WHEN job_id='ST_MAN' THEN 'B'
WHEN job_id='IT_PROG' THEN 'C'
WHEN job_id='SA_REP' THEN 'D'
WHEN job_id='ST_CLERK' THEN 'E'
ELSE 'F'
END AS '工种级别'
FROM employees;
23. 查询各 job_id 的员工工资的最大值,最小值,平均值,总和,并按 job_id 升序
SELECT job_id,MAX(salary) 最大值,MIN(salary) 最小值,AVG(salary) 平均值,SUM(salary) 总和 FROM employees GROUP BY job_id ORDER BY job_id ASC; # LIMIT 1,2
24. 查询员工最高工资和最低工资的差距(DIFFERENCE)
SELECT MAX(salary)-MIN(salary) DIFFERENCE FROM employees;
25. 查询各个管理者手下员工的最低工资,其中最低工资不能低于 6000,没有管理者的员工不计算在内
SELECT MIN(salary) 最底工资 FROM employees WHERE manager_id IS NOT NULL GROUP BY manager_id HAVING MIN(salary)>6000;
#注意select查询列的别名不能用
26. 查询所有部门的编号,员工数量和工资平均值,并按平均工资降序
SELECT department_id,COUNT(*),AVG(salary) FROM employees GROUP BY department_id ORDER BY AVG(salary) DESC;
27. 选择具有各个 job_id 的员工人数
SELECT COUNT(*) FROM employees GROUP BY job_id;
28. 查询编号>3 的女神的男朋友信息,如果有则列出详细,如果没有,用 NULL 填充
SELECT g.id ,b.* FROM beauty g,boys b WHERE g.id>3 AND g.boyfriend_id = b.id;
29. 查询哪个城市没有部门
SELECT * FROM locations l LEFT OUTER JOIN departments d ON l.location_id=d.location_id WHERE d.department_id IS NULL;
30. 查询部门名为 SAL 或 IT 的员工信息
SELECT e.*,d.department_name FROM employees e INNER JOIN departments d ON e.department_id=d.department_id WHERE d.department_name IN('SAL','IT');
31. 显示所有员工的姓名,部门号和部门名称。
SELECT e.last_name 姓名,e.department_id 部门号,d.department_name
FROM employees e
INNER JOIN departments d
ON e.department_id=d.department_id;
32. 查询 90 号部门员工的 job_id 和 90 号部门的 location_id
SELECT e.job_id,l.location_id
FROM employees e
INNER JOIN departments d
ON e.department_id=d.department_id
INNER JOIN locations l
ON d.location_id=l.location_id
WHERE e.department_id=90;
33. 选择所有有奖金的员工的
last_name , department_name , location_id , city
SELECT e.last_name,d.department_name,l.location_id,l.city
FROM employees e
INNER JOIN departments d
ON e.department_id=d.department_id
INNER JOIN locations l
ON d.location_id=l.location_id
WHERE e.commission_pct IS NOT NULL;
34. 选择city在Toronto工作的员工的
last_name , job_id , department_id , department_name
SELECT e.last_name,e.job_id,e.department_id,d.department_name,l.city
FROM employees e
INNER JOIN departments d
ON e.department_id=d.department_id
INNER JOIN locations l
ON d.location_id=l.location_id
WHERE l.city='Toronto';
35. 查询每个工种的工种名和最低工资
SELECT e.job_id,MIN(salary) FROM employees e GROUP BY e.job_id;
36. 查询每个国家下的部门个数大于 2 的国家编号
SELECT l.country_id,COUNT(d.department_id) 部门数
FROM locations l
INNER JOIN departments d
ON l.location_id=d.location_id
GROUP BY l.country_id
HAVING COUNT(d.department_id)>2;
SELECT country_id,COUNT(*) 部门个数
FROM departments d,locations l
WHERE d.`location_id`=l.`location_id`
GROUP BY country_id
HANDLER 部门个数>2;
37. 查询和 Zlotkey 相同部门的员工姓名和工资
SELECT last_name,salary FROM employees WHERE department_id IN (SELECT department_id FROM employees WHERE last_name='Zlotkey');
SELECT last_name,salary FROM employees WHERE department_id=(SELECT department_id FROM employees WHERE last_name='Zlotkey');
38. 查询工资比公司平均工资高的员工的员工号,姓名和工资。
SELECT employee_id,last_name,salary FROM employees WHERE salary>(SELECT AVG(salary) FROM employees) ORDER BY salary ASC;
39. 查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资
SELECT department_id,AVG(salary) FROM employees GROUP BY department_id;
SELECT e1.employee_id,e1.last_name,e1.salary,e1.department_id
FROM employees e1,(SELECT AVG(e2.salary) ag,e2.department_id dep_id FROM employees e2 GROUP BY e2.department_id) e3
WHERE e1.salary>e3.ag
AND e1.department_id=e3.dep_id;
40. 查询和姓名中包含字母 u 的员工在相同部门的员工的员工号和姓名
SELECT DISTINCT department_id FROM employees WHERE last_name LIKE '%u%';
SELECT employee_id,last_name FROM employees WHERE department_id IN (SELECT DISTINCT department_id FROM employees WHERE last_name LIKE '%u%');
41. 查询在部门的 location_id 为 1700 的部门工作的员工的员工号
SELECT department_id FROM departments WHERE location_id=1700;
SELECT employee_id FROM employees WHERE department_id IN (SELECT DISTINCT department_id FROM departments WHERE location_id=1700);
42. 查询管理者是 King 的员工姓名和工资
SELECT employee_id FROM employees WHERE last_name='K_ing';
SELECT last_name,salary,manager_id FROM employees WHERE manager_id IN(SELECT employee_id FROM employees WHERE last_name='K_ing');
43. 查询工资最高的员工的姓名,要求 first_name 和 last_name 显示为一列,列名为 姓.名
SELECT CONCAT(first_name,last_name) FROM employees WHERE salary=(SELECT MAX(salary) FROM employees);
44. 创建员工表
CREATE TABLE my_employees(
Id INT(10),
First_name VARCHAR(10),
Last_name VARCHAR(10),
Userid VARCHAR(10),
Salary DOUBLE(10,2)
);
45. 创建用户表
CREATE TABLE users(
id INT,
userid VARCHAR(10),
department_id INT
)
46. 向 my_employees 表中插入下列数据
INSERT INTO my_employees
VALUES(1,'patel','Ralph','Rpatel',895),
(2,'Dancs','Betty','Bdancs',860),
(3,'Biri','Ben','Bbiri',1100),
(4,'Newman','Chad','Cnewman',750),
(5,'Ropeburn','Audrey','Aropebur',1550);
47. 向 users 表中插入数据
INSERT INTO users
VALUES(1,'Rpatel',10),
(2,'Bdancs',10),
(3,'Bbiri',20),
(4,'Cnewman',30),
(5,'Aropebur',40);
48. 将 3 号员工的 last_name 修改为“drelxer”
UPDATE my_employees SET last_name='drelxer' WHERE Id=3;
49. 将所有工资少于 900 的员工的工资修改为 1000
UPDATE my_employees SET Salary=1000 WHERE Salary<900;
50. 将 userid 为 Bbiri 的 USER 表和 my_employees 表的记录全部删除
DELETE FROM my_employees WHERE Userid='Bbiri';
DELETE FROM users WHERE Userid='Bbiri';
51. 删除所有数据
方式一:
DELETE FROM my_employees;
DELETE FROM users;
方式二:
TRUNCATE my_employees;
TRUNCATE users;
52. 创建dept1
CREATE TABLE dept1(
NAME VARCHAR(25) NOT NULL,
id INT(7)
);
INSERT INTO dept1 (id,NAME) SELECT d.department_id,d.department_name FROM departments d;
CREATE TABLE emp5(
id INT(7),
First_name VARCHAR(25) NOT NULL,
Last_name VARCHAR(25),
Dept_id INT(7)
);
53. 将列 Last_name 的长度增加到 50
ALTER TABLE emp5 MODIFY Last_name VARCHAR(50);
54. 根据表 employees 创建 employees2
CREATE TABLE employees2 LIKE myemployees.employees;
55. 删除表 emp5
DROP TABLE emp5;
56. 将表 employees2 重命名为 emp5
方式一:
ALTER TABLE employees2 RENAME TO emp5;
方式二:
ALTER TABLE emp5 RENAME emp2;
57. 在表 dept1 和 emp5 中添加新列 test_column,并检查所作的操作
ALTER TABLE dept1 ADD COLUMN test_column VARCHAR(20);
ALTER TABLE emp5 ADD COLUMN test_column VARCHAR(20);
58. 直接删除表 emp5 中的列 dept_id
ALTER TABLE emp5 DROP test_column;
59. 向表 emp2 的 id 列中添加 PRIMARY KEY 约束(my_emp_id_pk)
ALTER TABLE emp2 MODIFY COLUMN employee_id INT PRIMARY KEY;
60. 向表 dept2 的 id 列中添加 PRIMARY KEY 约束(my_dept_id_pk)
ALTER TABLE dept1 MODIFY COLUMN id INT PRIMARY KEY;
61. 向表 emp2 中添加列 dept_id,并在其中定义 FOREIGN KEY 约束,与之相关联的列是dept2 表中的 id 列。
ALTER TABLE emp2 ADD COLUMN dept_id INT;
ALTER TABLE emp2 ADD CONSTRAINT fk_emp2_dept2 FOREIGN KEY(dept_id) REFERENCES dept1(id);
ALTER TABLE emp2 ADD CONSTRAINT fk_emp2_dept2 FOREIGN KEY(dept_id) REFERENCES dept1(id);
62. 删除外键
ALTER TABLE emp2 DROP FOREIGN KEY fk_emp2_dept2;
63. 创建一个表,里面有 id 为主键,stuname 唯一键,seat 座位号,要求将 id 设置成自增
CREATE TABLE stu(
id INT PRIMARY KEY AUTO_INCREMENT,
stuname VARCHAR(20) UNIQUE,
seat INT
);
64. 要求用事务的方式插入 3 行数据
SHOW VARIABLES LIKE "%commit%"
SET autocommit=0;
INSERT INTO stu(stuname,seat)
VALUES('jack',12),
('reos',13),
('make',14);
SELECT * FROM stu;
COMMIT;
65. 要求用事务的方式删除数据,并回滚
SET autommit=0; #开启事务
DELETE FROM stu;
SELECT * FROM stu;
ROLLBACK;
66. 创建视图 emp_v1,要求查询电话号码以‘011’开头的员工姓名和工资、邮箱
CREATE VIEW emp_v1 AS SELECT last_name,salary,email FROM employees WHERE phone_number LIKE '011%';
DROP VIEW emp_v1;
SELECT * FROM emp_v1 WHERE last_name='Jack';
67. 要求将视图 emp_v1 修改为查询电话号码以‘011’开头的并且邮箱中包含 e 字符的员工姓名和邮箱、电话号码
67. 向 emp_v1 插入一条记录,是否可以?
INSERT INTO emp_v1 VALUES('张飞1133',7868,'Tencet');
68. 修改刚才记录中的姓名为为‘yang’
UPDATE emp_v1 SET last_name='yang';
69. 删除刚才记录
DELETE FROM emp_v1;
70. 创建视图 emp_v2,要求查询部门的最高工资高于 12000 的部门信息
CREATE OR REPLACE VIEW emp_v2 AS
SELECT MAX(salary),department_id FROM employees GROUP BY department_id HAVING MAX(salary)>12000;
SELECT d.* FROM departments d INNER JOIN emp_v2 ev2 ON d.department_id=ev2.department_id;
SELECT d.* FROM departments d WHERE d.department_id IN (SELECT department_id FROM emp_v2);
71. 向 emp_v2 中插入一条记录,是否可以?
不可以,因为emp_v2中带有分组函数
72. 删除刚才的 emp_v2 和 emp_v1
DROP VIEW emp_v1,emp_v2;
73. 创建存储过程或函数实现传入用户名和密码,插入到 admin 表中
CREATE TABLE IF NOT EXISTS admin(
username VARCHAR(20),
pwd VARCHAR(20)
);
#修改sql结束符
DELIMITER $
#存储过程
CREATE PROCEDURE pro1(IN username VARCHAR(20),IN pwd VARCHAR(20))
BEGIN
INSERT INTO admin VALUES(username,pwd);
END$
#调用pro1
CALL pro1('yang','123456')$
#函数
CREATE FUNCTION fun1(username VARCHAR(20),pwd VARCHAR(20)) RETURNS INT
BEGIN
DECLARE result INT DEFAULT 0;#默认值
INSERT INTO admin VALUES(username,pwd);
result=1;
RETURN result;
END$
#调用fun1
SELECT fun1('ming','123456');
74. 创建存储过程或函数实现传入女神编号,返回女神名称和女神电话
#存储过程
CREATE PROCEDURE pro2(IN id INT)
BEGIN
SELECT NAME,phone FROM beauty b WHERE b.id=id;
END$
#函数
CREATE FUNCTION fun2(id INT) RETURNS VARCHAR(50)
BEGIN
DECLARE result VARCHAR(50) DEFAULT "";
SELECT CONCAT(NAME,',',phone) INTO result FROM beauty b WHERE b.id=id;
RETURN result;
END$
75. 创建存储存储过程或函数实现传入两个女神生日,返回大小
#存储过程
CREATE PROCEDURE pro3(IN n1 DATETIME,IN n2 DATETIME,OUT result INT)
BEGIN
SELECT DATEDIFF(n1,n2) INTO result;
END$
CALL pro3('1993-02-03 00:00:00','1992-02-03 00:00:00',@result)$
SELECT @result$
#函数
CREATE FUNCTION fun3(n1 DATETIME,n2 DATETIME) RETURNS INT
BEGIN
DECLARE result INT DEFAULT 0;
SELECT DATEDIFF(n1,n2) INTO result;
RETURN result;
END$
SELECT fun3('1993-02-03 00:00:00','1992-02-03 00:00:00')$
76. 创建存储过程或函数实现传入一个日期,格式化成 xx 年 xx 月 xx 日并返回
#存储过程
CREATE PROCEDURE pro4(IN oldtime DATE,OUT newtime VARCHAR(50))
BEGIN
SELECT DATE_FORMAT(oldtime,'%y年%i月%d日') INTO newtime;
END$
CALL pro4('1992-08-17',@newtime)$
SELECT @newtime$
#函数
CREATE FUNCTION fun4(oldtime DATE) RETURNS VARCHAR(50)
BEGIN
DECLARE result VARCHAR(50);
SELECT DATE_FORMAT(oldtime,'%y年%i月%d日') INTO result;
RETURN result;
END$
SELECT fun4('1992-08-17')$
77. 创建存储过程或函数实现传入女神名称,返回:女神 AND 男神 格式的字符串
如 传入 :小昭
返回: 小昭 AND 张无忌
#存储过程
DROP PROCEDURE pro5$
CREATE PROCEDURE pro5(IN username VARCHAR(50),OUT result VARCHAR(50))
BEGIN
SELECT CONCAT(b.name,' AND ',m.boyName) INTO result FROM beauty b INNER JOIN boys m ON b.boyfriend_id=m.id WHERE b.name=username;
END$
CALL pro5('小昭',@result)$
SELECT @result$
#函数
CREATE FUNCTION fun5(username VARCHAR(50)) RETURNS VARCHAR(50)
BEGIN
DECLARE result VARCHAR(50);
SELECT CONCAT(b.name,' AND ',m.boyName) INTO result FROM beauty b INNER JOIN boys m ON b.boyfriend_id=m.id WHERE b.name=username;
RETURN result;
END$
SELECT fun5('小昭');
78. 创建存储过程或函数,根据传入的条目数和起始索引,查询 beauty 表的记录
#存储过程
CREATE PROCEDURE pro6(IN size INT,IN startindex INT)
BEGIN
SELECT * FROM beauty LIMIT startindex,size;
END$
CALL pro6(2,3)$
#函数---报错Not allowed to return a result set from a function
CREATE FUNCTION fun6(size INT,startindex INT) RETURNS INT
BEGIN
DECLARE result INT DEFAULT 0;
SELECT * FROM beauty LIMIT startindex,size;
SET result=1;
RETURN result;
END$
SELECT fun6(2,3)$
79. 创建函数,实现传入两个 FLOAT,返回二者之和
CREATE FUNCTION fun6(num1 FLOAT,num2 FLOAT) RETURNS FLOAT
BEGIN
SET @SUM=0;
SET @SUM=num1+num2;
RETURN @SUM;
END$
SELECT fun6(12.12,23.14)$
89. 创建函数,实现传入工种名,返回该工种的员工人数
CREATE FUNCTION fun7(job_id VARCHAR(20)) RETURNS INT
BEGIN
DECLARE num INT DEFAULT 0;
SELECT COUNT(*) INTO num FROM employees e WHERE e.job_id=job_id;
RETURN num;
END$
SELECT fun7('AD_VP')$
90. 创建函数,实现传入员工名,返回该员工的领导名
SELECT e.last_name FROM employees e WHERE e.employee_id=(SELECT e2.manager_id FROM employees e2 WHERE e2.last_name='Gietz' );
CREATE FUNCTION fun8(last_name VARCHAR(50)) RETURNS VARCHAR(50)
BEGIN
DECLARE manager_name VARCHAR(50);
SELECT e.last_name INTO manager_name FROM employees e WHERE e.employee_id=(SELECT e2.manager_id FROM employees e2 WHERE e2.last_name=last_name );
RETURN manager_name;
END$
SELECT fun8('Ernst')$
91. 已知表 stringcontent
其中字段:
id 自增长
content VARCHAR(20)
向该表插入指定个数的,随机的字符串
CREATE TABLE IF NOT EXISTS stringcontent(
id INT PRIMARY KEY AUTO_INCREMENT,
content VARCHAR(20)
);
CREATE PROCEDURE test_randstr_insert(IN insertCount INT)
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE str VARCHAR(26) DEFAULT 'abcdefghijklmnopqrstuvwxyz';
DECLARE startIndex INT;#代表初始索引
DECLARE len INT;#代表截取的字符长度
DECLARE len1 INT;
WHILE i<=insertcount DO
SET startIndex=FLOOR(RAND()*26+1);#代表初始索引,随机范围1-26
SET len=FLOOR(RAND()*(20-startIndex+1)+1);#代表截取长度,随机范围1-(20-startIndex+1)
CASE
WHEN len=-4 THEN SET len1=0;
WHEN len=-3 THEN SET len1=1;
WHEN len=-2 THEN SET len1=2;
WHEN len=-1 THEN SET len1=3;
ELSE SET len1=len;
END CASE;
INSERT INTO stringcontent(content) VALUES(SUBSTR(str,startIndex,len));
SET i=i+1;
END WHILE;
END $
CALL test_randstr_insert(1)$
92. 查询工资最低的员工信息: last_name, salary
SELECT last_name,salary FROM employees WHERE salary=(SELECT MIN(salary) FROM employees);
93. 查询平均工资最低的部门信息
SELECT department_id FROM employees GROUP BY department_id ORDER BY AVG(salary) ASC LIMIT 1,1;
SELECT * FROM departments d
INNER JOIN
(SELECT department_id FROM employees GROUP BY department_id ORDER BY AVG(salary) ASC LIMIT 1,1) avg_i
ON d.department_id=avg_i.department_id;
94. 查询平均工资最低的部门信息和该部门的平均工资
SELECT d.*,avg_i.avg_salary FROM departments d
INNER JOIN
(SELECT department_id,AVG(salary) avg_salary FROM employees GROUP BY department_id ORDER BY AVG(salary) ASC LIMIT 1,1) avg_i
ON d.department_id=avg_i.department_id;
95. 查询平均工资最高的 job 信息
SELECT j.* FROM jobs j
INNER JOIN
(SELECT job_id FROM employees GROUP BY job_id ORDER BY AVG(salary) DESC LIMIT 1,1) avg_i
ON j.job_id=avg_i.job_id;
96. 查询平均工资高于公司平均工资的部门有哪些?
SELECT AVG(salary) FROM employees;
SELECT AVG(salary),department_id FROM employees GROUP BY department_id;
SELECT a_d.id FROM
(SELECT AVG(salary) avg_salary,department_id id FROM employees GROUP BY department_id) a_d
WHERE a_d.avg_salary>(SELECT AVG(salary) FROM employees);
97. 查询出公司中所有 manager 的详细信息.
SELECT DISTINCT manager_id FROM employees;
SELECT e.* FROM employees e WHERE e.employee_id IN(SELECT DISTINCT manager_id FROM employees);
98. 各个部门中 最高工资中最低的那个部门的 最低工资是多少
SELECT MAX(salary) ,department_id FROM employees
GROUP BY department_id
ORDER BY MAX(salary)
ASC LIMIT 1,1;
SELECT MIN(salary) FROM employees e
INNER JOIN (SELECT department_id
FROM employees
GROUP BY department_id
ORDER BY MAX(salary) ASC LIMIT 1,1) d
ON e.department_id=d.department_id;
99. 查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email,salary
SELECT department_id FROM employees
GROUP BY department_id
ORDER BY AVG(salary)
DESC LIMIT 1,1;
#方式一:
SELECT e.* FROM employees e
WHERE e.employee_id =(SELECT d.manager_id FROM departments d
INNER JOIN (SELECT department_id FROM employees
GROUP BY department_id
ORDER BY AVG(salary)
DESC LIMIT 1,1) e1
ON d.department_id=e1.department_id);
#方式二:
SELECT e.* FROM employees e
INNER JOIN (SELECT d.manager_id mi FROM departments d
INNER JOIN (SELECT department_id FROM employees
GROUP BY department_id
ORDER BY AVG(salary)
DESC LIMIT 1,1) e1
ON d.department_id=e1.department_id) e2
ON e.employee_id=e2.mi;