-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect_queries_scripts.sql
More file actions
260 lines (184 loc) · 5.85 KB
/
Copy pathselect_queries_scripts.sql
File metadata and controls
260 lines (184 loc) · 5.85 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
-- =========================================================
-- FILE: case_statements.sql
-- PROJECT: TechSphere Solutions Database System
-- AUTHOR: Benard Onyango Omoga
-- DATABASE: MySQL
-- DESCRIPTION:
-- Demonstrates CASE statements in MySQL
-- using the TechSphere database structure.
-- =========================================================
-- =========================================================
-- USE DATABASE
-- =========================================================
USE techsphere_solutions;
-- =========================================================
-- SECTION 1: INTRODUCTION TO CASE STATEMENTS
-- PURPOSE:
-- CASE statements add conditional logic to SQL queries,
-- similar to IF...ELSE statements in programming.
-- =========================================================
-- View all employee records
SELECT *
FROM employees;
-- =========================================================
-- SECTION 2: SIMPLE CASE STATEMENT
-- PURPOSE:
-- Categorize employees by age group
-- =========================================================
SELECT
first_name,
last_name,
TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS age,
CASE
WHEN TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) <= 30
THEN 'Young Professional'
END AS age_category
FROM employees;
-- WHAT IT DOES:
-- Calculates employee age
-- Labels employees aged 30 or below as:
-- "Young Professional"
-- =========================================================
-- SECTION 3: MULTIPLE CONDITIONS IN CASE
-- PURPOSE:
-- Create multiple employee age categories
-- =========================================================
SELECT
first_name,
last_name,
TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS age,
CASE
WHEN TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) <= 30
THEN 'Young Professional'
WHEN TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) BETWEEN 31 AND 45
THEN 'Experienced Professional'
WHEN TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) > 45
THEN 'Senior Professional'
END AS age_category
FROM employees;
-- WHAT IT DOES:
-- Divides employees into:
-- 1. Young Professional
-- 2. Experienced Professional
-- 3. Senior Professional
-- =========================================================
-- SECTION 4: CASE WITH SALARY CALCULATIONS
-- PURPOSE:
-- Apply salary increment logic
-- =========================================================
SELECT
role_title,
base_salary,
CASE
WHEN base_salary >= 120000
THEN base_salary + (base_salary * 0.10)
WHEN base_salary < 120000
THEN base_salary + (base_salary * 0.05)
END AS revised_salary
FROM job_roles;
-- WHAT IT DOES:
-- Gives:
-- 10% increment for salaries >= 120000
-- 5% increment for salaries below 120000
-- =========================================================
-- SECTION 5: CASE WITH BONUS CALCULATIONS
-- PURPOSE:
-- Add department-specific bonuses
-- =========================================================
SELECT
e.first_name,
e.last_name,
d.department_name,
jr.base_salary,
CASE
WHEN jr.base_salary >= 120000
THEN jr.base_salary + (jr.base_salary * 0.10)
WHEN jr.base_salary < 120000
THEN jr.base_salary + (jr.base_salary * 0.05)
END AS revised_salary,
CASE
WHEN d.department_name = 'AI Research'
THEN jr.base_salary * 0.15
WHEN d.department_name = 'Cybersecurity'
THEN jr.base_salary * 0.10
ELSE 0
END AS bonus
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:
-- Salary increments
-- Department bonuses
-- BONUS RULES:
-- AI Research staff get 15%
-- Cybersecurity staff get 10%
-- Others get no bonus
-- =========================================================
-- SECTION 6: PERFORMANCE CLASSIFICATION
-- PURPOSE:
-- Simulate employee ranking system
-- =========================================================
SELECT
role_title,
base_salary,
CASE
WHEN base_salary >= 150000
THEN 'Executive Level'
WHEN base_salary BETWEEN 100000 AND 149999
THEN 'Senior Level'
WHEN base_salary BETWEEN 70000 AND 99999
THEN 'Mid Level'
ELSE 'Entry Level'
END AS salary_grade
FROM job_roles;
-- WHAT IT DOES:
-- Categorizes job roles into salary bands
-- =========================================================
-- SECTION 7: PRACTICAL BUSINESS ANALYTICS
-- PURPOSE:
-- HR and payroll decision support
-- =========================================================
SELECT
CONCAT(e.first_name, ' ', e.last_name) AS employee_name,
d.department_name,
jr.role_title,
jr.base_salary,
CASE
WHEN jr.base_salary >= 140000
THEN 'High Cost Employee'
WHEN jr.base_salary BETWEEN 90000 AND 139999
THEN 'Moderate Cost Employee'
ELSE 'Low Cost Employee'
END AS employee_cost_category
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:
-- Helps management classify employees
-- based on payroll cost levels
-- =========================================================
-- KEY CONCEPT SUMMARY
-- =========================================================
-- CASE:
-- Adds conditional logic to SQL queries
-- WHEN:
-- Specifies condition to test
-- THEN:
-- Specifies returned value if condition is TRUE
-- ELSE:
-- Optional default value
-- END:
-- Closes CASE statement
-- =========================================================
-- END OF FILE
-- =========================================================
```