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.
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)
63 Comments. Leave new
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.
Hi,
This is the very nice query
Regards,
Manoj Kumar Singh
Wonderful. Thanks. keep on writing.
You are true genius.
Hi,
Thanks for the query.
Regards
Manohar K Patidar
Hi
This is a very good query
Keep it up
regards
Yogesh D Nayak
I got the correct solution from your website. thank u.
Regards,
Durga Prasad A.
Hi,
thanks for the best query.
Best regards…
kishor
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
Select Top 10 distinct Name,Vlaue from Table1 order by Value desc
Your examples are very useful in my job
Dear Sir/Madam,
Thanks for the perfect solution to find out the Nth record from sql server database.
Hi,
Tanks for the perfect n easy solution.
Thanks n Regards,
Manjusha
Tks for u r solution
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
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
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
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
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
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 !!!!
Yeah.. I agree with your approach..
Hello sir,
i have one dpubt……..
How will we identify the duplicate key by refer the number?
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.
Then try methods 4 and 5 specified in this link
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