0% found this document useful (0 votes)
36 views

SQL Server Function Algoritm

The ROW_NUMBER() function assigns a sequential integer to each row in a result set or partition. It takes an optional PARTITION BY clause to divide rows into partitions and a mandatory ORDER BY clause to define the logical order of rows. ROW_NUMBER() is useful for pagination by returning specific page ranges based on the assigned row numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

SQL Server Function Algoritm

The ROW_NUMBER() function assigns a sequential integer to each row in a result set or partition. It takes an optional PARTITION BY clause to divide rows into partitions and a mandatory ORDER BY clause to define the logical order of rows. ROW_NUMBER() is useful for pagination by returning specific page ranges based on the assigned row numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL Server ROW_NUMBER Function

Summary: in this tutorial, you will learn how to use the SQL Server ROW_NUMBER() function to assign a
sequential integer to each row of a result set.

Introduction to SQL Server ROW_NUMBER() function


The ROW_NUMBER() is a window function that assigns a sequential integer to each row within a partition of a
result set. The row number starts with one for the first row in each partition.

The following shows the syntax of the ROW_NUMBER() function:

1 ROW_NUMBER() OVER (
2 [PARTITION BY partition_expression, ... ]
3 ORDER BY sort_expression [ASC | DESC], ...
4 )

Let’s examine the syntax of the ROW_NUMBER() function in detail.

PARTITION BY

The PARTITION BY clause divides the result set into partitions. The ROW_NUMBER() function is applied to each
partition separately and reinitialized the row number for each partition.

The PARTITION BY clause is optional. If you skip it, the ROW_NUMBER() function will treat the whole result set as
a single partition.

ORDER BY

The ORDER BY clause defines the logical order of the rows within each partition of the result set. The ORDER
BY clause is mandatory because the ROW_NUMBER() function is order sensitive.

SQL Server ROW_NUMBER() examples


We’ll use the sales.customers table from the sample database to demonstrate the ROW_NUMBER() function.
Using SQL Server ROW_NUMBER() function over a result set example
The following statement uses the ROW_NUMBER() to assign each customer row a sequential number:

1 SELECT
2 ROW_NUMBER() OVER (
3 ORDER BY first_name
4 ) row_num,
5 first_name,
6 last_name,
7 city
8 FROM
9 sales.customers;

Here is the partial output:

In this example, we skipped the PARTITION BY clause, therefore, the ROW_NUMBER() treated the whole result
set as a single partition.
Using SQL Server ROW_NUMBER() over partitions example
The following example uses the ROW_NUMBER() function to assign a sequential integer to each customer. It
resets the number when the city changes:

1 SELECT
2 first_name,
3 last_name,
4 city,
5 ROW_NUMBER() OVER (
6 PARTITION BY city
7 ORDER BY first_name
8 ) row_num
9 FROM
10 sales.customers
11 ORDER BY
12 city;

The following picture shows the partial output:

In this example, we used the PARTITION BY clause to divide the customers into partitions by city. The row
number was reinitialized when the city changed.

Using SQL Server ROW_NUMBER() for pagination


The ROW_NUMBER() function is useful for pagination in applications. For example, you can display a list of
customers by page, where each page has 10 rows.

The following example uses the ROW_NUMBER() to return customers from row 11 to 20, which is the second
page:

1 WITH cte_customers AS (
2 SELECT
3 ROW_NUMBER() OVER(
4 ORDER BY
5 first_name,
6 last_name
7 ) row_num,
8 customer_id,
9 first_name,
10 last_name
11 FROM
12 sales.customers
13 ) SELECT
14 customer_id,
15 first_name,
16 last_name
17 FROM
18 cte_customers
19 WHERE
20 row_num > 20 AND
21 row_num <= 30;

The output is as follows:

In this example:

First, the CTE used the ROW_NUMBER() function to assign every row in the result set a sequential integer.
Second, the outer query returned the rows of the second page, which have the row number between 11 to
20.

In this tutorial, you have learned how to use the SQL Server ROW_NUMBER() function to assign a sequential
integer to each row within a partition of a query.

You might also like