Home:ALL Converter>SQL: avg() function in where clause

SQL: avg() function in where clause

Ask Time:2021-06-05T18:38:16         Author:Nofal

Json Formatter

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". How can I use avg() function in where clause?

select   sal 
from emp
where sal> (avg(sal)) ;

Author:Nofal,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/67848679/sql-avg-function-in-where-clause
Jayvee :

you may need to use subquery:\nselect sal \nfrom emp\nwhere sal> (select avg(sal) avgsal from emp) ;\n",
2021-06-05T10:47:08
Loupeznik :

You can't use AVG in WHERE clause like that. Try this query:\nSELECT sal FROM emp WHERE sal > (SELECT AVG(sal) FROM emp)\n",
2021-06-05T10:47:04
yy