Wednesday 20 March 2013

Rank Function in SQL server


Written By:- Isha Malhotra (Software Developer)
Email:-malhotra.isha3388@gmail.com.
Note: -if you find this article helpful then kindly leave your comment

Rank Function in SQL server
SQL server also supports Some rank function also. To use the rank function you must have to assign it to Over function. In over function we give the order in which records will be Shown.

These functions are Following:-
   1.    Rank ()
This function is used to Define the rank according to the order which we set in order by clause.
For example:-
See the following records Of temp table:-

In this table there are Two records which having the same salary i.e. 1000.
Now apply the rank function on it.


select *, RANK() over( order by salary asc) as rank from emp;
as I am showing records in ascending order then it will show the output like that:-


As you can see that it gives same rank to the same rcords according to its order.



  2.  Row_number()
This function is used to count the row in a table.
For example:-
select *, row_number() over(order by salary asc) as 'Row Number' from emp;
this query will show the Output as follows:-


No comments:

Post a Comment