当前位置:   article > 正文

mysql——子查询经典案例_子查询样例

子查询样例

子查询经典案例

1. 查询工资最低的员工信息: last_name, salary

  1. SELECT last_name,salary
  2. FROM employees
  3. WHERE salary=(
  4.     SELECT MIN(salary)
  5.     FROM employees
  6. );

2. 查询平均工资最低的部门信息

  1. SELECT *
  2. FROM departments
  3. WHERE department_id=(
  4.     SELECT department_id
  5.     FROM employees
  6.     GROUP BY department_id
  7.     ORDER BY AVG(salary) ASC
  8.     LIMIT 1
  9. );
  1. SELECT *
  2. FROM departments
  3. WHERE department_id=(
  4.     SELECT department_id
  5.     FROM employees
  6.     GROUP BY department_id
  7.     HAVING AVG(salary)=(
  8.         SELECT MIN(avgs)
  9.         FROM (
  10.             SELECT AVG(salary) avgs,department_id
  11.             FROM employees
  12.             GROUP BY department_id
  13.         ) avg_table
  14.     )
  15. );

3. 查询平均工资最低的部门信息和该部门的平均工资

  1. SELECT d.*,AVG(salary)
  2. FROM departments d
  3. INNER JOIN employees e ON d.department_id=e.department_id
  4. WHERE
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/999616
推荐阅读
相关标签
  

闽ICP备14008679号