I have not written about this subject for long time, as I strongly believe that Book On Line explains this concept very well. SQL Server 2005 has total of 4 ranking function. Ranking functions return a ranking value for each row in a partition. All the ranking functions are non-deterministic.
ROW_NUMBER () OVER ([<partition_by_clause>] <order_by_clause>)
Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.
RANK () OVER ([<partition_by_clause>] <order_by_clause>)
Returns the rank of each row within the partition of a result set.
DENSE_RANK () OVER ([<partition_by_clause>] <order_by_clause>)
Returns the rank of rows within the partition of a result set, without any gaps in the ranking.
NTILE (integer_expression) OVER ([<partition_by_clause>] <order_by_clause>)
Distributes the rows in an ordered partition into a specified number of groups.
All the above definition and syntax are taken from BOL. It is difficult to explain above function anything else than what they are explained in BOL. Following example is excellent example from BOL again. This function explains usage of all the four function together in one query.
USE AdventureWorks;
GO
SELECT c.FirstName, c.LastName
,ROW_NUMBER() OVER (
ORDER BY a.PostalCode) AS 'Row Number'
,RANK() OVER (
ORDER BY a.PostalCode) AS 'Rank'
,DENSE_RANK() OVER (
ORDER BY a.PostalCode) AS 'Dense Rank'
,NTILE(4) OVER (
ORDER BY a.PostalCode) AS 'Quartile'
,s.SalesYTD, a.PostalCode
FROM Sales.SalesPerson s
INNER JOIN Person.Contact c
ON s.SalesPersonID = c.ContactID
INNER JOIN Person.Address a
ON a.AddressID = c.ContactID
WHERE TerritoryID IS NOT NULL
AND SalesYTD <> 0;
Resultset:
Most of the content of this article is taken from BOL.
Reference : Pinal Dave (http://blog.SQLAuthority.com), ROW_NUMBER, RANK, DENSE_RANK, NTILE












Hi Pinal,
Can we use ROW_NUMBER() function in views ?
If not then what is the alternative method to give the row numbers in view
Thanks
-Sudhir
Very clearly described the definitions and example of all the 4. I was searching exactly this kind of concise & clear stuff.
Dave, Thanks.
-Manoj
Hi Pinal,
I have MS SQL 2000 on production server and as you know these functions are not available on same. My developer has used these functions extensively in application and insisting me to upgrade MS SQL 2000 to MS SQL 2005. This is not correct time for me to upgrade my system so can you please tell me the way out. Is it possible to use these functions in MS SQL 2000 by installing some patches or sp or by installing only t-sql of MS SQL 2005.
Thanks
Vikram
yes we can use try this……………..
CREATE VIEW testview AS SELECT ROW_NUMBER() OVER (ORDER BY )-1 as autoIndex,
* FROM
CREATE VIEW testview AS SELECT ROW_NUMBER() OVER (ORDER BY )-1 as autoIndex,
* FROM
hello,
Please clafiry this doubt In MYSQL query can use like this
select * FROM `table` WHERE MATCH (`email`) AGAINST (‘+firstname.lastname@hotmail.com’);
where incase of MSSQL what quey can use to select record from database exact records ..for example in textbox i’m entering chennai,software,IT. i want all the records related to this string .. please reply
Hi,
It was an amazing article with apt example.
AWESOME STUFF!!!
how to get values like this is sqlserver query?
Order Count,1,2,1-2,5,63,5-63,64,64,64,65,80,65-80,
[...] SQL SERVER – 2005 – Sample Example of RANKING Functions – ROW_NUMBER, RANK, DENSE_RANK, NTILE [...]
[...] Returns the rank of rows within the partition of a result set, without any gaps in the ranking. (Read More Here [...]
Hi Pinal,
I try to determine the newest Price from multiple Items in a PriceTable. In Oracle, I can do this with ranking functions:
SELECT ItemId, MAX(Price) KEEP(DENSE_RANK FIRST ORDER BY FromDate DESC) AS Price
FROM PriceTable
GROUP BY ItemId
Is it possible to do the same with ranking functions in MSSQL?
Thanks
Martin
Hi Pinal,
I’m trying to convert these two rows to access sql.
dense_rank() over(partition by field1 order by field2) as name1,
row_number() over(partition by fld1, (dense_rank() over(partition by fldnm1 order by fldnm2)) order by fld2) as name2
can you help me?
thanks in advance
hi sir,
this is hanu, i have ony doubt that is
how to store dense_rank() function results in sqlserver database.
give me solution with example, thank u sir
ex:
sid same marks rank
1 hanu 68 3
2 bhanu 78 2
3 ramu 80 1
Display the different types of credit caeds used for purchasing products?
@Yogitha bali g.s
Question : Display the different types of credit caeds used for purchasing products?
Answer :
VISA
MASTER
AMERICAN EXPRESS
Blah Blah….
How I am I suppose to know this information.
Please post your complete question, with complete details.
~ IM.
hi ,
Can u let me know how 2 ignore ranking if the column value is null.for e.g
colX Rank
52 7
108 1
80 2
80 2
null 8
in the above case i want the ranking for 5th row to be null instead of 8.Need ur guidance
As a quick reply (not sure if this is best). Add a WHERE clause ignoring NULLs, then UNION ALL to add the record that are NULL.
Or, CASE colx WHEN NULL THEN NULL ELSE RANK….
Hi Pinal
Can U explain briefly about NTILE Ranking function with an realtime example
NTile Ranking function groups the total records in a specified no of groups. Here it mentioned as NTile(4), thus split total records to 4 groups(1,2 3 & 4). For more information goto
http://msdn.microsoft.com/en-us/library/ms175126.aspx
Hi Pinal,
I want to get some amount of rows from a table, e.g. 10 rows, from 15th row in the table ordered by some column.
how to use row_numberIO or any other functions
Another Question:
Can Identity Column is attached to select list in any select Query susch as
select Identity(1,1) as row_number,col1,col2 from table1
Your posts are very easy to understand.
Thanks for the great article. It’s very useful to us.
Hello – please help – I want to rank records in the table and store this rank in the same table for further processing – what is the simplest way to do it? I’m working in SQL 2005……
[...] Returns the rank of rows within the partition of a result set, without any gaps in the ranking. (Read more here [...]
Hi,
I have strange requirement .
I need to show ranks for all the rows except few in between.
I tried that in using where id’s not in (’1′,’2′)
In this case I’m getting only the rows where id’s not in (1,2)
Is there any way to get those rows as well in the same query ?
Or Do I need to Follow a different approach?
I have the same problem as anand has ..
does any one has any solution ??
Query
I have a list of runners and their race times and ages. Write a
Query to display the runners sorted by finish time with a column showing
their finish position (ranking) within their age group.
The age groups are
0 – 15 years
16 – 29 years
30+ years
Name Time Age
——————–
Steve 12 33
Tim 34 28
Mark 22 37
Tom 21 30
Cliff 13 33
Vini 17 28
Matt 10 28
Ben 9 29
Brandon 15 14
Output should be like below
Name Time Age Ranking
===== ==== === =======
Ben 9 29 1
Matt 10 28 2
Steve 12 33 1
Cliff 13 33 2
Brandon 15 14 1
Vini 17 28 3
Tom 21 30 3
Mark 22 37 4
Tim 34 28 4
select Ranking,BloggerName from
(select BloggerName,Row_Number() OVER (Partition by Topic Order by Total) ‘Ranking’
FROM BlogCount where BloggerName=’Ritesh’) t where Ranking not in(2)
how to use date function in sql server…m new in sql server…….. plz help me
Have a look at SQL Server helo file BOL and search for functions like DAY, MONTH, YEAR, DATEADD,DATEDIFF, etc. Also make sure to read the examples posted at the end of the following blog
http://beyondrelational.com/blogs/madhivanan/archive/2010/06/03/understanding-datetime-column-part-iii.aspx
Hi Pinal, thanks for the article but it’s confusing and the results are not explained well, it’s taken from the 70-433 book examples and in my idea it’s not the best example ever.
Recently I came across this article and it’s the greatest in explaining the aggregates SO FAR!!
http://www.4guysfromrolla.com/webtech/010406-1.shtml
hi Jason Yousef
thanx x-at post i am serchic 4 :)
Thank you pinal. It helped me in a nice way.
Hi Sir,
Can we use Row_Number in multiple join queries?
Actually i am trying to get that but getting error
Hi Prajakta,
Can you please describe what exactly you are trying to achieve?
hi sir,
if i want to display the name of persons from a table who booked to travel in same day
Hi Sir,
From long time i want to know that in which scenarios we use rank() and dense_rank functions.
tnx
DECLARE @S varchar(max),
@Split char(1),
@X xml
SELECT @S = ’1,2,3′,
@Split = ‘,’
SELECT @X = CONVERT(xml,” + REPLACE(@S,@Split,”) + ”)
insert tablename
SELECT [Value] = T.c.value(‘.’,'varchar(20)’)
FROM @X.nodes(‘/root/s’) T(c)
my current answer is
a
1
2
3
I expect this answer ,what can i do for this
a b c
1 2 3
I am executing same stored procedure on two servers. One execution is faster whereas other is slow compared to other. what are the possible reasons for this?
Think about thesere do?
1 What does the procedu
2 How many data are involved?
I am having values in a column as 1,1,1,2,2,2,3,3,3. I want to write a query which results me like 1,2,3,1,2,3,1,2,3. How can we do this?
What is the datatype of the column?
Hi Pinal,
The blog is very interesting ..And I am not able to understand about NTILE() function..Can you please one more example with explanation..
Hi Pinal,
I have a string @value = ~46|tblAccount|OwnerID|2012.07.20|7|5~279|tblAccount|Name|2012.07.20|Lyrics|Future|~279|tblAccount|OwnerID|2012.07.20|4|5
Now i have to split the result into a new columns and new rows with the respective delimiters , We need the result as :
1) where ever ‘~’ is present as a new record
2) wherever ‘|’ is present into a new column
the above result should look like
Column1 Column2 Column3 Column4 Column5 Column6
46 tblAccount OwnerID 2012.07.19 7 5
279 tblAccount Name 2012.07.19 Lyrics Future
279 tblAccount OwnerID 2012.07.19 4 5
Dear pinaldave ,
I have 5 MNC offer.ur articles helped me a lot. thanks for the great work :).
Thanks you, this articles helped me a lot.
Great example
u r a genius man….. :)
Hi Pinal,
Have one question – When ROW_NUMBER(), RANK() & DENSE_RANK() will produce the same result?
ROW_NUMBER(), RANK() & DENSE_RANK() will produce the same result when the column on which we are doing the sorting (order by) have only unique values.
Example: In above example if Postal Code column fro,m Adress table have only unique entries then ROW_NUMBER(), RANK() & DENSE_RANK() will produce the same result
I am not clear with the NTILE function , can you explain it a bit more?
very helpful article :-)
Hi Shilpa,
ROW_NUMBER(), RANK() & DENSE_RANK() will produce the same result when the column on which we are doing the sorting (order by) have only unique value.
Example: In above example if Postal Code column fro,m Adress table have only unique entries then ROW_NUMBER(), RANK() & DENSE_RANK() will produce the same result.
how to write a function for generate uniqe employee id
ex,, emp-kk-0001,emp-kk-0002,,Function?
have two columns one with identity property and another column which is derived from it
create table test
(
id int identity(1,1) not null,
employee_id as ‘emp-kk-’+right(’0000′+cast(id as varchar(10)),4)
)
What’s great is that urban clothing is already really cheap to buy. Knowing the growing amount of customers who have transferred from actual store shopping to online shop buying, you can make a presumption as to how your online business should react to the trend.. ralph lauren http://www.likeralphlaurenpolo.co.uk%2
How can I make two column out of one. That is, Range_From and Range_To for the column SalesPersonID.
For example if I’v 500 Sales Persons working on different days. Data are:
Working of Sale Persons
———————–
SalePersonID SaleDate
001 2012-01-01
002 2012-01-01
003 2012-01-01
004 2012-01-01
.
.
150 2012-01-01
151 2012-01-02
152 2012-01-02
.
.
160 2012-01-02
161 2012-01-03
164 2012-01-04
165 2012-01-04
.
.
500 2012-01-04
I want to have result like:
Working of Sale Persons
———————–
Range_From Range_To SaleDate
001 150 2012-01-01
151 160 2012-01-02
161 161 2012-01-03
162 500 2012-01-04