You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question.
28
-
29
-
ROW_NUMBER and RANK are similar. ROW_NUMBER numbers all rows sequentially (for example 1, 2, 3, 4, 5). RANK provides the same numeric value for ties (for example 1, 2, 2, 4, 5).
30
-
31
-
> [!NOTE]
32
-
> RANK is a temporary value calculated when the query is run. To persist numbers in a table, see [IDENTITY Property](../../t-sql/statements/create-table-transact-sql-identity-property.md) and [SEQUENCE](../../t-sql/statements/create-sequence-transact-sql.md).
RANK ( ) OVER ( [ partition_by_clause ] order_by_clause )
40
-
```
41
-
29
+
Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question.
30
+
31
+
`ROW_NUMBER` and `RANK` are similar. `ROW_NUMBER` numbers all rows sequentially (for example 1, 2, 3, 4, 5). `RANK` provides the same numeric value for ties (for example 1, 2, 2, 4, 5).
32
+
33
+
> [!NOTE]
34
+
> `RANK` is a temporary value calculated when the query is run. To persist numbers in a table, see [IDENTITY (Property)](../statements/create-table-transact-sql-identity-property.md) and [CREATE SEQUENCE](../statements/create-sequence-transact-sql.md).
RANK ( ) OVER ( [ partition_by_clause ] order_by_clause )
42
+
```
43
+
42
44
## Arguments
43
-
OVER **(**[_partition\_by\_clause_]_order\_by\_clause_**)**
44
-
*partition_by_clause* divides the result set produced by the FROM clause into partitions to which the function is applied. If not specified, the function treats all rows of the query result set as a single group. _order\_by\_clause_ determines the order of the data before the function is applied. The *order_by_clause* is required. The \<rows or range clause/> of the OVER clause cannot be specified for the RANK function. For more information, see [OVER Clause (Transact-SQL)](../../t-sql/queries/select-over-clause-transact-sql.md).
45
-
46
-
## Return Types
47
-
**bigint**
48
-
49
-
## Remarks
50
-
If two or more rows tie for a rank, each tied row receives the same rank. For example, if the two top salespeople have the same SalesYTD value, they are both ranked one. The salesperson with the next highest SalesYTD is ranked number three, because there are two rows that are ranked higher. Therefore, the RANK function does not always return consecutive integers.
51
-
52
-
The sort order that is used for the whole query determines the order in which the rows appear in a result set.
53
-
54
-
RANK is nondeterministic. For more information, see [Deterministic and Nondeterministic Functions](../../relational-databases/user-defined-functions/deterministic-and-nondeterministic-functions.md).
55
-
56
-
## Examples
57
-
58
-
### A. Ranking rows within a partition
59
-
The following example ranks the products in inventory the specified inventory locations according to their quantities. The result set is partitioned by `LocationID` and logically ordered by `Quantity`. Notice that in location 3, products 494 and 495 have the same quantity. Because they are tied, they are both ranked one.
The following example returns the top ten employees ranked by their salary. Because a PARTITION BY clause was not specified, the RANK function was applied to all rows in the result set.
#### OVER ( [*partition_by_clause*]*order_by_clause* )
47
+
48
+
The *partition_by_clause* divides the result set produced by the `FROM` clause into partitions to which the function is applied. If not specified, the function treats all rows of the query result set as a single group.
49
+
50
+
The *order_by_clause* determines the order of the data before the function is applied. The *order_by_clause* is required. The `<rows or range clause>` of the `OVER` clause can't be specified for the `RANK` function. For more information, see [SELECT - OVER clause](../queries/select-over-clause-transact-sql.md).
51
+
52
+
## Return types
53
+
54
+
**bigint**
55
+
56
+
## Remarks
57
+
58
+
If two or more rows tie for a rank, each tied row receives the same rank. For example, if the two top salespeople have the same `SalesYTD` value, they're both ranked one. The salesperson with the next highest `SalesYTD` is ranked number three, because there are two rows that are ranked higher. Therefore, the `RANK` function doesn't always return consecutive integers.
59
+
60
+
The sort order that is used for the whole query determines the order in which the rows appear in a result set.
61
+
62
+
`RANK` is nondeterministic. For more information, see [Deterministic and nondeterministic functions](../../relational-databases/user-defined-functions/deterministic-and-nondeterministic-functions.md).
The following example ranks the products in inventory the specified inventory locations according to their quantities. The result set is partitioned by `LocationID` and logically ordered by `Quantity`. In location 3, products 494 and 495 have the same quantity. Because they're tied, they're both ranked one.
71
+
72
+
```sql
73
+
USE AdventureWorks2025;
74
+
GO
75
+
76
+
SELECTi.ProductID,
77
+
p.Name,
78
+
i.LocationID,
79
+
i.Quantity,
80
+
RANK() OVER (PARTITION BY i.LocationIDORDER BYi.QuantityDESC) AS Rank
## Examples: [!INCLUDE[ssazuresynapse-md](../../includes/ssazuresynapse-md.md)] and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
125
-
126
-
### C: Ranking rows within a partition
127
-
The following example ranks the sales representatives in each sales territory according to their total sales. The rowset is partitioned by `SalesTerritoryGroup` and sorted by `SalesAmountQuota`.
128
-
129
-
```sql
130
-
-- Uses AdventureWorks
131
-
132
-
SELECT LastName, SUM(SalesAmountQuota) AS TotalSales, SalesTerritoryRegion,
133
-
RANK() OVER (PARTITION BY SalesTerritoryRegion ORDER BYSUM(SalesAmountQuota) DESC ) AS RankResult
The following example returns the top 10 employees ranked by their salary. Because a `PARTITION BY` clause isn't specified, the `RANK` function is applied to all rows in the result set.
## Examples: Azure Synapse Analytics and Analytics Platform System (PDW)
143
+
144
+
### C: Ranking rows within a partition
145
+
146
+
The following example ranks the sales representatives in each sales territory according to their total sales. The rowset is partitioned by `SalesTerritoryGroup` and sorted by `SalesAmountQuota`.
147
+
148
+
```sql
149
+
-- Uses AdventureWorks
150
+
SELECTe.LastName,
151
+
st.SalesTerritoryGroup,
152
+
SUM(sq.SalesAmountQuota) AS TotalSales,
153
+
RANK() OVER (PARTITION BY st.SalesTerritoryGroupORDER BYSUM(sq.SalesAmountQuota) DESC) AS RankResult
0 commit comments