SQL SERVER – Query to Retrieve the Nth Maximum Value

Replace Employee with your table name, and Salary with your column name. Where N is the level of Salary to be determined. Let us see a query to retrieve the Nth Maximum Value.

SQL SERVER - Query to Retrieve the Nth Maximum Value nthhigh-800x291

SELECT *
FROM Employee E1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(E2.Salary))
FROM Employee E2
WHERE E2.Salary > E1.Salary)

In the above example, the inner query uses a value of the outer query in its filter condition, meaning; the inner query cannot be evaluated before evaluating the outer query. So each row in the outer query is evaluated first and the inner query is run for that row.

In above query where you see (N-1), you can replace the N with any other number based on your need. For example, if you want to find the 10th highest record, you can just write (10-1) or just 9 there and it will bring back necessary record. It is a very easy query and requires one time proper understanding how it works.

Here is another relevant blog post which has working examples of AdventureWorks database: SQL SERVER – Find Nth Highest Record from Database Table

Let me know if you have any question in the comments section. I keep on sharing the various tips, I suggest you sign up for my newsletter http://go.sqlauthority.com

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL Function, SQL Joins, SQL Scripts, SQL Server
Previous Post
SQL SERVER – Locking Hints and Examples
Next Post
SQL SERVER – Good, Better and Best Programming Techniques

Related Posts

63 Comments. Leave new

  • Nicholas Paldino [.NET/C# MVP]
    April 27, 2007 8:28 am

    It should be noted that for SQL Server 2005, using the row_number ranking function is a better, meaning easier to maintain, and more performant.

    Reply
  • Wonderful. Thanks. keep on writing.
    You are true genius.

    Reply
  • Manohar K Patidar
    May 11, 2007 3:41 am

    Hi,

    Thanks for the query.

    Regards
    Manohar K Patidar

    Reply
  • Yogesh D Nayak
    May 29, 2007 3:15 am

    Hi

    This is a very good query

    Keep it up

    regards
    Yogesh D Nayak

    Reply
  • Durga Prasad
    June 18, 2007 4:31 am

    I got the correct solution from your website. thank u.

    Regards,
    Durga Prasad A.

    Reply
  • kishor khilari
    July 30, 2007 9:32 pm

    Hi,
    thanks for the best query.

    Best regards…
    kishor

    Reply
  • Hello,

    I want to find first 10 rows with max value from a table. Can you suggest me what should be query for this ?

    Thanking you in advance.

    Regds,
    Bhavesh

    Reply
  • Select Top 10 distinct Name,Vlaue from Table1 order by Value desc

    Reply
  • Your examples are very useful in my job

    Reply
  • Dear Sir/Madam,

    Thanks for the perfect solution to find out the Nth record from sql server database.

    Reply
  • Hi,
    Tanks for the perfect n easy solution.

    Thanks n Regards,
    Manjusha

    Reply
  • Tks for u r solution

    Reply
  • Hi,

    Thanks for the perfect solution to find out the Nth record from sql server database.

    One request:
    how to get top 3 departments and bottom 3 departments

    waiting for ur reply

    with reagards
    sekar

    Reply
    • Top 3

      Select top 3 * from table order by dept desc

      Bottom 3

      Select top 3 * from table order by dept asc

      But you need to define what you meant by Top and bottom

      Reply
  • hi

    i want a query .dynamicale i new row should Added and it should display in Gui by using a Query.pleas provide me this query.

    Regards
    Srinath

    Reply
  • Hi executing the same query using Top keyword is much faster, when the number of records are comparatively too large. FYI (copied from the same site :) )

    SELECT TOP 1 salary
    FROM (
    SELECT DISTINCT TOP 6 salary
    FROM employee
    ORDER BY salary DESC) a
    ORDER BY salary

    Reply
  • I want to find first 10 rows with max value from a table.
    and

    how to get top 3 departments and bottom 3 departments
    Can you suggest me what should be queries for above two questions?

    Thanks
    krishna

    Reply
  • MilesPerSecond
    July 31, 2008 3:00 pm

    Hi All,
    I compared above said query and found this one performming like a Lamborghini Reventon as compared to the above proposed Ambassador, for table(id,name,salary) with 20K records:

    DECLARE @myNth INT
    SET @myNth=56
    SELECT TOP 1 SALARY
    FROM(
    SELECT TOP(@myNth) SALARY
    FROM MinMaxTable
    ORDER BY Salary DESC
    ) T
    ORDER BY Salary ASC

    So This one is the BEST !!!!

    Reply
  • Hello sir,
    i have one dpubt……..
    How will we identify the duplicate key by refer the number?

    Reply
  • Your query is correct if it came from student who has 5 minutes to solve this problem.

    But if it in real world application,
    Your query is very low performance.

    Reply
  • Hi

    following query will find out the nth max salary from employee table. just replace the number 5 with your number. Also it should check the duplicate value in salary field. I think Bala wants to know this.

    SELECT max(salary) FROM ( SELECT DISTINCT TOP 5 salary FROM employee order by salary) a

    Regards
    Satyabrata Paul

    Reply

Leave a Reply