×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT技术讨论 / here is the table, write a query to find out who have second highest salary?
    Name Salary
    -------------------------------------------------- -----------
    Bob 23456
    Sam 34567
    Bill 45678
    Green 12345
    Ken 12345
    Welly 59000

    (6 row(s) affected)
    • "Can you tell me who earns a little bit less than the top paid guy? "
    • select * from name_salary where salary < (select max(salary) from name_salary) and rownum < 2; (FOR ORACLE)
      • it's not correct
        your query equals to this one

        SELECT top 1 name, salary from employee WHERE salary NOT IN
        ( SELECT TOP 1 salary FROM employee ORDER BY salary DESC )
        order by salary desc

        but imagine if there are more than 1 people
        have the same salary which is second highest?
        your hardcoded number 2 does not work in thi situation
    • suppose the name of table is tn:
      select name, salary from tn tn3
      where salary in
      (
      select max(salary) from tn t1
      where salary not in
      (
      select max(salary) from tn t2
      )
      )
    • select * from ( select name from [table name] order by salary desc ) where rownum = 2;
    • My Answer (in MS SQL), similar like kiwi's
      select * from employee
      where salary=(
      select max(salary) from employee where
      salary<(select max(salary) from employee)
      )

      anybody knows answer in Informix or DB2?