sql query group by departname with other table in other table

时间:2016-05-30 04:16:52

标签: mysql

表:员工

EmpID: INTEGER NOT NULL
DeptID: INTEGER NOT NULL
Name: Char(40) NOT NULL
Age: INTEGER NOT NULL
Salary: INTEGER NOT NULL

表:部门

DeptID: INTEGER NOT NULL
Name: CHAR(20) NOT NULL

我们需要编写一个SQL查询,输出所有部门的名称,使部门的平均工资大于10,00,000。输出应只包含一列:DeptName

试过这个:

SELECT AVG(Salary) AS avg_salary ,DeptID FROM ( SELECT DeptID, Salary FROM employee GROUP BY 1) DeptID GROUP BY DeptID HAVING avg_salary > 10;

结果:无法加入。

1 个答案:

答案 0 :(得分:0)

以下是查询:

SELECT 
Department.name AS DeptName
FROM Department 
INNER JOIN Employee ON Department.DeptID = Employee.DeptID
GROUP BY Department.DeptID
HAVING AVG(Employee.Salary) > 1000000;