I'm working on a homework problem where the unit deals with Subqueries. This is the only problem dealing with an aggregate function, and I don't know how to properly write the query. This is what my professor is asking-
"Create a query to display empno column, ename column, sal column, and the dept’s average sal data from emp table. Display all employees who have earned more than the same deptno’s average sal."
Here is what the correct output should look like from his example-
EMPN ENAME SAL AVG(SAL)
---- --------------- ---------- ----------
7788 SCOTT 3000 2916.66667
7839 KING 5000 1566.66667
7698 BLAKE 2850 2175
7782 CLARK 2450 1566.66667
7566 JONES 2975 2175
7902 FORD 3000 2175
I'm so close to the correct answer, I think I need to use the 'deptno' somewhere in my code, but I don't know where/how. Here is my code-
SQL> SELECT empno, ename, sal, AVG(sal) AS "DEPT_AVG_SAL"
2 FROM emp
3 GROUP BY empno, ename, sal
4 HAVING sal >
5 (SELECT AVG(sal)
6 FROM emp);
Here is the output I get. The 'sal' column is correct, just not the 'AVG(sal)'-
EMPN ENAME SAL DEPT_AVG_SAL
---- --------------- ---------- ------------
7788 SCOTT 3000 3000
7839 KING 5000 5000
7698 BLAKE 2850 2850
7782 CLARK 2450 2450
7566 JONES 2975 2975
7902 FORD 3000 3000
6 rows selected.
Thank you for anyone's help!