Results 1 to 8 of 8

Thread: 1st and 2nd Maximum salary through same SQL Query

  1. #1
    Junior Member
    Join Date
    Jun 2007
    Answers
    4

    1st and 2nd Maximum salary through same SQL Query

    How can I get first maximum salary as well as second maximum salary thru the same SQL query?


  2. #2
    Expert Member
    Join Date
    Jun 2006
    Answers
    410

    Re: 1st and 2nd Maximum salary through same SQL Query

    You can get it. But tell me you want this in single record or in two records?


  3. #3
    Junior Member
    Join Date
    Oct 2007
    Answers
    2

    Re: 1st and 2nd Maximum salary through same SQL Query

    By using below query u can retrieve the 1st & 2nd maximum salaries select sal from(select sal,rownum from emp group by sal,rownum order by sal desc) group by sal, having rownum=1 or rownum=2 order by sal desc; ok. I think u got it.


  4. #4
    Junior Member
    Join Date
    Feb 2009
    Answers
    4

    Re: 1st and 2nd Maximum salary through same SQL Query

    select top 2 salary from table_name order by salary desc


  5. #5
    Junior Member
    Join Date
    Mar 2009
    Answers
    6

    Re: 1st and 2nd Maximum salary through same SQL Query

    select (select max(sal) from tbl_employee), max(sal)from tbl_employee
    where sal < (select max(sal) from tbl_employee)


  6. #6
    Junior Member
    Join Date
    Mar 2010
    Answers
    1

    Re: 1st and 2nd Maximum salary through same SQL Query

    select top 1 * from having salary < max(salary) order by salary desc


  7. #7
    Junior Member
    Join Date
    Nov 2009
    Answers
    1

    Re: 1st and 2nd Maximum salary through same SQL Query

    Quote Originally Posted by amarmail_999 View Post
    How can I get first maximum salary as well as second maximum salary thru the same SQL query?
    select sal from emp e where &n=select count(sal) from (select max(sal) from emp f )where e.sal<=f.sal


  8. #8
    Expert Member
    Join Date
    Apr 2007
    Answers
    500

    Re: 1st and 2nd Maximum salary through same SQL Query

    Try this ..

    Select empno, ename, sal,
    DECODE (ROWNUM,
    1, ROWNUM || 'st Highest Salary',
    2, ROWNUM || 'nd Highest Salary'
    ) remark
    From (Select *
    From emp e
    Order By sal Desc)
    Where ROWNUM <= 2

    Sireesha

  9.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact