# 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;

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; 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: 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: 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 🙂

Regards

Piyush Bajaj
{"ft_lang_label":"__label__en","ft_lang_prob":0.86057127,"math_prob":0.84197634,"size":3171,"snap":"2021-43-2021-49","text_gpt3_token_len":750,"char_repetition_ratio":0.12598674,"word_repetition_ratio":0.041292638,"special_character_ratio":0.23304951,"punctuation_ratio":0.11378205,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98026675,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,1,null,1,null,1,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-11-27T23:09:05Z\",\"WARC-Record-ID\":\"<urn:uuid:47bd9947-2136-4ec8-86dd-3f44b3b2bb23>\",\"Content-Length\":\"91022\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:873a408b-3ffe-4920-a97b-d24fd14b468d>\",\"WARC-Concurrent-To\":\"<urn:uuid:d0242931-bc23-4e20-ba12-597772675964>\",\"WARC-IP-Address\":\"208.113.199.116\",\"WARC-Target-URI\":\"https://sqlservergeeks.com/windowing-and-ranking-in-sql-server-part-2/\",\"WARC-Payload-Digest\":\"sha1:O4AFWFX6QLWKXB3XHPOJFOMFHSBHIWL6\",\"WARC-Block-Digest\":\"sha1:F5LEMU2Y7HZJ3M3CNC55IPV7KZDSWBFL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964358323.91_warc_CC-MAIN-20211127223710-20211128013710-00062.warc.gz\"}"}