Please read the Flexibility using TOP clause in SQL Server 2005 for more details.
Lets say, we are having student and their marks, and we want the nth max mark. First will see the table structure and will add few data into it.
SET NOCOUNT ON
DECLARE @Marks TABLE
(
StudName VARCHAR(100),
Mark INT
)
INSERT INTO @Marks VALUES('AAAAA', 55)
INSERT INTO @Marks VALUES('BBBBB', 65)
INSERT INTO @Marks VALUES('CCCCC', 59)
INSERT INTO @Marks VALUES('DDDDD', 52)
INSERT INTO @Marks VALUES('FFFFF', 65)
INSERT INTO @Marks VALUES('EEEEE', 95)
SELECT Mark FROM @Marks ORDER BY Mark DESC
Mark
-----------
95
65
65
59
55
52
Now we write the query which allow us to find nth max mark form this list.
DECLARE @Top INT
SET @Top = 2
SELECT MIN(Mark) AS 'Top' FROM(
SELECT DISTINCT TOP (@Top) Mark FROM @Marks ORDER BY Mark DESC) A
Top
-----------
65
@Top is variable; which gives you the ability to fetch Nth max.