-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmployee_Database_Challenge.sql
225 lines (187 loc) · 5.48 KB
/
Employee_Database_Challenge.sql
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
--CHALLENGE QUERIES
----------------------------------------------------------------------
--DELIVERABLE 1--
DROP TABLE retirement_titles;
--Make a retiree info table with name number and title info (not dept, not gender),
--and only restricted birth dates: 1952-1955, order by employee number, export...
SELECT e.emp_no,
e.first_name,
e.last_name,
j.title,
j.from_date,
j.to_date
INTO retirement_titles
FROM employees as e
INNER JOIN titles as j
ON (e.emp_no = j.emp_no)
WHERE (e.birth_date BETWEEN '1952-01-01' AND '1955-12-31')
ORDER BY e.emp_no;
SELECT * FROM retirement_titles;
SELECT COUNT(emp_no) FROM retirement_titles;
--COPY CHALLENGE 'CODE' HERE:
SELECT DISTINCT ON (______) _____,
______,
______,
______
INTO nameyourtable
FROM _______
WHERE _______
ORDER BY _____, _____ DESC;
DROP TABLE unique_titles;
--retiree info table with still employeed employees and latest title info...
SELECT DISTINCT ON (emp_no) emp_no,
first_name,
last_name,
title
INTO unique_titles
FROM retirement_titles as r
WHERE (to_date = '9999-01-01')
ORDER BY emp_no, to_date DESC;
SELECT * FROM unique_titles;
SELECT COUNT(emp_no) FROM unique_titles;
SELECT DISTINCT title from unique_titles;
--sort and count retiree titles and put into new table...
SELECT COUNT(*), title
INTO retiring_titles
FROM unique_titles
GROUP BY title
ORDER BY count DESC;
SELECT * FROM retiring_titles;
----------------------------------------------------------------------
--DELIVERABLE 2--
DROP TABLE mentorship_eligibility;
SELECT DISTINCT ON (e.emp_no) e.emp_no,
e.first_name,
e.last_name,
e.birth_date,
de.from_date,
de.to_date,
j.title
INTO mentorship_eligibility
FROM employees as e
INNER JOIN titles as j
ON (e.emp_no = j.emp_no)
INNER JOIN dept_emp as de
ON (e.emp_no = de.emp_no)
WHERE (e.birth_date BETWEEN '1965-01-01' AND '1965-12-31')
AND (de.to_date = '9999-01-01')
ORDER BY e.emp_no, de.to_date DESC;
SELECT * FROM mentorship_eligibility;
SELECT COUNT(emp_no) FROM mentorship_eligibility;
----------------------------------------------------------------------
--DELIVERABLE 3--
--mentor titles
SELECT DISTINCT title from mentorship_eligibility;
--sort and count retiree titles and put into new table...
SELECT COUNT(*), title
INTO mentor_titles
FROM mentorship_eligibility
GROUP BY title
ORDER BY count DESC;
SELECT * FROM mentor_titles;
DROP TABLE mentor_titles;
--INCREASE MENTORSHIP PROGRAM ACCEPTED YEARS???
DROP TABLE mentorship_eligibility;
SELECT DISTINCT ON (e.emp_no) e.emp_no,
e.first_name,
e.last_name,
e.birth_date,
de.from_date,
de.to_date,
j.title
INTO mentorship_eligibility
FROM employees as e
INNER JOIN titles as j
ON (e.emp_no = j.emp_no)
INNER JOIN dept_emp as de
ON (e.emp_no = de.emp_no)
WHERE (e.birth_date BETWEEN '1964-01-01' AND '1969-12-31')
AND (de.to_date = '9999-01-01')
ORDER BY e.emp_no, de.to_date DESC;
SELECT * FROM mentorship_eligibility;
SELECT COUNT(emp_no) FROM mentorship_eligibility;
--FOCUS ON DEPARTMENTS INSTEAD: RETIREE and MENTORSHIP DEPARTMENTS
DROP TABLE department_eligibility;
--sort and count retiree departments and put into new table...
SELECT DISTINCT ON (e.emp_no) e.emp_no,
e.first_name,
e.last_name,
e.birth_date,
de.from_date,
de.to_date,
de.dept_no,
d.dept_name,
j.title
INTO department_eligibility
FROM employees as e
INNER JOIN dept_emp as de
ON (e.emp_no = de.emp_no)
INNER JOIN departments as d
ON (de.dept_no = d.dept_no)
INNER JOIN titles as j
ON (e.emp_no = j.emp_no)
WHERE (e.birth_date BETWEEN '1964-01-01' AND '1969-12-31')
AND (de.to_date = '9999-01-01')
ORDER BY e.emp_no, de.to_date DESC;
SELECT * FROM department_eligibility;
SELECT COUNT(emp_no) FROM department_eligibility;
DROP TABLE department_titles;
SELECT DISTINCT (dept_name, title) from department_eligibility;
--sort and count department mentees and put into new table...
SELECT COUNT(*), dept_name
INTO department_titles
FROM department_eligibility
GROUP BY dept_name
ORDER BY count DESC;
SELECT * FROM department_titles;
------------------------------------------------------------
DROP TABLE retirement_department_needs;
--sort and count retiree departments and put into new table...
SELECT DISTINCT ON (e.emp_no) e.emp_no,
e.first_name,
e.last_name,
e.birth_date,
de.from_date,
de.to_date,
de.dept_no,
d.dept_name,
j.title
INTO retirement_department_needs
FROM employees as e
INNER JOIN dept_emp as de
ON (e.emp_no = de.emp_no)
INNER JOIN departments as d
ON (de.dept_no = d.dept_no)
INNER JOIN titles as j
ON (e.emp_no = j.emp_no)
WHERE (e.birth_date BETWEEN '1952-01-01' AND '1955-12-31')
AND (de.to_date = '9999-01-01')
ORDER BY e.emp_no, de.to_date DESC;
SELECT * FROM retirement_department_needs;
SELECT COUNT(emp_no) FROM retirement_department_needs;
DROP TABLE retirement_needs;
SELECT DISTINCT (dept_name, title) from retirement_department_needs;
--sort and count department mentees and put into new table...
SELECT COUNT(*), dept_name
INTO retirement_needs
FROM retirement_department_needs
GROUP BY dept_name
ORDER BY count DESC;
SELECT * FROM retirement_needs;
--my preferred retirement count query...
DROP TABLE retirement_count;
SELECT DISTINCT ON (e.emp_no) e.emp_no,
e.first_name,
e.last_name,
j.title,
j.from_date,
j.to_date
INTO retirement_count
FROM employees as e
INNER JOIN titles as j
ON (e.emp_no = j.emp_no)
WHERE (e.birth_date BETWEEN '1952-01-01' AND '1955-12-31')
AND (to_date = '9999-01-01')
ORDER BY e.emp_no, j.from_date DESC;
SELECT * FROM retirement_count;
SELECT COUNT(emp_no) FROM retirement_count;