DBAsupport.com Forums - Powered by vBulletin
Results 1 to 5 of 5

Thread: sql queries urgent help required

  1. #1
    Join Date
    Apr 2003
    Posts
    29

    sql queries urgent help required

    emp table

    empno,
    ename,
    mgr,
    sal,
    deptno

    dept table

    deptno,
    dname,
    loc

    I wanted the solution for the following queries.

    List all Departments Names and the second highest salary for that particular department.

    List employee name, his manager's name and salary of employee or manager whichever is higher.

  2. #2
    Join Date
    Aug 2002
    Location
    Atlanta
    Posts
    1,187
    homework sucks huh?
    I'm stmontgo and I approve of this message

  3. #3
    Join Date
    May 2001
    Location
    San Francisco, California
    Posts
    511
    No.. It is sweet if someone else does it.. for me
    Last edited by kris109; 01-26-2004 at 01:32 AM.
    Remember the Golden Rule - He who has the gold makes the rules!
    ===================
    Kris109
    Ph.D., OCP 8i, 9i, 10g, 11g DBA

  4. #4
    Join Date
    Jul 2003
    Posts
    53
    Try this for the first query

    SELECT d.dname,e.sal FROM dept d,emp e WHERE e.deptno = d.deptno AND e.deptno IN
    (SELECT deptno FROM
    (SELECT e.deptno,dense_rank() over (ORDER BY e.sal DESC) AS sal_rank FROM dept d,emp e
    WHERE d.deptno=e.deptno GROUP BY e.deptno)WHERE sal_rank =2 ) AND e.deptno = d.deptno
    anu

  5. #5
    Join Date
    Aug 2002
    Location
    Colorado Springs
    Posts
    5,253
    Originally posted by anupamasuresh
    Try this for the first query

    SELECT d.dname,e.sal FROM dept d,emp e WHERE e.deptno = d.deptno AND e.deptno IN
    (SELECT deptno FROM
    (SELECT e.deptno,dense_rank() over (ORDER BY e.sal DESC) AS sal_rank FROM dept d,emp e
    WHERE d.deptno=e.deptno GROUP BY e.deptno)WHERE sal_rank =2 ) AND e.deptno = d.deptno
    Better is ...

    Code:
    Select
       dname,
       sal
    From
       (
       Select
          d.dname,
          e.sal,
          Dense_Rank() Over (
             Partition By d.dname
             Order by e.sal Desc) rnk
       From
          dept d,
          emp e,
       Where
          d.deptno = e.deptno
       )
    Where
       rnk = 2
    /
    David Aldridge,
    "The Oracle Sponge"

    Senior Manager, Business Intelligence Development
    XM Satellite Radio
    Washington, DC

    Oracle ACE

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width