Windowing and Ranking in SQL Server – Part 2

Hello Folks,

You must have heard about this feature which was introduced earlier in SQL Server 2005. And also you had seen my previous article on Windowing. If you want to refer it again, so here’s the link;

This article post is mainly being based upon Ranking Functions.

Ranking Functions:

The windowing capability is not useful without we specify with Ranking functions. So, that’s how Ranking functions comes into the play. In fact there are some ranking functions as follows:

  • ROW_NUMBER
  • RANK
  • DENSE_RANK
  • NTILE

We will discuss each of these one by one:

ROW_NUMBER () function:

  • The ROW_NUMBER () function generates an auto-incrementing integer according to the sort order of the OVER () clause.
  • It returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.
  • The row number function simply numbers the rows in the query result, and there’s absolutely no correlation between the physical address and actual row numbers.
  • The syntax can be seen as-

ROW_NUMBER ( )     OVER ( [ <partition_by_clause> ] <order_by_clause> )

  • One of the practical usage of the ROW_NUMBER () function could be to filter by the row number values for pagination.
  • This will be much more clear to you after seeing this example:-

I have used “Students” table from “Test” database;

1_SQL_Server_Windowing_and_Ranking_Part2

Suppose from these I want to see all the Students sorted as per their Salary and then as per their S_Id, so the query will be:

USE TEST
SELECT ROW_NUMBER() OVER(ORDER BY Salary,S_Id) AS RowNumber,
FName,Class, Salary
FROM Students
ORDER BY RowNumber

The result for this will be shown as:

   

2_SQL_Server_Windowing_and_Ranking_Part2

Now, if I want to see only 4 records, i.e. , from 6th row to the 9th row, then I will surely try to filter the above table using a WHERE clause;

USE TEST
SELECT ROW_NUMBER() OVER(ORDER BY Salary,S_Id) AS RowNumber,
Fname,Class, Salary
FROM Students
WHERE ROW_NUMBER() OVER(ORDER BY Salary,S_Id)
BETWEEN 6 AND 9
ORDER BY RowNumber

Error comes! What goes wrong in the above code?

Msg 4108, Level 15, State 1, Line 5

Windowed functions can only appear in the SELECT or ORDER BY clauses. 

Now what does this means? Is it indicating that we won’t be able to use OVER () clause in the WHERE condition? If it’s yes, then how will go out to filter this table?

Well don’t’ worry, you will get an answer for every question that might be arising on your head.

If this might be the case, then you have to use a Subquery for this. This means you have to declare the Windowed functions inside the Subquery itself.

So the query for this will be:

USE TEST
SELECT RowNumber,FName,Class, Salary
FROM ( 
        SELECT ROW_NUMBER() OVER(ORDER BY Salary,S_Id) AS RowNumber, FName, Class, Salary
        FROM Students
     ) AS Q
WHERE RowNumber BETWEEN 6 AND 9
ORDER BY RowNumber

The result can be seen as:

3_SQL_Server_Windowing_and_Ranking_Part2

So this was all about ROW_NUMBER function, in the next section I would like to deal with the remaining Ranking functions (rank, dense_rank, ntile).

So be tuned!

Hope you got it understood well 🙂

And also comments on this!!

 

Regards

Piyush Bajaj

Like us on FaceBook Follow us on Twitter | Join the fastest growing SQL Server group on FaceBook

Follow me on Twitter  |  Follow me on FaceBook

   

About Piyush Bajaj

I am very passionate about SQL Server. I also did certification on MCSA – SQL Server 2012, Querying and Administering; MCTS – SQL Server 2008, Database Development; and MCTS – SQL Server 2005, Implementation & Maintenance, which helped me to get more knowledge and interest on this field.Please feel free to drop me any question online or offline, I will try to give you the best possible answer from my side.Right now I am working as a SQL Server developer in TCS. I have an experience of just 2.6 years, well I can only say that “If you have an interest and passion, experience might become a very small thing”.

View all posts by Piyush Bajaj →

Leave a Reply

Your email address will not be published.