Replace Employee with your table name, and Salary with your column name. Where N is the level of Salary to be determined.
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.
Source: http://www.sqlteam.com/item.asp?ItemID=16134 (This link does not exist any more)






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.
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
how to get top 3 departments and bottom 3 departments
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
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
[...] 8, 2008 by pinaldave I have previously written SQL SERVER - Query to Retrieve the Nth Maximum value. I just received email that if I can write this using AdventureWorks database as it is default [...]
[...] Please read my article here to find Nth Highest Salary of Employee table : SQL SERVER - Query to Retrieve the Nth Maximum value [...]
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 !!!!
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.
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
Hi,
Can we find out the first inserted row from a table?
Regards
Manohar K Patidar Jamner (M. P. )
table_a table_b
salary salary
_____ _____
1000 2000
3000 4000
I want a T-sql query to get “4000″ as answer