当前位置:   article > 正文

Mysql —— 6、子查询_mysql salary语句

mysql salary语句

目录

1、子查询的基本使用及分类

1.1、子查询的基本使用

1.2、子查询的分类

2、单行子查询

2.1、单行子查询比较操作符

 2.2、代码示例

2.3、HAVING中的子查询

2.4、CASE中的子查询

2.5、子查询中的空值问题

3、多行子查询

3.1、多行比较操作符

3.2、代码示例 

4、相关子查询

4.1、相关子查询执行过程

4.2、代码示例

4.3、EXISTS 与 NOT EXISTS关键字

4.4、相关更新

4.5、相关删除

5、关于自连接和子查询效率问题

6、练习


子查询指一个查询语句嵌套在另一个查询语句内部的查询,这个特性从MySQL 4.1开始引入。

SQL 中子查询的使用大大增强了 SELECT 查询的能力,因为很多时候查询需要从结果集中获取数据,或者需要从同一个表中先计算得出一个数据结果,然后与这个数据结果(可能是某个标量,也可能是某个集合)进行比较。

1、子查询的基本使用及分类

1.1、子查询的基本使用

  • 子查询的基本语法结构:括号内为内查询(子查询),括号外为外查询(主查询)

  • 在SELECT中,除了GROUP BY 和 LIMIT之外,其他位置都可以声明子查询

  • 子查询(内查询)在主查询之前一次执行完成。

  • 子查询的结果被主查询(外查询)使用 。

  • 注意事项

    • 子查询要包含在括号内

    • 将子查询放在比较条件的右侧

    • 单行操作符对应单行子查询,多行操作符对应多行子查询

1.2、子查询的分类

1、按内查询的结果返回一条还是多条记录,将子查询分为单行子查询多行子查询

2、按内查询是否被执行多次,将子查询划分为相关(或关联)子查询不相关(或非关联)子查询

        子查询从数据表中查询了数据结果,如果这个数据结果只执行一次,然后这个数据结果作为主查询的条件进行执行,那么这样的子查询叫做不         相关子查询。

        同样,如果子查询需要执行多次,即采用循环的方式,先从外部查询开始,每次都传入子查询进行查询,然后再将结果反馈给外部,这种嵌套         的执行方式就称为相关子查询。

2、单行子查询

2.1、单行子查询比较操作符

 2.2、代码示例

  1. #题目:查询工资大于149号员工工资的员工的信息
  2. SELECT employee_id, last_name, salary
  3. FROM employees
  4. WHERE salary > (
  5. SELECT salary
  6. FROM employees
  7. WHERE employee_id = 149
  8. );
  9. #题目:返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id和工资
  10. SELECT last_name, job_id, salary
  11. FROM employees
  12. WHERE job_id = (
  13. SELECT job_id
  14. FROM employees
  15. WHERE employee_id = 141
  16. )
  17. AND salary > (
  18. SELECT salary
  19. FROM employees
  20. WHERE employee_id = 143
  21. );
  22. #题目:返回公司工资最少的员工的last_name,job_id和salary
  23. SELECT last_name, job_id, salary
  24. FROM employees
  25. WHERE salary = (
  26. SELECT MIN(salary)
  27. FROM employees
  28. );
  29. #题目:查询与141号员工的manager_id和department_id相同的其他员工的employee_id,manager_id,department_id。
  30. #方式1:
  31. SELECT employee_id, manager_id, department_id
  32. FROM employees
  33. WHERE manager_id = (
  34. SELECT manager_id
  35. FROM employees
  36. WHERE employee_id = 141
  37. )
  38. AND department_id = (
  39. SELECT department_id
  40. FROM employees
  41. WHERE employee_id = 141
  42. )
  43. AND employee_id <> 141;
  44. #方式2:了解
  45. SELECT employee_id,manager_id,department_id
  46. FROM employees
  47. WHERE (manager_id,department_id) = (
  48. SELECT manager_id,department_id
  49. FROM employees
  50. WHERE employee_id = 141
  51. )
  52. AND employee_id <> 141;

2.3、HAVING中的子查询

  • 首先执行子查询。

  • 向主查询中的HAVING 子句返回结果

  1. #题目:查询最低工资大于110号部门最低工资的部门id和其最低工资
  2. SELECT department_id, MIN(salary)
  3. FROM employees
  4. WHERE department_id IS NOT NULL
  5. GROUP BY department_id
  6. HAVING MIN(salary) > (
  7. SELECT MIN(salary)
  8. FROM employees
  9. WHERE department_id = 110
  10. );

2.4、CASE中的子查询

  1. SELECT employee_id,last_name,CASE department_id WHEN(SELECT department_id
  2. FROM departments
  3. WHERE location_id = 1800) THEN 'Canada'
  4. ELSE 'USA' END "location"
  5. FROM employees

2.5、子查询中的空值问题

子查询不返回任何行,主查询也不返回任何行

  1. SELECT last_name, job_id
  2. FROM employees
  3. WHERE job_id =
  4. #子查询中空值
  5. (SELECT job_id
  6. FROM employees
  7. WHERE last_name = 'Haas');

3、多行子查询

  • 也称为集合比较子查询

  • 内查询返回多行

  • 使用多行比较操作符

3.1、多行比较操作符

3.2、代码示例 

1、IN

  1. SELECT employee_id, last_name
  2. FROM employees
  3. WHERE salary IN
  4. (SELECT MIN(salary)
  5. FROM employees
  6. GROUP BY department_id);

2、ANY

  1. #题目:返回其它job_id中比job_id为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id 以及salary
  2. SELECT employee_id, last_name, job_id, salary
  3. FROM employees
  4. WHERE job_id <> 'IT_PROG'
  5. AND salary < ANY(
  6. SELECT salary
  7. FROM employees
  8. WHERE job_id = 'IT_PROG'
  9. );

3、ALL

  1. #题目:查询平均工资最低的部门id
  2. #MySQL中聚合函数是不能嵌套使用的。
  3. #方式1
  4. SELECT department_id
  5. FROM employees
  6. GROUP BY `department_id`
  7. HAVING AVG(salary) = (
  8. SELECT MIN(avg_sal)
  9. FROM (
  10. SELECT AVG(salary) avg_sal
  11. FROM employees
  12. GROUP BY department_id
  13. ) t_dept_avg_sal
  14. );
  15. #方式2
  16. SELECT department_id
  17. FROM employees
  18. GROUP BY `department_id`
  19. HAVING AVG(salary) <= ALL(
  20. SELECT AVG(salary)
  21. FROM employees
  22. GROUP BY department_id
  23. )

4、相关子查询

4.1、相关子查询执行过程

如果子查询的执行依赖于外部查询,通常情况下都是因为子查询中的表用到了外部的表,并进行了条件关联,因此每执行一次外部查询,子查询都要重新计算一次,这样的子查询就称之为关联子查询

相关子查询按照一行接一行的顺序执行,主查询的每一行都执行一次子查询。

说明:子查询中使用主查询中的列  

4.2、代码示例

  1. #题目:查询员工中工资大于本部门平均工资的员工的last_name,salary和其department_id
  2. #方式1:使用相关子查询
  3. SELECT last_name, salary, department_id
  4. FROM employees e1
  5. WHERE salary > (
  6. SELECT AVG(salary)
  7. FROM employees e2
  8. WHERE department_id = e1.department_id
  9. );
  10. #方式2:在FROM中声明子查询
  11. SELECT e1.last_name, e1.salary, e1.department_id
  12. FROM employees e1, (
  13. SELECT department_id, AVG(salary) avg_sal
  14. FROM employees e2
  15. GROUP BY `department_id`
  16. ) e2
  17. WHERE e1.department_id = e2.department_id
  18. AND e1.salary > avg_sal
  1. #题目:查询员工的id,salary,按照department_name 排序
  2. SELECT employee_id, salary
  3. FROM `employees` e
  4. ORDER BY (
  5. SELECT `department_name`
  6. FROM `departments` d
  7. WHERE e.department_id = d.department_id
  8. );
  1. #题目:若employees表中employee_id与job_history表中employee_id相同的数目不小于2
  2. #输出这些相同id的员工的employee_id,last_name和其job_id
  3. SELECT e.employee_id, e.last_name, e.job_id
  4. FROM employees e
  5. WHERE 2 <= (
  6. SELECT COUNT(*)
  7. FROM `job_history` j
  8. WHERE j.employee_id = e.employee_id
  9. );

4.3、EXISTS 与 NOT EXISTS关键字

  • 关联子查询通常也会和 EXISTS操作符一起来使用,用来检查在子查询中是否存在满足条件的行。

  • 如果在子查询中不存在满足条件的行:

    • 条件返回 FALSE

    • 继续在子查询中查找

  • 如果在子查询中存在满足条件的行:

    • 不在子查询中继续查找

    • 条件返回 TRUE

  • NOT EXISTS关键字表示如果不存在某种条件,则返回TRUE,否则返回FALSE。

1、EXISTS

  1. #题目:查询公司管理者的employee_id,last_name,job_id,department_id信息
  2. #方式1:自连接
  3. SELECT DISTINCT m.employee_id, m.last_name, m.job_id, m.department_id
  4. FROM employees e JOIN employees m
  5. ON e.manager_id = m.employee_id;
  6. #方式2:子查询
  7. SELECT e.employee_id, e.last_name, e.job_id, e.department_id
  8. FROM employees e
  9. WHERE e.`employee_id` IN(
  10. SELECT DISTINCT m.manager_id
  11. FROM employees m
  12. );
  13. #方式3:使用EXISTS
  14. SELECT e.employee_id, e.last_name, e.job_id, e.department_id
  15. FROM employees e
  16. WHERE EXISTS (
  17. SELECT *
  18. FROM employees m
  19. WHERE e.`employee_id` = m.manager_id
  20. );

2、NOT EXISTS

  1. #题目:查询departments表中,不存在于employees表中的部门的department_id和department_name
  2. #方式1
  3. SELECT d.department_id, d.department_name
  4. FROM employees e RIGHT JOIN departments d
  5. ON e.department_id = d.department_id
  6. WHERE e.department_id IS NULL;
  7. #方式2
  8. SELECT department_id, department_name
  9. FROM departments d
  10. WHERE NOT EXISTS (
  11. SELECT *
  12. FROM employees e
  13. WHERE e.department_id = d.department_id
  14. )

4.4、相关更新

使用相关子查询依据一个表中的数据更新另一个表的数据。

  1. #题目:在employees中增加一个department_name字段,数据为员工对应的部门名称
  2. # 第一步
  3. ALTER TABLE employees
  4. ADD(department_name VARCHAR2(14));
  5. # 第二步
  6. UPDATE employees e
  7. SET department_name = (SELECT department_name
  8. FROM departments d
  9. WHERE e.department_id = d.department_id);

4.5、相关删除

使用相关子查询依据一个表中的数据删除另一个表的数据。

  1. #删除表employees中,其与emp_history表皆有的数据
  2. DELETE FROM employees e
  3. WHERE employee_id IN
  4. (SELECT employee_id
  5. FROM emp_history
  6. WHERE employee_id = e.employee_id);

5、关于自连接和子查询效率问题

题目中可以使用子查询,也可以使用自连接。一般情况建议你使用自连接,因为在许多 DBMS 的处理过程中,对于自连接的处理速度要比子查询快得多。

可以这样理解:子查询实际上是通过未知表进行查询后的条件判断,而自连接是通过已知的自身数据表进行条件判断,因此在大部分 DBMS 中都对自连接处理进行了优化。

6、练习

  1. #1.查询和Zlotkey相同部门的员工姓名和工资
  2. SELECT last_name, salary
  3. FROM employees
  4. WHERE department_id (
  5. SELECT department_id
  6. FROM employees
  7. WHERE last_name = 'Zlotkey'
  8. );
  9. #2.查询工资比公司平均工资高的员工的员工号,姓名和工资。
  10. SELECT employee_id, last_name, salary
  11. FROM employees
  12. WHERE salary > (
  13. SELECT AVG(salary)
  14. FROM employees
  15. );
  16. #3.选择工资大于所有JOB_ID = 'SA_MAN'的员工的工资的员工的last_name, job_id, salary
  17. SELECT last_name , job_id, salary
  18. FROM employees
  19. WHERE salary > ALL(
  20. SELECT salary
  21. FROM employees
  22. WHERE job_id = 'SA_MAN'
  23. );
  24. #4.查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名
  25. SELECT employee_id, last_name
  26. FROM employees
  27. WHERE department_id IN(
  28. SELECT DISTINCT department_id
  29. FROM employees
  30. WHERE last_name LIKE '%u%'
  31. );
  32. #5.查询在部门的location_id为1700的部门工作的员工的员工号
  33. SELECT employee_id
  34. FROM employees
  35. WHERE department_id IN(
  36. SELECT department_id
  37. FROM `departments`
  38. WHERE `location_id` = 1700
  39. );
  40. #6.查询管理者是King的员工姓名和工资
  41. #方法1
  42. SELECT last_name, salary
  43. FROM employees
  44. WHERE manager_id IN(
  45. SELECT employee_id
  46. FROM employees
  47. WHERE last_name = 'King'
  48. );
  49. #7.查询工资最低的员工信息: last_name, salary
  50. SELECT last_name, salary
  51. FROM employees
  52. WHERE salary = (
  53. SELECT MIN(salary)
  54. FROM employees
  55. );
  56. #8.查询平均工资最低的部门信息
  57. #方式1
  58. SELECT *
  59. FROM departments
  60. WHERE department_id = (
  61. SELECT department_id
  62. FROM employees
  63. GROUP BY department_id
  64. HAVING AVG(salary) = (
  65. SELECT MIN(avg_sal)
  66. FROM (
  67. SELECT AVG(salary) avg_sal
  68. FROM employees
  69. GROUP BY department_id
  70. ) t_dept_avg_sal
  71. )
  72. );
  73. #方式2
  74. SELECT *
  75. FROM departments
  76. WHERE department_id = (
  77. SELECT department_id
  78. FROM employees
  79. GROUP BY department_id
  80. HAVING AVG(salary) <= ALL(
  81. SELECT AVG(salary)
  82. FROM employees
  83. GROUP BY department_id
  84. )
  85. );
  86. #方式3:LIMIT
  87. SELECT *
  88. FROM departments
  89. WHERE department_id = (
  90. SELECT department_id
  91. FROM employees
  92. GROUP BY department_id
  93. HAVING AVG(salary) = (
  94. SELECT AVG(salary) avg_sal
  95. FROM employees
  96. GROUP BY department_id
  97. ORDER BY avg_sal ASC
  98. LIMIT 0,1
  99. )
  100. )
  101. #方式4
  102. SELECT d.*
  103. FROM departments d, (
  104. SELECT department_id, AVG(salary) avg_sal
  105. FROM employees
  106. GROUP BY department_id
  107. ORDER BY avg_sal ASC
  108. LIMIT 0,1
  109. ) t_dept_avg_sal
  110. WHERE d.department_id = t_dept_avg_sal.department_id;
  111. #9.查询平均工资最低的部门信息和该部门的平均工资(相关子查询)
  112. #方式1
  113. SELECT d.*,(SELECT AVG(salary) FROM employees WHERE department_id = d.`department_id`) avg_sal
  114. FROM departments d
  115. WHERE department_id = (
  116. SELECT department_id
  117. FROM employees
  118. GROUP BY department_id
  119. HAVING AVG(salary ) = (
  120. SELECT MIN(avg_sal)
  121. FROM (
  122. SELECT AVG(salary) avg_sal
  123. FROM employees
  124. GROUP BY department_id
  125. ) t_dept_avg_sal
  126. )
  127. );
  128. #方式2
  129. SELECT d.*,(SELECT AVG(salary) FROM employees WHERE department_id = d.`department_id`) avg_sal
  130. FROM departments d
  131. WHERE department_id = (
  132. SELECT department_id
  133. FROM employees
  134. GROUP BY department_id
  135. HAVING AVG(salary ) <= ALL(
  136. SELECT AVG(salary)
  137. FROM employees
  138. GROUP BY department_id
  139. )
  140. );
  141. #方式3LIMIT
  142. SELECT d.*,(SELECT AVG(salary) FROM employees WHERE department_id = d.`department_id`) avg_sal
  143. FROM departments d
  144. WHERE department_id = (
  145. SELECT department_id
  146. FROM employees
  147. GROUP BY department_id
  148. HAVING AVG(salary ) =(
  149. SELECT AVG(salary) avg_sal
  150. FROM employees
  151. GROUP BY department_id
  152. ORDER BY avg_sal ASC
  153. LIMIT 1
  154. )
  155. );
  156. #方式4
  157. SELECT d.*,(SELECT AVG(salary) FROM employees WHERE department_id = d.`department_id`) avg_sal
  158. FROM departments d,(
  159. SELECT department_id,AVG(salary) avg_sal
  160. FROM employees
  161. GROUP BY department_id
  162. ORDER BY avg_sal ASC
  163. LIMIT 0,1
  164. ) t_dept_avg_sal
  165. WHERE d.`department_id` = t_dept_avg_sal.department_id
  166. #10.查询平均工资最高的 job 信息
  167. #方式1
  168. SELECT *
  169. FROM jobs
  170. WHERE job_id = (
  171. SELECT job_id
  172. FROM employees
  173. GROUP BY job_id
  174. HAVING AVG(salary) = (
  175. SELECT MAX(avg_sal)
  176. FROM (
  177. SELECT AVG(salary) avg_sal
  178. FROM employees
  179. GROUP BY job_id
  180. ) t_job_avg_sal
  181. )
  182. );
  183. #方式2
  184. SELECT *
  185. FROM jobs
  186. WHERE job_id = (
  187. SELECT job_id
  188. FROM employees
  189. GROUP BY job_id
  190. HAVING AVG(salary) >= ALL(
  191. SELECT AVG(salary)
  192. FROM employees
  193. GROUP BY job_id
  194. )
  195. );
  196. #方式3
  197. SELECT *
  198. FROM jobs
  199. WHERE job_id = (
  200. SELECT job_id
  201. FROM employees
  202. GROUP BY job_id
  203. HAVING AVG(salary) =(
  204. SELECT AVG(salary) avg_sal
  205. FROM employees
  206. GROUP BY job_id
  207. ORDER BY avg_sal DESC
  208. LIMIT 0,1
  209. )
  210. );
  211. #方式4
  212. SELECT j.*
  213. FROM jobs j,(
  214. SELECT job_id,AVG(salary) avg_sal
  215. FROM employees
  216. GROUP BY job_id
  217. ORDER BY avg_sal DESC
  218. LIMIT 0,1
  219. ) t_job_avg_sal
  220. WHERE j.job_id = t_job_avg_sal.job_id
  221. #11.查询平均工资高于公司平均工资的部门有哪些?
  222. SELECT department_id
  223. FROM employees
  224. WHERE department_id IS NOT NULL
  225. GROUP BY department_id
  226. HAVING AVG(salary) > (
  227. SELECT AVG(salary)
  228. FROM employees
  229. );
  230. #12.查询出公司中所有 manager 的详细信息
  231. #方式1:自连接
  232. SELECT DISTINCT mgr.employee_id,mgr.last_name,mgr.job_id,mgr.department_id
  233. FROM employees emp JOIN employees mgr
  234. ON emp.manager_id = mgr.employee_id;
  235. #方式2:子查询
  236. SELECT employee_id,last_name,job_id,department_id
  237. FROM employees
  238. WHERE employee_id IN (
  239. SELECT DISTINCT manager_id
  240. FROM employees
  241. );
  242. #方式3:使用EXISTS
  243. SELECT employee_id,last_name,job_id,department_id
  244. FROM employees e1
  245. WHERE EXISTS (
  246. SELECT *
  247. FROM employees e2
  248. WHERE e1.`employee_id` = e2.`manager_id`
  249. );
  250. #13.各个部门中 最高工资中最低的那个部门的 最低工资是多少?
  251. #方式1
  252. SELECT MIN(salary)
  253. FROM employees
  254. WHERE department_id = (
  255. SELECT department_id
  256. FROM employees
  257. GROUP BY department_id
  258. HAVING MAX(salary) = (
  259. SELECT MIN(max_sal)
  260. FROM (
  261. SELECT MAX(salary) max_sal
  262. FROM employees
  263. GROUP BY department_id
  264. ) t_dept_max_sal
  265. )
  266. );
  267. SELECT *
  268. FROM employees
  269. WHERE department_id = 10;
  270. #方式2
  271. SELECT MIN(salary)
  272. FROM employees
  273. WHERE department_id = (
  274. SELECT department_id
  275. FROM employees
  276. GROUP BY department_id
  277. HAVING MAX(salary) <= ALL (
  278. SELECT MAX(salary)
  279. FROM employees
  280. GROUP BY department_id
  281. )
  282. );
  283. #方式3
  284. SELECT MIN(salary)
  285. FROM employees
  286. WHERE department_id = (
  287. SELECT department_id
  288. FROM employees
  289. GROUP BY department_id
  290. HAVING MAX(salary) = (
  291. SELECT MAX(salary) max_sal
  292. FROM employees
  293. GROUP BY department_id
  294. ORDER BY max_sal ASC
  295. LIMIT 0,1
  296. )
  297. );
  298. #方式4
  299. SELECT MIN(salary)
  300. FROM employees e,(
  301. SELECT department_id,MAX(salary) max_sal
  302. FROM employees
  303. GROUP BY department_id
  304. ORDER BY max_sal ASC
  305. LIMIT 0,1
  306. ) t_dept_max_sal
  307. WHERE e.department_id = t_dept_max_sal.department_id
  308. #14.查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary
  309. #方式1
  310. SELECT last_name, department_id, email, salary
  311. FROM employees
  312. WHERE employee_id IN ANY (
  313. SELECT DISTINCT manager_id
  314. FROM employees
  315. WHERE department_id = (
  316. SELECT department_id
  317. FROM employees
  318. GROUP BY department_id
  319. HAVING AVG(salary) = (
  320. SELECT MAX(avg_sal)
  321. FROM (
  322. SELECT AVG(salary) avg_sal
  323. FROM employees
  324. GROUP BY department_id
  325. ) t_dept_avg_sal
  326. )
  327. )
  328. );
  329. #方式2
  330. SELECT last_name, department_id, email, salary
  331. FROM employees
  332. WHERE employee_id = ANY (
  333. SELECT DISTINCT manager_id
  334. FROM employees
  335. WHERE department_id = (
  336. SELECT department_id
  337. FROM employees
  338. GROUP BY department_id
  339. HAVING AVG(salary) >= ALL (
  340. SELECT AVG(salary) avg_sal
  341. FROM employees
  342. GROUP BY department_id
  343. )
  344. )
  345. );
  346. #方式3
  347. SELECT last_name, department_id, email, salary
  348. FROM employees
  349. WHERE employee_id IN (
  350. SELECT DISTINCT manager_id
  351. FROM employees e,(
  352. SELECT department_id,AVG(salary) avg_sal
  353. FROM employees
  354. GROUP BY department_id
  355. ORDER BY avg_sal DESC
  356. LIMIT 0,1
  357. ) t_dept_avg_sal
  358. WHERE e.`department_id` = t_dept_avg_sal.department_id
  359. );
  360. #15. 查询部门的部门号,其中不包括job_id是"ST_CLERK"的部门号
  361. #方式1
  362. SELECT department_id
  363. FROM departments
  364. WHERE department_id NOT IN (
  365. SELECT DISTINCT department_id
  366. FROM employees
  367. WHERE job_id = 'ST_CLERK'
  368. );
  369. #方式2
  370. SELECT department_id
  371. FROM departments d
  372. WHERE NOT EXISTS (
  373. SELECT *
  374. FROM employees e
  375. WHERE d.`department_id` = e.`department_id`
  376. AND e.`job_id` = 'ST_CLERK'
  377. );
  378. #16. 选择所有没有管理者的员工的last_name
  379. SELECT last_name
  380. FROM employees e
  381. WHERE NOT EXISTS (
  382. SELECT employee_id
  383. FROM employees m
  384. WHERE e.manager_id = m.employee_id
  385. );
  386. #17.查询员工号、姓名、雇用时间、工资,其中员工的管理者为 'De Haan'
  387. #方式1
  388. SELECT employee_id,last_name,hire_date,salary
  389. FROM employees
  390. WHERE manager_id IN (
  391. SELECT employee_id
  392. FROM employees
  393. WHERE last_name = 'De Haan'
  394. );
  395. #方式2
  396. SELECT employee_id,last_name,hire_date,salary
  397. FROM employees e1
  398. WHERE EXISTS (
  399. SELECT *
  400. FROM employees e2
  401. WHERE e1.`manager_id` = e2.`employee_id`
  402. AND e2.last_name = 'De Haan'
  403. );
  404. #18.查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资(相关子查询)
  405. #方式1:使用相关子查询
  406. SELECT last_name,salary,department_id
  407. FROM employees e1
  408. WHERE salary > (
  409. SELECT AVG(salary)
  410. FROM employees e2
  411. WHERE department_id = e1.`department_id`
  412. );
  413. #方式2:在FROM中声明子查询
  414. SELECT e.last_name,e.salary,e.department_id
  415. FROM employees e,(
  416. SELECT department_id,AVG(salary) avg_sal
  417. FROM employees
  418. GROUP BY department_id) t_dept_avg_sal
  419. WHERE e.department_id = t_dept_avg_sal.department_id
  420. AND e.salary > t_dept_avg_sal.avg_sal
  421. #19.查询每个部门下的部门人数大于 5 的部门名称(相关子查询)
  422. #方式1
  423. SELECT department_name
  424. FROM departments d, (
  425. SELECT department_id, COUNT(*) num
  426. FROM employees
  427. GROUP BY department_id
  428. ) dept_num
  429. WHERE d.department_id = dept_num.department_id
  430. AND num > 5
  431. #方式2
  432. SELECT department_name
  433. FROM departments d
  434. WHERE 5 < (
  435. SELECT COUNT(*)
  436. FROM employees e
  437. WHERE d.department_id = e.`department_id`
  438. );
  439. #20.查询每个国家下的部门个数大于 2 的国家编号(相关子查询)
  440. SELECT country_id
  441. FROM locations l
  442. WHERE 2 < (
  443. SELECT COUNT(*)
  444. FROM departments d
  445. WHERE l.`location_id` = d.`location_id`
  446. );

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/962764
推荐阅读
相关标签
  

闽ICP备14008679号