翼度科技»论坛 编程开发 mysql 查看内容

MySQL基础20题(续前面的20题)

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
写在前面

今天继续前面的20题之后的练习,数据都是一样的,可以直接使用,来看看你的sql功底降了没。
基础20题
  1. #1.查询每个员⼯的姓名、邮箱、职位名称以及所在部⻔名称。
  2.         SELECT
  3.         CONCAT(last_name,first_name) as 姓名,
  4.         email,
  5.         job_title,
  6.         department_name
  7. FROM
  8.         employees e
  9.         JOIN departments d ON e.department_id=d.department_id
  10.         JOIN jobs j ON e.job_id = j.job_id ;
  11. #2.查询⽉薪最⾼的员⼯的姓名、职位名称以及⽉薪。
  12.         SELECT
  13.         CONCAT(last_name,first_name) as 姓名,
  14.         job_title,
  15.         salary
  16. FROM
  17.         employees e
  18. JOIN jobs j on e.job_id = j.job_id
  19. WHERE        salary = (SELECT MAX(salary) FROM employees);
  20. #3.查询每个部⻔的平均⽉薪。
  21. #注:如果某一部门没有员工不会显示
  22.         SELECT
  23.         department_name,
  24.         AVG(salary)
  25. FROM
  26.         employees e
  27. JOIN departments d ON e.department_id = d.department_id
  28. GROUP BY
  29.         department_name;   
  30. #4.查询部⻔中员⼯⽉薪⾼于5000的所有员⼯的姓名、⽉薪以及所在部⻔名称。
  31.         SELECT
  32.         CONCAT(last_name,first_name) as 姓名,
  33.         salary,
  34. department_name
  35. FROM
  36.         employees e
  37. JOIN departments d ON e.department_id = d.department_id
  38. WHERE
  39.         salary > 5000;
  40. #5.查询奖⾦率最⾼的员⼯的姓名、职位名称以及奖⾦率。
  41.         SELECT
  42.         CONCAT(last_name,first_name) as 姓名,
  43.         job_title,
  44.         commission_pct
  45. FROM
  46.         employees e
  47. JOIN jobs j ON e.job_id = j.job_id
  48. WHERE
  49.         commission_pct = (SELECT MAX(commission_pct) FROM employees);
  50. #6.查询每个职位的最⾼⽉薪和最低⽉薪。
  51.         SELECT
  52.         job_title,
  53.         MAX(salary),
  54.         MIN(salary)
  55. FROM
  56.         employees e
  57. JOIN jobs j ON e.job_id = j.job_id
  58. GROUP BY
  59.         job_title;
  60.        
  61. #7.查询⼊职时间早于2000年的所有员⼯的姓名、⼊职⽇期以及职位名称。
  62.         SELECT
  63.         CONCAT(last_name,first_name) as 姓名,
  64.         hiredate,
  65.         job_title
  66. FROM
  67.         employees e
  68. JOIN jobs j on e.job_id = j.job_id
  69. WHERE
  70.         YEAR(hiredate) < 2000;
  71. #8.查询每个部⻔的员⼯⼈数。
  72.         SELECT
  73.         COUNT(*) as 人数,
  74.         department_name
  75. FROM
  76.   employees e
  77. JOIN departments d ON e.department_id = d.department_id
  78. GROUP BY
  79.         department_name;
  80. #9.查询在每个部⻔中⽉薪最⾼的员⼯的姓名、⽉薪以及部⻔名称。
  81. #注:在使用in时可以使用()将需要的列括起来查询
  82.         SELECT
  83.         CONCAT(e.last_name,e.first_name) as 姓名,
  84.         e.salary,
  85.         d.department_name
  86. FROM
  87.         employees e JOIN departments d ON e.department_id = d.department_id
  88. WHERE
  89.         (e.salary,d.department_id) in (
  90. SELECT MAX(salary),department_id FROM employees GROUP BY department_id);               
  91. #10.查询⽉薪超过部⻔平均⽉薪的员⼯的姓名、⽉薪以及所在部⻔名称。
  92.         SELECT
  93.         CONCAT( e1.last_name, e1.first_name ) AS 姓名,
  94.         e1.salary,
  95.         d.department_name
  96. FROM
  97.         employees e1
  98.         JOIN departments d ON e1.department_id = d.department_id
  99. WHERE
  100. e1.salary > ( SELECT AVG( e2.salary ) FROM employees e2 WHERE e1.department_id = e2.department_id);
  101. #11.查询每个职位的员⼯⼈数。
  102.         SELECT
  103.         COUNT(*) as 人数,
  104.         job_title
  105. FROM
  106.         employees e
  107. JOIN jobs j ON e.job_id = j.job_id
  108. GROUP BY
  109.         job_title;
  110. #12.查询每个部⻔的最⾼⽉薪和最低⽉薪。
  111.         SELECT
  112.         department_name,
  113.         MAX(salary),
  114.         MIN(salary)
  115. FROM
  116.         employees e
  117. JOIN departments d ON e.department_id = d.department_id
  118. GROUP BY
  119.         department_name;
  120. #13.查询每个员⼯的姓名、邮箱、职位名称以及他们的上级领导的姓名。
  121.         SELECT
  122.         CONCAT(e1.last_name,e1.first_name) as 姓名,
  123.         e1.email,
  124.         j.job_title,
  125.         CONCAT(e2.last_name,e2.first_name) as 上级领导
  126. FROM
  127.         employees e1
  128. JOIN employees e2 ON e1.manager_id = e2.employee_id
  129. JOIN jobs j ON e1.job_id = j.job_id;
  130. #14.查询每个部⻔的员⼯平均奖⾦率。
  131.         SELECT
  132.         department_name,
  133.         AVG(commission_pct) as 平均奖金率
  134. FROM
  135.         employees e
  136. JOIN departments d ON e.department_id = d.department_id
  137. GROUP BY
  138.         department_name;
  139. #15.查询每个城市的员⼯⼈数。
  140.         SELECT
  141.         COUNT(*) as 人数,
  142.         city
  143. FROM
  144.         employees e
  145. JOIN departments d ON e.department_id = d.department_id
  146. JOIN locations l ON d.location_id = l.location_id
  147. GROUP BY
  148.         city;
  149. #16.查询每个部⻔的职位种类数。
  150. #注:使用distinct可以去重算种类数
  151.         SELECT
  152.         COUNT(DISTINCT job_id) as 种类数,
  153.         department_name
  154. FROM
  155.         employees e
  156. JOIN departments d ON e.department_id = d.department_id
  157. GROUP BY
  158.         department_name;  
  159. #17.查询⼯资⾼于其职位平均⼯资的员⼯姓名、职位名称以及⽉薪。
  160.         SELECT
  161.         CONCAT(e1.last_name, e1.first_name ) AS 姓名,
  162.         j.job_title,
  163.         e1.salary
  164. FROM
  165. employees e1
  166. JOIN jobs j ON e1.job_id = j.job_id
  167. WHERE
  168.         e1.salary > (SELECT AVG(e2.salary) FROM employees e2 WHERE e1.job_id = e2.job_id);
  169. #18.查询每个国家的员⼯⼈数。
  170.         SELECT
  171.         COUNT(DISTINCT employee_id) as 人数,
  172.         country_id
  173. FROM
  174.         employees e
  175. JOIN departments d ON e.department_id = d.department_id
  176. JOIN locations l ON d.location_id = l.location_id
  177. GROUP BY
  178.         country_id;
  179. #19.查询没有领导的员⼯的姓名以及职位名称。
  180.         SELECT
  181.         CONCAT( last_name, first_name ) AS 姓名,
  182.         job_title
  183. FROM
  184.         employees e
  185. JOIN jobs j ON e.job_id = j.job_id
  186. WHERE
  187.         manager_id is null ;
  188. #20.查询job_id为"IT_PROG"的员⼯的姓名、职位名称以及⽉薪。
  189.         SELECT
  190.         CONCAT( last_name, first_name ) AS 姓名,
  191.         job_title,
  192.         salary
  193. FROM
  194.         employees e
  195. JOIN jobs j ON e.job_id = j.job_id
  196. WHERE
  197.         e.job_id = 'IT_PROG';
复制代码
好了,今天的分享结束了,答案仅供参考不代表最终答案,如果有更换的方法,欢迎在底下留言评论!

来源:https://www.cnblogs.com/cjybigdatablog/p/18334913
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具