how to retrive the second highest value from the table

  • Ignore my last post and try this CODE

    ---- For 2nd highest value

    select max(salary) from employee_details e1

    where 2 <=(select count(*) from employee_details e2 where e1.salary<= e2.salary)

    ---- For 2nd highest value in noncluster

    select max(salary) from employee_details e1

    where 2 <=(select count(distinct salary) from employee_details e2 where e1.salary<= e2.salary)

  • I can't believe this thread is still alive...

    Maybe we'll have to write an article titled "100 ways to find 2nd highest salary"...:-D

    -- Gianluca Sartori

  • Gianluca Sartori (5/20/2009)


    I can't believe this thread is still alive...

    Maybe we'll have to write an article titled "100 ways to find 2nd highest salary"...:-D

    Fire the 1st guy and find max(salary) . . . 😛

    ---------------------------------------------------------
    How best to post your question[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

    "stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."

  • jcrawf02 (5/20/2009)


    Fire the 1st guy and find max(salary) . . . 😛

    [font="Verdana"]Pay everyone the same. :-P[/font]

  • Bruce W Cassidy (5/20/2009)


    jcrawf02 (5/20/2009)


    Fire the 1st guy and find max(salary) . . . 😛

    [font="Verdana"]Pay everyone the same. :-P[/font]

    Or like one of my former employers decided, don't pay anyone.

  • Would paying everyone NULL work?

    :w00t:

  • :w00t: :w00t: :w00t: :w00t: :w00t:

    May be this might also work :w00t::hehe::cool:;-):-)

    [Code]

    --

    use DB_TEST

    create table employee (E_id int,Name varchar(50),Gender char(20),Age int,SALARY MONEY,constraint E_pk primary key (E_id),constraint E_chk check (Gender in ('M','F')))

    SELECT SALARY FROM EMPLOYEE E1 WHERE 2=(SELECT COUNT(*) FROM EMPLOYEE E2 WHERE E1.SALARY<=E2.SALARY)

    --

    [/Code] :alien:

    Tanx 😀

  • Paul White (5/21/2009)


    Would paying everyone NULL work?

    :w00t:

    No, because NULL is undefined, could be $1, could be $1,000,000,000.00. You'd be bankrupt in a heartbeat :hehe:

    ---------------------------------------------------------
    How best to post your question[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

    "stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."

Viewing 8 posts - 16 through 22 (of 22 total)

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