【问题标题】:Write down the SQL to show the department in which the average salary of the employees (whose salary is greater than 5000) is less than 8000写下SQL显示员工(工资大于5000)平均工资小于8000的部门
【发布时间】:2017-03-20 17:24:34
【问题描述】:

写下SQL,显示员工(工资大于5000)平均工资低于8000的部门。

这是我写的。我不确定这是否正确。

select departments.department_name, employees.avg(salary) as avgsalary from departments
inner join employees
on departments.department_id = employees.department_id
where avgsalary > 5000 and avgsalary < 8000;

【问题讨论】:

  • 你需要一个 group by 和一个 (having 而不是 where)
  • 作为建议,请在每个 fromjoinwheregroup byhaving 之前换行,但不要在 on 之前换行(或缩进on)

标签: sql


【解决方案1】:
select departments.department_name
    , avg(employees.salary) as avgsalary 
from departments inner join employees
    on departments.department_id = employees.department_id
group by departments.department_name
having avg(employees.salary) > 5000 and avg(employees.salary) < 8000;

【讨论】:

    猜你喜欢
    • 2020-08-09
    • 2011-04-28
    • 2021-09-09
    • 2019-01-25
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 2016-07-07
    • 2012-08-02
    相关资源
    最近更新 更多