[英]SQL: avg() function in where clause
I want the salary of all employees whose sal
(salary) is greater than the average salary of all employees, however I get the error "invalid use of group function".我想要所有员工的sal
(薪水)大于所有员工的平均薪水,但是我收到错误“无效使用组功能”。 How can I use avg()
function in where
clause?如何在where
子句中使用avg()
function?
select sal
from emp
where sal> (avg(sal)) ;
You can't use AVG in WHERE clause like that.你不能像这样在 WHERE 子句中使用 AVG。 Try this query:试试这个查询:
SELECT sal FROM emp WHERE sal > (SELECT AVG(sal) FROM emp)
you may need to use subquery:您可能需要使用子查询:
select sal
from emp
where sal> (select avg(sal) avgsal from emp) ;
You can use avg
as a window function
, this normally yields better performance over a sub-query:您可以将avg
用作window function
,这通常比子查询产生更好的性能:
select Sal from (
select Sal, Avg(Sal) over() avgSal
from Employees
)e
where Sal > avgSal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.