SQLTeam.com | Weblogs | Forums

Top 5 sql records

sql2012

#1

i wanna select top 5 record of my database on the basis of marks, but if a case comes when 6th record or 7th record of my databse has same marks as 5th record marks,

then it should include 6 record or even 7th record, but keep in mind if 6th record marks are not same as 5th record, then it normaly select top 5, what should i do?

pplzz help


#2

take a look at dense_rank() window function

https://msdn.microsoft.com/en-sg/library/ms173825.aspx


#3

You may try this one.

 SELECT *
    FROM
    (
    SELECT *,
    DENSE_RANK() OVER (ORDER BY Marks DESC) AS Rnk
    FROM Table
    )t
    WHERE Rnk <= 5