![]() |
The row number function is one of the window functions used for assigning the row number to the rows in each of the partitions in the order in which they were sorted in the partition using the PARTITION clause, PARTITION only the ORDER clause can be used inside the OVER clause in such case the whole table will be considered as one partition. But the ORDER BY clause is mandatory for using the ROW_NUMBER() function since it arranges the rows in the partitions in that logical order and later ROW_NUMBER() function can assign the row number. In each partition, the row number starts from 1. Syntax:
Components of ROW_NUMBER() function
Usage of the ROW_NUMBER() FunctionStep 1: Create the database Geeksforgeeks by using the following SQL query: Query CREATE DATABASE Geeksforgeeks
Use the GFG Database. Query USE GeeksforGeeks
Step 3: Create a table with the students of different sections with their total marks out of 500. Query CREATE TABLE studentsSectionWise( Step 4: Insert the rows into the table : Query INSERT INTO studentsSectionWise Step 5: Check the table SELECT * FROM studentsSectionWise
Output: ![]() studentsSectionWise table Using ORDER_BY Clause with ROW_NUMBER() FunctionUsing simply the ORDER BY clause with ascending or descending considers the whole table as one partition only. Let’s check the rankNumber of the students using the ROW_NUMBER() function with the ORDER BY clause including all sections: SELECT * , ROW_NUMBER() OVER ( ORDER BY studentMarks DESC) AS rankNumber Explanation: In the query we have used ORDER BY clause with the studentsMarks which is ordered in DESC order. Output: ![]() Output Explanation: In the result we can see that whole table is considered as a single partition and the rankNumber number is in the increasing order with the studentsMarks in the descending order. Using PARTITION_BY with ROW_NUMBER() FunctionUsing simply the PARTITION BY clause divdies and the table into different partitions based on the column name and then ORDER BY clause with ascending or descending considers the whole table as one partition only. And then the ROW_NUMBER() functions each unique number starting from 1 in each partition. Let’s check the ranking section wise using the ROW_NUMBER() function with the PARTITION BY and ORDER BY clauses. SELECT * , ROW_NUMBER() OVER (PARTITION BY sectionName ORDER BY studentMarks DESC) AS rankNumber Explanation: In the query we have used PARTITION BY clause to partition the table on the sectionName wise and ORDER BY clause with the studentsMarks which is ordered in DESC. Output: ![]() Output Explanation: In the result we can see we have partitions divided by the sectionName wise and then in each partition the students are ordered on the basis of studentmarks in the DESC order , ROW_NUMBER() function assigned the row number starting from in each partition with it getting incremented. Understanding ROW_NUMBER() Without PARTITION BYSQL Server’s ROW_NUMBER() function is a flexible tool that allows you to provide each row in a result set a unique row number. It is equally effective when used without the PARTITION BY clause, even though it is frequently used in conjunction with it for grouping and ranking within partitions. The possibilities and uses of ROW_NUMBER() without the PARTITION BY clause will be discussed in this article. Syntax:
Let’s check the ranking section wise using the ROW_NUMBER() function without the PARTITION BY and ORDER BY clauses. SELECT * , ROW_NUMBER() OVER (ORDER BY studentMarks DESC) AS rankNumber Output: ![]() Output Using ROW_NUMBER() to Get the Subset of RowsThe ROW_NUMBER() function can be used to get the subset of rows from the table using the CTE which can be useful in the case of pagination of the data. Let’s check the top 2 rankers of every class using the CTE(Common Table Expression) with ROW_NUMBER() function : WITH topTwoRankers AS Explanation: Here in the query we have used the CTE expression to get the temporary table from the query used above and the we have used the outer SELCT query to get the students whose rankNumber is less than or equal to 2. Output: ![]() Output Explanation: In the result we can see we have the top 2 rankers from each section which are the subset of the table obtained from the query shown in the previous example. Advantages of
|
Reffered: https://www.geeksforgeeks.org
Databases |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |