.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> SQL Query >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to Use Rank () function in SQL sever 2005 & 2008

Posted By:Sajid P K       Posted Date: November 25, 2010    Points: 15    Category: SQL Query    URL: http://www.dotnetspark.com  

The Code segment explain you, how to use rank() function in sql server 2005 & 2008 with a simple example.
 

In order to demonstrate of Rank () function, we need a table. So here we are going to use "Student" table. This table consists of two Column 'Name' and 'ToltalMark'.

 

SQL query for given below can be use for Creating and Populating Student table.

SET NOCOUNT ON
CREATE TABLE Student(FirstName VARCHAR(20),Mark INT)

INSERT INTO Student VALUES ('Sree Hari',23)
INSERT INTO Student VALUES ('Sajid',40)
INSERT INTO Student VALUES ('George',6)
INSERT INTO Student VALUES ('Mary',11)
INSERT INTO Student VALUES ('Sam',17)
INSERT INTO Student VALUES ('Jafar',6)
INSERT INTO Student VALUES ('Babu',38)
INSERT INTO Student VALUES ('Larry',5)
INSERT INTO Student VALUES ('Malu',29)
INSERT INTO Student VALUES ('Nisha',49)
INSERT INTO Student VALUES ('Biju',23)

The RANK function can use as given below

SELECT RANK() OVER (ORDER  BY   Mark DESC) AS [Rank],      
    FirstName,  
    Mark 
 FROM student

 


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend