sub qry

  • HI there have 2 tbls

    dept (deptno(pk), dname(varcahr), loc(varchar)

    emp(empno(pk), deptno(fk), sal, job, ename)

    for each loc.. need to display emploname, his job & who has the highest sal.

    I can do by deptno, but not by loc pl shelp!

    **

    select deptno, ename, job, sal

    from emp

    where (deptno, sal) IN

    (select deptno, max(sal) from emp group by deptno)

    DEPTNO ENAME JOB SAL

    ---------- ---------- --------- ----------

    10 KING PRESIDENT 5000

    20 FORD ANALYST 3000

    20 SCOTT ANALYST 3000

    30 BLAKE MANAGER 2850

  • how about this :

    select e.deptno, e.ename, e.job, M.MaxSal

    from emp e

    inner join

    (select deptno, max(sal) as MaxSal from emp group by deptno) M

    on e.deptno = M.deptno

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • quote:


    how about this :

    select e.deptno, e.ename, e.job, M.MaxSal

    from emp e

    inner join

    (select deptno, max(sal) as MaxSal from emp group by deptno) M

    on e.deptno = M.deptno


    I would change that to:

    select e.deptno, e.ename, e.job, M.MaxSal

    from emp e

    inner join

    (select deptno, max(sal) as MaxSal from emp group by deptno) M

    on e.deptno = M.deptno AND e.sal = M.MaxSal


    * Noel

  • correct, noeld.

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply