Search This Blog

Thursday, November 8, 2007

Rank Function

As the name suggests it does the same of displaying rank againt each record.

You might wonder ROWNUM also does the same function then why do i need one more function of the same feature.Answer is Rank provides the same Serial number if two records have the same value.Lets understand the same by means of a example.

Students Table
StudentID
Student Name
Total Marks

Records
S1, "Mark",450
S2, "Lloyd",400
S3, "Rob", 327
S4, "Henry",450
S5, "Peter",327

Query using Rank
Select StudentID,
FirstName,
TotalMarks,
Rank() Over (Order by TotalMarks desc)
From Students

Output
S1, "Mark",450,1
S4, "Henry",450,1
S2, "Lloyd",400,2
S3, "Rob", 327,3
S5, "Peter",327,5

No comments: