-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow_functions.sql
More file actions
237 lines (185 loc) · 5.81 KB
/
Copy pathwindow_functions.sql
File metadata and controls
237 lines (185 loc) · 5.81 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
-- =========================================================
-- FILE: window_functions.sql
-- PROJECT: TechSphere Solutions Database System
-- AUTHOR: Benard Onyango Omoga
-- DATABASE: MySQL
-- DESCRIPTION:
-- Demonstrates Window Functions in MySQL using TechSphere schema.
-- Includes AVG, SUM, ROW_NUMBER, RANK, and DENSE_RANK.
-- =========================================================
-- =========================================================
-- USE DATABASE
-- =========================================================
USE techsphere_solutions;
-- =========================================================
-- SECTION 1: INTRODUCTION TO WINDOW FUNCTIONS
-- PURPOSE:
-- Window functions perform calculations across a set of rows
-- without collapsing results like GROUP BY.
-- =========================================================
-- View employee dataset
SELECT *
FROM employees;
-- =========================================================
-- SECTION 2: GROUP BY VS WINDOW FUNCTION
-- PURPOSE:
-- Compare aggregation approaches
-- =========================================================
-- GROUP BY (collapses rows)
SELECT
d.department_name,
ROUND(AVG(jr.base_salary), 2) AS avg_salary
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
JOIN employee_positions ep
ON e.employee_id = ep.employee_id
JOIN job_roles jr
ON ep.role_id = jr.role_id
GROUP BY d.department_name;
-- WINDOW FUNCTION (keeps all rows)
SELECT
e.employee_id,
e.first_name,
d.department_name,
jr.base_salary,
AVG(jr.base_salary) OVER() AS company_avg_salary
FROM employees e
JOIN employee_positions ep
ON e.employee_id = ep.employee_id
JOIN job_roles jr
ON ep.role_id = jr.role_id
JOIN departments d
ON e.department_id = d.department_id;
-- WHAT IT DOES:
-- Shows each employee alongside global average salary
-- Unlike GROUP BY, no rows are removed
-- =========================================================
-- SECTION 3: PARTITION BY (GROUPED WINDOW CALCULATION)
-- PURPOSE:
-- Perform calculations within logical groups
-- =========================================================
SELECT
e.employee_id,
e.first_name,
d.department_name,
jr.base_salary,
AVG(jr.base_salary) OVER(PARTITION BY d.department_name) AS dept_avg_salary
FROM employees e
JOIN employee_positions ep
ON e.employee_id = ep.employee_id
JOIN job_roles jr
ON ep.role_id = jr.role_id
JOIN departments d
ON e.department_id = d.department_id;
-- WHAT IT DOES:
-- Calculates department-level average salary
-- Each employee still appears individually
-- =========================================================
-- SECTION 4: RUNNING TOTAL (ORDERED WINDOW FUNCTION)
-- PURPOSE:
-- Perform cumulative calculations
-- =========================================================
SELECT
e.employee_id,
e.first_name,
d.department_name,
jr.base_salary,
SUM(jr.base_salary)
OVER(PARTITION BY d.department_name ORDER BY e.employee_id)
AS running_salary_total
FROM employees e
JOIN employee_positions ep
ON e.employee_id = ep.employee_id
JOIN job_roles jr
ON ep.role_id = jr.role_id
JOIN departments d
ON e.department_id = d.department_id;
-- WHAT IT DOES:
-- Computes cumulative salary totals per department
-- Ordered by employee ID
-- =========================================================
-- SECTION 5: ROW NUMBER
-- PURPOSE:
-- Assign sequential numbering within groups
-- =========================================================
SELECT
e.employee_id,
e.first_name,
d.department_name,
jr.base_salary,
ROW_NUMBER() OVER(
PARTITION BY d.department_name
ORDER BY jr.base_salary DESC
) AS row_num
FROM employees e
JOIN employee_positions ep
ON e.employee_id = ep.employee_id
JOIN job_roles jr
ON ep.role_id = jr.role_id
JOIN departments d
ON e.department_id = d.department_id;
-- WHAT IT DOES:
-- Assigns unique ranking per department
-- No duplicates allowed in numbering
-- =========================================================
-- SECTION 6: RANK VS DENSE_RANK
-- PURPOSE:
-- Compare ranking behaviors
-- =========================================================
SELECT
e.employee_id,
e.first_name,
d.department_name,
jr.base_salary,
ROW_NUMBER() OVER(
PARTITION BY d.department_name
ORDER BY jr.base_salary DESC
) AS row_num,
RANK() OVER(
PARTITION BY d.department_name
ORDER BY jr.base_salary DESC
) AS salary_rank,
DENSE_RANK() OVER(
PARTITION BY d.department_name
ORDER BY jr.base_salary DESC
) AS dense_salary_rank
FROM employees e
JOIN employee_positions ep
ON e.employee_id = ep.employee_id
JOIN job_roles jr
ON ep.role_id = jr.role_id
JOIN departments d
ON e.department_id = d.department_id;
-- WHAT IT DOES:
-- ROW_NUMBER → unique sequence
-- RANK → skips numbers after ties
-- DENSE_RANK → no gaps in ranking
-- =========================================================
-- KEY CONCEPT SUMMARY
-- =========================================================
-- WINDOW FUNCTIONS:
-- Perform calculations without collapsing rows
-- PARTITION BY:
-- Splits data into logical groups
-- ORDER BY (inside OVER):
-- Defines ranking or running calculation order
-- ROW_NUMBER:
-- Unique sequential numbering
-- RANK:
-- Ranking with gaps for ties
-- DENSE_RANK:
-- Ranking without gaps
-- =========================================================
-- BUSINESS USE CASES
-- =========================================================
-- Used for:
-- - Employee performance ranking
-- - Department salary analysis
-- - Payroll analytics
-- - KPI dashboards
-- - Data segmentation and scoring models
-- =========================================================
-- END OF FILE
-- =========================================================
```