When you are looking for the 3rd highest, you can consider the following scenario:
Get the highest number, where the number is not in the Top 2 highest numbers.
That is ignore/remove the TOP 2, then the topmost(Top 1 remaining) is the 3rd highest of the original.
Code:
SELECT sname
,smark
FROM studentmarks A
WHERE a.smark IN (
SELECT TOP 1 smark
FROM studentmarks ZZ
WHERE zz.sname = a.sname
AND smark NOT IN (
SELECT TOP 2 smark
FROM studentmarks ww
WHERE ww.sname = a.sname
ORDER BY smark DESC
)
)
GROUP BY sname
,smark
ORDER BY smark DESC;
I used your data to create table StudentMarks with fields Id, SName and SMark.
Result based on the data provided:
sname |
smark |
Student2 |
8 |
Student1 |
3 |