1. We have moved to a new forum! There may be a few things not working properly so please let us know if you find a bug. Remember to use the bbCode [ sql ] tag for SQL statements.

i want to get the third highest salary from a table?

Discussion in 'SQL - Questions and Answers' started by Prashant Yadav, Dec 10, 2006.

  1. Dialect: SQL 92
    table is two fields
    Emp_id and Emp_salary

    How to get third highest salary without using YOP function.
  2. Laura G. Guest

    Dialect: T-SQL
    SQL:

    SELECT top 3 * into #topthree FROM dbo.myTable
    ORDER BY Emp_salary DESC
    SELECT top 1 * FROM #topthree
    ORDER BY Emp_salary
  3. Don D Guest

    Dialect: SQL 92
    SQL:

    SELECT TOP 1 *
    FROM (SELECT TOP 3 *
    FROM employees
    ORDER BY salary) Top3
    ORDER BY salary DESC
  4. Don D Guest

    Dialect: OTHER
    On second thought...

    What's a YOP function? Did you mean "...without using TOP function."?
  5. Hank Swart Guest

    Dialect: MS SQL
    SQL:

    DECLARE @tblTempStorage TABLE (
    [ID] INT IDENTITY(1, 1),
    Result FLOAT)

    INSERT INTO @tblTempStorage (Result)
    SELECT DISTINCT
    DEPTH_TO
    FROM
    GB_SAMPLE
    ORDER BY
    DEPTH_TO

    SELECT
    *
    FROM
    GB_SAMPLE
    WHERE
    DEPTH_TO = (
    SELECT
    Result
    FROM
    @tblTempStorage
    WHERE
    ([ID] = 3))
  6. GasGiant Guest

    Dialect: MySQL
    SQL:

    SELECT Emp_id, Emp_salary
    FROM employees
    ORDER BY Emp_salary
    LIMIT 2, 1;
  7. Bga Guest

    Dialect: Oracle
    Oracle allows to select pseudo-columns with a special meaning.
    SQL:

    SELECT *
    FROM (
    SELECT ROWNUM as rank, Emp_id, Emp_salary
    FROM employees
    ORDER BY Emp_salary
    )
    WHERE rank=3
    ;
  8. deepak garg New Member

    use for any highest salary from the table

    SQL:
    select min(sal) from (select sal from emp where sal is not NULL order by sal desc) where rownum<=&rownum
  9. lavanya New Member

    I am not sure but it will definitely work just try once.


    SQL:
    ( select salary from <tablename> where sal <(select salary from <tablename> where sal <(select max(sal) from <tablename>))) 


    This will give the third highest salary from a table.
    And for the second highest salary just remove one phrase that is

    SQL:
    select salary from <tablename> where salary<(select max(salary) from <tablename>)
  10. Unregistered Guest

    SQL:
    Select Empname,salary From ##employees A Where 3=(select Count (distinct(salary)) From ##employees B 
    Where B.salary>=a.salary)
  11. supriya Guest

    SQL:
    select TOP 1 salary from Employee( select distinct top 3 salary from employee ORDER BY DESC) a ORDER BY salary
  12. Unregistered Guest



    SQL:
    select salary from <tablename> where salary<(select max(salary) from <tablename> wont work for second highest salary.


    The correct query is
    SQL:
    select max(salary) from <tablename> where salary<(select max(salary) from <tablename>


    to get the third highest salary :

    SQL:
    ( select max(salary) from <tablename> where sal <(select max(salary) from <tablename> where sal <(select max(sal) from <tablename>)))
  13. vens Guest

    SQL:
    select sal from  employees .e where &n=(select distinct(sal) from employees where e.sal<=employees.sal);
  14. manjunath ps Guest

    SQL:
     select * from emp a where 2=(select count(*) from emp b where a.sal<b.sal);
  15. Unregistered Guest

    Consider the Table Salary_TB with 2 columns empname,salary

    SQL
    ----
    SQL:
    select top 1 (salary) from Salary_TB  where salary< (select max(salary) from Salary_TB where salary<(select max(salary) from Salary_TB ) ) order by salary desc


    i think this can understand all to get third highest salary from a table

    Anoop.G
    3MenTechnologies
    9847160368
    anoop.gs@gmail.com
  16. Vishal Guest

    y go in for such complex query..try this

    SQL:
    select min(Emp_Sal) from Employee_Test where Emp_Sal in
    (select distinct top 2 Emp_Sal from Employee_Test order by Emp_Sal desc)


    Vishal
  17. bhaskar New Member

    :cool

Share This Page