-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_Optimize Employee Training Programs.sql
More file actions
194 lines (181 loc) · 5.55 KB
/
SQL_Optimize Employee Training Programs.sql
File metadata and controls
194 lines (181 loc) · 5.55 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
-- Checking for any data discrepancies in Gender Column and Position Column
SELECT Gender
from hr_data
group by Gender;
SELECT Position
from hr_data
group by Position;
-- Checking for data type of the columns
describe hr_data;
-- For the purpose of more detailed analysis, the Age and Salary columns were categorized into different groups.
CREATE TABLE hr_database AS
SELECT
EmployeeID,
Age,
CASE
WHEN Age <= 30 THEN '<= 30 years'
ELSE '> 30 years'
END AS AgeGroup,
REPLACE(REPLACE(GENDER, 'Female', 'F'), 'Male', 'M') AS Gender,
Department,
REPLACE(REPLACE(Position, 'DataScientist', 'Data Scientist'), 'Marketinganalyst', 'Marketing Analyst') AS Position,
YearsOfService,
Salary,
CASE
WHEN Salary >= 90000 THEN '90K - 100K'
WHEN Salary >= 80000 THEN '80K - 90K'
WHEN Salary >= 70000 THEN '70K - 80K'
WHEN Salary >= 60000 THEN '60K - 70K'
ELSE '50K - 60K'
END AS SalaryBucket,
PerformanceRating,
WorkHours,
Attrition,
Promotion,
TrainingHours,
SatisfactionScore,
LastPromotionDate
FROM hrdata;
-- datatype of the column Last_Promotion_Date was changed from text to varchar(50)
ALTER TABLE hr_database
MODIFY COLUMN Last_Promotion_Date VARCHAR(50);
-- data inside the column was converted
UPDATE hr_database
SET Last_Promotion_Date =
CASE
WHEN LENGTH(Last_Promotion_Date) = 10 AND Last_Promotion_Date LIKE '__-__-____' THEN STR_TO_DATE(Last_Promotion_Date, '%d-%m-%Y')
WHEN LENGTH(Last_Promotion_Date) = 10 AND Last_Promotion_Date LIKE '____-__-__' THEN STR_TO_DATE(Last_Promotion_Date, '%Y-%m-%d')
ELSE NULL
END;
-- datatype of the column Last_Promotion_Date was changed from varchar(50) to date
ALTER TABLE hr_database
MODIFY COLUMN Last_Promotion_Date DATE;
-- Analysing age group of employees who has left the organisation.
SELECT
AgeGroup,
Performance_Rating,
count(*) as employeeLeft
from hr_database
where attrition='Yes'
group by AgeGroup, Performance_Rating;
-- Comparing this with Employees who has not left the organisation
SELECT
AgeGroup,
Performance_Rating,
count(*) as employeeLeft
from hr_database
where attrition='No'
group by AgeGroup, Performance_Rating;
-- Analysing Gender of the employees with their performance rating is compared.
SELECT
Gender,
Performance_Rating,
count(*) as employeeLeft
from hr_database
where attrition='Yes'
group by Gender, Performance_Rating;
-- Comparing this with Employees who has not left the organisation
SELECT
Gender,
Performance_Rating,
count(*) as employeeLeft
from hr_database
where attrition='No'
group by Gender, Performance_Rating;
-- Comparing Year of Service with performance rating
SELECT
CASE
WHEN Years_of_Service <= 5 THEN '<= 5 years'
WHEN Years_of_Service > 5 AND Years_of_Service <= 10 THEN '6 to 10 years'
ELSE '> 10 years'
END AS Years_of_Service_bucket,
Performance_Rating,
COUNT(*) AS employeeLeft
FROM hr_database
WHERE attrition = 'Yes'
GROUP BY Years_of_Service_bucket, Performance_Rating;
-- Comparing this with Employees who has not left the organisation
SELECT
CASE
WHEN Years_of_Service <= 5 THEN '<= 5 years'
WHEN Years_of_Service > 5 AND Years_of_Service <= 10 THEN '6 to 10 years'
ELSE '> 10 years'
END AS Years_of_Service_bucket,
Performance_Rating,
COUNT(*) AS employeeLeft
FROM hr_database
WHERE attrition = 'No' GROUP BY Years_of_Service, bucket, Performance_Ratin;
-- Comparing Salary Bucket with performance rating
SELECT
SalaryBucket,
Performance_Rating,
count(*)
from hr_database
WHERE attrition = 'Yes'
GROUP BY SalaryBucket, Performance_Rating;
-- Comparing this with Employees who has not left the organisation
SELECT
SalaryBucket,
Performance_Rating,
count(*)
from hr_database
WHERE attrition = 'No'
GROUP BY SalaryBucket, Performance_Rating;
-- Comparing Training Hours with performance rating
SELECT
CASE
WHEN Training_Hours <= 15 THEN '<= 15 Hours'
WHEN Training_Hours > 15 AND Training_Hours <= 20 THEN '15 to 20 Hours'
WHEN Training_Hours > 20 AND Training_Hours <= 25 THEN '20 to 25 Hours'
ELSE '> 15 Hours'
END AS Training_Hours_bucket,
Performance_Rating,
COUNT(*) AS employeeLeft
FROM hr_database
WHERE attrition = 'Yes'
GROUP BY Training_Hours_bucket, Performance_Rating;
-- Comparing this with Employees who has not left the organisation
SELECT
CASE
WHEN Training_Hours <= 15 THEN '<= 15 Hours'
WHEN Training_Hours > 15 AND Training_Hours <= 20 THEN '15 to 20 Hours'
WHEN Training_Hours > 20 AND Training_Hours <= 25 THEN '20 to 25 Hours'
ELSE '> 15 Hours'
END AS Training_Hours_bucket,
Performance_Rating,
COUNT(*) AS employeeStayed
FROM hr_database
WHERE attrition = 'No'
GROUP BY Training_Hours_bucket, Performance_Rating;
-- Comparing Promotions with performance rating
SELECT
Promotion,
Performance_Rating,
count(*)
from hr_database
WHERE attrition = 'Yes'
GROUP BY Promotion, Performance_Rating;
-- Comparing this with Employees who has not left the organisation
SELECT
Promotion,
Performance_Rating,
count(*)
from hr_database
WHERE attrition = 'No'
GROUP BY Promotion, Performance_Rating;
-- Comparing Satisfaction Score with performance rating
SELECT
Satisfaction_Score,
Performance_Rating,
count(*)
from hr_database
WHERE attrition = 'Yes'
GROUP BY Satisfaction_Score, Performance_Rating;
-- Comparing this with Employees who has not left the organisation
SELECT
Satisfaction_Score,
Performance_Rating,
count(*)
from hr_database
WHERE attrition = 'No'
GROUP BY Satisfaction_Score, Performance_Rating;