写下SQL显示员工(工资大于5000)平均工资小于8000的部门
Posted
技术标签:
【中文标题】写下SQL显示员工(工资大于5000)平均工资小于8000的部门【英文标题】: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 【发布时间】: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
)
作为建议,请在每个 from
、join
、where
、group by
、having
之前换行,但不要在 on
之前换行(或缩进on
)
【参考方案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;
【讨论】:
以上是关于写下SQL显示员工(工资大于5000)平均工资小于8000的部门的主要内容,如果未能解决你的问题,请参考以下文章