SQL SERVER – 2005 – Sample Example of RANKING Functions – ROW_NUMBER, RANK, DENSE_RANK, NTILE

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;
SELECT c.FirstName, c.LastName
ORDER BY a.PostalCode) AS 'Row Number'
ORDER BY a.PostalCode) AS 'Rank'
ORDER BY a.PostalCode) AS 'Dense Rank'
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
SalesYTD <> 0;

SQL SERVER - 2005 - Sample Example of RANKING Functions - ROW_NUMBER, RANK, DENSE_RANK, NTILE ranking

Most of the content of this article is taken from BOL.

Reference : Pinal Dave (https://blog.sqlauthority.com), ROW_NUMBER, RANK, DENSE_RANK, NTILE


Ranking Functions, SQL Function, SQL Scripts
Previous Post
SQL SERVER – 2005 – Connection Property of SQL Server Management Studio SSMS
Next Post
SQL SERVER – UDF vs. Stored Procedures and Having vs. WHERE

Related Posts

66 Comments. Leave new

  • 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

  • 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!!

  • Thank you pinal. It helped me in a nice way.

  • Prajakta Chaudhari
    April 14, 2012 5:22 pm

    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.

  • saeedtech13
    May 3, 2012 6:03 pm


  • 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

    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?

  • 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?

  • 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 :-)


Leave a Reply