employee表的全部数据:
SELECT EMPLOYEE_id,salary,avg(salary) FROM test1.employees
group by employee_id
having avg(salary)>8000;
使用这个查询语句想得到平均工资大于8000的平均工资,可是为什么结果不是平均工资呢?
高洛峰2017-04-17 15:06:36
Because you group by id, doesn’t there only be one salary under each ID?
select EMPLOYEE_id,salary,(select avg(salary) FROM employees) from employees where salary > (select avg(salary) FROM employees);
Probably, you can give it a try
怪我咯2017-04-17 15:06:36
You are grouping by employee_id. There is only one record for each employee_id in the table record. Doesn’t your avg have only one value?
If you want the average salary of all employees, don’t groupb by
迷茫2017-04-17 15:06:36
I have a problem understanding, average salary, what is average? If it is for employee_id, how can a person's salary be said to be average salary. Generally speaking, it is the salary of employees in a certain department whose average salary is greater than 8,000
select t.salary,t.employee_id,t1.avg_salary from employees t inner join (select department_id,avg(salary) avg_salary from employees group by department_id) t1 on t.department_id = t1.department and t1.avg_salary > 8000