To Find the 2nd Maximum Of Mark in a Data Set
SELECT * FROM Student a WHERE 2=(SELECT COUNT(DISTINCT Mark) FROM Student b WHERE a.Mark<=b.Mark)
To Find the 2nd Minimum Of Mark in a Data Set
SELECT * FROM Student a WHERE 2=(SELECT COUNT(DISTINCT Mark) FROM Student b WHERE a.Mark>=b.Mark)
To Find nth Maximum and Minimum
SELECT * FROM Student a WHERE n=(SELECT COUNT(DISTINCT Mark) FROM Student b WHERE a.Mark<=b.Mark)
SELECT * FROM Student a WHERE n=(SELECT COUNT(DISTINCT Mark) FROM Student b WHERE a.Mark>=b.Mark)
replace n with the corresponding number
Popularity: 2% [?]
Related posts:

March 21st, 2010
Jaffar
Posted in 

I am using SQL Server 2008. I have a Student table in which there are following fields:
1. StudentId, 2. StudentName, 3. Marks .
I want to get a resultset in which there should be a column named “Position”. Something like “Select StudentId,StudentName,Marks, as Position from Student” so that, depending on the marks a student scored, i can evaluate them as the 1st, 2nd or 20th position. If students have the same marks, then they have the same position.
Thanks in advance.
afyanor
Thanks for your articles Jaffar.
I got what i was looking for at:
http://msdn.microsoft.com/en-us/library/ms189798.aspx