-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16_Rank_Functions.sql
More file actions
192 lines (175 loc) · 5.83 KB
/
16_Rank_Functions.sql
File metadata and controls
192 lines (175 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**************************************************************
* SQL Server 2022 Rank Functions Tutorial
* Description: This script demonstrates the use of ranking
* functions (RANK, DENSE_RANK, NTILE, ROW_NUMBER)
* in Transact-SQL. It covers:
* - Basic ranking functions.
* - Ranking functions with PARTITION BY.
* - Advanced queries using Common Table Expressions (CTEs)
* for filtering top-N results within partitions.
**************************************************************/
-------------------------------------------------
-- Region: 0. Initialization
-------------------------------------------------
/*
Ensure you are using the target database.
*/
USE TestDB;
GO
-------------------------------------------------
-- Region: 1. Creating the Employees Table and Inserting Sample Data
-------------------------------------------------
/*
1.1 Drop the Employees table if it already exists.
*/
IF OBJECT_ID(N'dbo.Employees', N'U') IS NOT NULL
DROP TABLE dbo.Employees;
GO
/*
1.2 Create the Employees table with sample columns.
*/
CREATE TABLE dbo.Employees
(
EmployeeID INT PRIMARY KEY,
EmployeeName NVARCHAR(100),
Department NVARCHAR(100),
Salary DECIMAL(10, 2)
);
GO
/*
1.3 Insert sample employee data.
*/
INSERT INTO dbo.Employees (EmployeeID, EmployeeName, Department, Salary)
VALUES
(1, 'Alice', 'HR', 60000.00),
(2, 'Bob', 'IT', 80000.00),
(3, 'Charlie', 'HR', 70000.00),
(4, 'David', 'IT', 90000.00),
(5, 'Eve', 'Finance', 75000.00);
GO
-------------------------------------------------
-- Region: 2. Basic Ranking Functions (Without PARTITION BY)
-------------------------------------------------
/*
2.1 RANK() - Assigns a rank to each row ordered by Salary in descending order.
Ties receive the same rank, with gaps in ranking.
*/
SELECT EmployeeID, EmployeeName, Department, Salary,
RANK() OVER (ORDER BY Salary DESC) AS Rank
FROM dbo.Employees;
GO
/*
2.2 DENSE_RANK() - Similar to RANK() but without gaps in ranking.
*/
SELECT EmployeeID, EmployeeName, Department, Salary,
DENSE_RANK() OVER (ORDER BY Salary DESC) AS DenseRank
FROM dbo.Employees;
GO
/*
2.3 NTILE() - Distributes rows into a specified number of groups.
In this example, employees are divided into 3 groups based on salary.
*/
SELECT EmployeeID, EmployeeName, Department, Salary,
NTILE(3) OVER (ORDER BY Salary DESC) AS NTile
FROM dbo.Employees;
GO
/*
2.4 ROW_NUMBER() - Assigns a unique sequential number to each row.
*/
SELECT EmployeeID, EmployeeName, Department, Salary,
ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RowNum
FROM dbo.Employees;
GO
-------------------------------------------------
-- Region: 3. Ranking Functions with PARTITION BY
-------------------------------------------------
/*
3.1 RANK() with PARTITION BY Department - Ranks employees within each department.
*/
SELECT EmployeeID, EmployeeName, Department, Salary,
RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS Rank
FROM dbo.Employees;
GO
/*
3.2 DENSE_RANK() with PARTITION BY Department - Dense ranking within each department.
*/
SELECT EmployeeID, EmployeeName, Department, Salary,
DENSE_RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS DenseRank
FROM dbo.Employees;
GO
/*
3.3 NTILE() with PARTITION BY Department - Divides employees in each department into 2 groups based on salary.
*/
SELECT EmployeeID, EmployeeName, Department, Salary,
NTILE(2) OVER (PARTITION BY Department ORDER BY Salary DESC) AS NTile
FROM dbo.Employees;
GO
/*
3.4 ROW_NUMBER() with PARTITION BY Department - Assigns a unique sequential number within each department.
*/
SELECT EmployeeID, EmployeeName, Department, Salary,
ROW_NUMBER() OVER (PARTITION BY Department ORDER BY Salary DESC) AS RowNum
FROM dbo.Employees;
GO
-------------------------------------------------
-- Region: 4. Advanced Ranking Queries with Filtering (CTEs)
-------------------------------------------------
/*
4.1 Advanced query using RANK() with PARTITION BY
Retrieve the top 2 highest salaries in each department.
*/
WITH RankedEmployees AS
(
SELECT EmployeeID, EmployeeName, Department, Salary,
RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS Rank
FROM dbo.Employees
)
SELECT EmployeeID, EmployeeName, Department, Salary, Rank
FROM RankedEmployees
WHERE Rank <= 2;
GO
/*
4.2 Advanced query using DENSE_RANK() with PARTITION BY
Retrieve the top 2 highest salaries in each department without gaps.
*/
WITH DenseRankedEmployees AS
(
SELECT EmployeeID, EmployeeName, Department, Salary,
DENSE_RANK() OVER (PARTITION BY Department ORDER BY Salary DESC) AS DenseRank
FROM dbo.Employees
)
SELECT EmployeeID, EmployeeName, Department, Salary, DenseRank
FROM DenseRankedEmployees
WHERE DenseRank <= 2;
GO
/*
4.3 Advanced query using NTILE() with PARTITION BY
Divide employees within each department into 2 groups and select one group.
*/
WITH NTileEmployees AS
(
SELECT EmployeeID, EmployeeName, Department, Salary,
NTILE(2) OVER (PARTITION BY Department ORDER BY Salary DESC) AS NTile
FROM dbo.Employees
)
SELECT EmployeeID, EmployeeName, Department, Salary, NTile
FROM NTileEmployees
WHERE NTile = 1;
GO
/*
4.4 Advanced query using ROW_NUMBER() with PARTITION BY
Retrieve the top 2 highest salaries in each department with unique row numbers.
*/
WITH RowNumberedEmployees AS
(
SELECT EmployeeID, EmployeeName, Department, Salary,
ROW_NUMBER() OVER (PARTITION BY Department ORDER BY Salary DESC) AS RowNum
FROM dbo.Employees
)
SELECT EmployeeID, EmployeeName, Department, Salary, RowNum
FROM RowNumberedEmployees
WHERE RowNum <= 2;
GO
-------------------------------------------------
-- Region: End of Script
-------------------------------------------------