query on emp table [message #334607] |
Thu, 17 July 2008 06:18 |
jyothsna1612
Messages: 68 Registered: June 2008
|
Member |
|
|
Hi
I need the HINT for the following query
Display the empno,ename, sal for all employees who earn more than the average sal and who work in a department with any employee
I tried the following:(In select clause the columns are different)
SELECT SAL,
COUNT(* )
FROM EMP
WHERE SAL > ANY (SELECT AVG(SAL)
FROM EMP
GROUP BY DEPTNO)
GROUP BY DEPTNO,
SAL;
I even tried with the > all but none gave correct result..
Please excuse me if this is a newbie question.
Thanks
Jyothsna
|
|
|
|
Re: query on emp table [message #334616 is a reply to message #334607] |
Thu, 17 July 2008 06:43 |
jyothsna1612
Messages: 68 Registered: June 2008
|
Member |
|
|
Hi
There would be a chance to misunderstand the query as this is not much clear for the first time.
Ex:
The avg(sal) for deptno 10 = 2916. If The employees who belong to only dept 10 earns more than 2916, then his details must be displayed..
Like the same approach for dept 20 and 30. also
I think now i am somewhat clear to this query..
|
|
|
|
Re: query on emp table [message #334620 is a reply to message #334617] |
Thu, 17 July 2008 07:17 |
jyothsna1612
Messages: 68 Registered: June 2008
|
Member |
|
|
My doubt is:
SQL> SELECT DEPTNO,
ROUND(AVG(SAL))
FROM EMP
GROUP BY DEPTNO;
DEPTNO ROUND(AVG(SAL))
---------- ---------------
30 1567
20 2175
10 2917
Here deptno10's avg(sal) is 2917
But check the o/p of the query i wrote:
SELECT SAL,deptno,
COUNT(* )
FROM EMP
WHERE SAL > ANY (SELECT AVG(SAL)
FROM EMP
GROUP BY DEPTNO)
GROUP BY DEPTNO,
SAL;
SAL DEPTNO COUNT(*)
---------- ---------- ----------
5000 10 1
3000 20 2
2450 10 1
1600 30 1
2850 30 1
2975 20 1
In the above o/p: the salary 2450 for dept10 which is
less than the avg(sal) of deptno10 i.e.,2917 , and is the wrong o/p:
So HINT me to change my query:
|
|
|
|
|
|
|
Re: query on emp table [message #334646 is a reply to message #334607] |
Thu, 17 July 2008 09:24 |
sivanagahari.devarapalli@
Messages: 1 Registered: May 2008
|
Junior Member |
|
|
Hi,
First let me express my understanding about question:
===============================================================
Display emp details whose salary is greater than average salary of department in which he is working.
===============================================================
----------------------------------------------------------------
According to this first try to get each department's AVG salary:
----------------------------------------------------------------
SELECT deptno,ROUND(avg(sal)) avg_sal
FROM emp
GROUP BY deptno;
This query results:::
DEPTNO AVG_SAL
--------- ----------
30 1567
20 2175
10 2917
----------------------------------------------------------------
Now try to form final statement using the above statement as part of FROM clause "INLINE VIEW" :
----------------------------------------------------------------
SELECT empno,ename,sal,e.deptno
FROM emp e, (SELECT deptno,ROUND(avg(sal)) avg_sal
FROM emp
GROUP BY deptno) i
WHERE e.deptno=i.deptno AND e.sal >avg_sal;
EMPNO ENAME SAL DEPTNO
-------- ---------- ---------- ----------
7698 BLAKE 2850 30
7499 ALLEN 1600 30
7902 FORD 3000 20
7788 SCOTT 3000 20
7566 JONES 2975 20
7839 KING 5000 10
Excuse me for the format & confusion if any.
thanks & Regards,
Siva Naga Hari
|
|
|
|
Re: query on emp table [message #334780 is a reply to message #334646] |
Fri, 18 July 2008 00:19 |
jyothsna1612
Messages: 68 Registered: June 2008
|
Member |
|
|
Hi,
Michel is correct, as my query is taking the avg of all departments not a particular department. Now my confusion is clear
and query written by SIVA is correct according to my requirement.
Thank you again.
|
|
|
Re: query on emp table [message #334781 is a reply to message #334780] |
Fri, 18 July 2008 00:32 |
|
Michel Cadot
Messages: 68718 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
I encourage you to rewrite Siva's query the same way you wrote yours (using a subquery instead of an inline view), just as an exercise.
Regards
Michel
|
|
|