Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.tools -> Re: correlated subquery
"m.mueller" <m.mueller_at_snafu.de> schrieb im Newsbeitrag
news:3A77208B.9DEF2BC9_at_snafu.de...
> Hello,
> I'm trying to understand correlated subquery, got result that puzzles
> me.
Hi Manuela,
I am not sure to understand your question as you mean it. Did you notice, that the first query lists all employes with a salary higher than the average salary of the department (of the employes) while the last query lists all employes with a salary higher than the avergabe of all employes?
> SELECT deptno, ename, sal
> FROM emp emp_alias1
> WHERE sal >
> (SELECT avg(sal)
> FROM emp emp_alias2
> WHERE emp_alias1.deptno = emp_alias2.deptno
> )
> ORDER BY deptno;
>> selected?
> output:
> DEPTNO ENAME SAL
> ---------- ---------- ----------
> 10 KING 5000
> 20 JONES 2975
> 20 SCOTT 3000
> 20 FORD 3000
> 30 ALLEN 1600 <- sal < avg(sal), why is this row
The average salary of department 30 seems to be lower than 1600.
> SELECT avg(sal)
> FROM emp;
>
This is the average salary of all employes in all departments.
> SELECT deptno, ename, sal
> FROM emp
> WHERE sal >
> (SELECT avg(sal)
> FROM emp
In the first query there is a where-clause at this place!
> )
> ORDER BY deptno;
Hope to have helped you...
Holger Kreft Received on Wed Jan 31 2001 - 06:33:47 CST