to find 4th maximum in a sql table

  • i am using sql server 2005 express edition...how to write a simple query to find the 4th or 3rd maximum value from a table..

    i tried with 'limit'. but its not working....

    note : pls dont give multiple subqueries...

    help me..

  • try looking into RANK() or ROW_NUMBER() functions.....

    Kev

  • If you post your table def, and some sample data i am sure there is a solution...

  • You really need to read this article:

    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

  • Hii,

    There are different ways to get this..!

    Try This...

    ----------------------------------------------------------

    DECLARE @Emp Table ( ID int, Name Varchar(10), Salary int)

    INSERT INTO @Emp

    SELECT 1,'A',20000 UNION

    SELECT 2,'B',25000 UNION

    SELECT 3,'C',15000 UNION

    SELECT 4,'D',18000 UNION

    SELECT 5,'E',22000 UNION

    SELECT 6,'F',24000

    -- First Method

    SELECT Min(Salary) FourthMax

    FROM (SELECT TOP 4 * FROM @Emp ORDER BY Salary Desc) AS Temp

    -- Second Method

    SELECT Name,Salary

    FROM (SELECT *,Row_Number() OVER (Order By Salary Desc) AS Row_Number FROM @Emp) as Temp

    WHERE Row_Number = 4

    If still you have some problem then feel free to ask... 😛

Viewing 5 posts - 1 through 4 (of 4 total)

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