-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmployeesAnalysis
328 lines (190 loc) · 6.75 KB
/
EmployeesAnalysis
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
-- Querying in MySQL with a database on employee information to gain insights for a company.
Select *
FROM Departments;
--- Select all people from the “employees” table whose first name is “Elvis”.
SELECT *
FROM employees
WHERE first_name = 'Elvis';
--- Retrieve a list with all female employees whose first name is Kellie.
SELECT *
FROM employees
WHERE first_name = 'Kellie'
AND gender = 'F';
--- Retrieve a list with all employees whose first name is either Kellie or Aruna.
SELECT *
FROM employees
WHERE first_name = 'Kellie' OR first_name = 'Aruna';
--- Retrieve a list with all female employees whose first name is either Kellie or Aruna.
SELECT *
FROM employees
WHERE gender = 'F' and (first_name = 'Kellie' OR first_name = 'Aruna');
--- Select all individuals from the “employees” table, whose first name is either “Denis”, or “Elvis”.
SELECT *
FROM employees
WHERE first_name IN ('Dennis', 'Elvis');
--- Extract all records from the ‘employees’ table, aside from those with employees named John, Mark, or Jacob.
SELECT *
FROM employees
WHERE first_name NOT IN ('John', 'Mark', 'Jacobl');
--- Select the data about all individuals, whose first name starts with “Mark”;
SELECT *
FROM employees
WHERE first_name LIKE 'Mark%';
--- Retrieve a list with all employees who have been hired in the year 2000.
SELECT *
FROM employees
WHERE hire_date LIKE '%2000%';
--- Retrieve a list with all employees whose employee number is written with 5 characters, and starts with “1000”.
SELECT *
FROM employees
WHERE emp_no LIKE '1000_';
--- Select all the information from the “salaries” table regarding contracts from 66,000 to 70,000 dollars per year.
SELECT *
FROM salaries
WHERE salary BETWEEN '66,000' AND '70,000';
--- Retrieve a list with all individuals whose employee number is not between ‘10004’ and ‘10012’.
SELECT *
FROM employees
WHERE emp_no NOT BETWEEN '10004' AND '10012';
--- Select the names of all departments with numbers between ‘d003’ and ‘d006’.
SELECT dept_name
FROM departments
WHERE dept_no BETWEEN 'd003' AND 'd006';
--- Select the names of all departments whose department number value is not null.
SELECT dept_name
FROM departments
WHERE dept_no IS NOT NULL;
--- Retrieve a list with data about all female employees who were hired in the year 2000 or after.
SELECT *
FROM employees
WHERE gender = 'f'
AND hire_date >= '2000-01-01';
--- Extract a list with all employees’ salaries higher than $150,000 per annum.
SELECT *
FROM salaries
WHERE salary > 150000;
--- Obtain a list with all different “hire dates” from the “employees” table.
SELECT DISTINCT hire_date
FROM employees;
--- How many annual contracts with a value higher than or equal to $100,000 have been registered in the salaries table?
SELECT COUNT(salary)
FROM salaries
WHERE salary >= 100000;
--- How many managers do we have in the “employees” database?
SELECT COUNT(*)
FROM dept_manager;
--- Select all data from the “employees” table, ordering it by “hire date” in descending order.
SELECT *
FROM employees
ORDER BY hire_date DESC;
--- Write a query that obtains two columns. The first column must contain annual salaries higher than 80,000 dollars.
--- The second column, renamed to “emps_with_same_salary”, must show the number of employees contracted to that salary.
--- Lastly, sort the output by the first column.
SELECT salary, COUNT(emp_no) as emps_with_same_salary
FROM salaries
WHERE salary > 80000
GROUP BY salary
ORDER BY salary
--- Select all employees whose average salary is higher than $120,000 per annum.
SELECT emp_no, AVG(salary)
FROM salaries
GROUP BY emp_no
HAVING AVG(salary) > 120000
ORDER BY emp_no;
--- Select the employee numbers of all individuals who have signed more than 1 contract after the 1st of January 2000.
SELECT emp_no
FROM dept_emp
WHERE from_date > '2000-01-01'
GROUP BY emp_no
HAVING COUNT(from_date) > 1
ORDER BY emp_no;
--- Select the first 100 rows from the ‘dept_emp’ table.
SELECT *
FROM dept_emp
LIMIT 100;
--- Insert information about employee number 999903. State that he/she is a “Senior Engineer”, who has started working in this position on October 1st, 1997.
INSERT INTO titles
(emp_no,
title,
from_date) VALUES
( 999903,
'Senior Engineer',
1997-10-01);
--- Insert information about the individual with employee number 999903 into the “dept_emp” table. He/She is working for department number 5, and has started work on October 1st, 1997; her/his contract is for an indefinite period of time.
SELECT *
FROM dept_emp
ORDER BY emp_no DESC
LIMIT 10;
INSERT INTO dept_emp
( emp_no,
dept_no,
from_date,
to_date) VALUES
( 999903,
'd005',
'1997-10-01',
'9999-01-10');
--- Create a new department called “Business Analysis”. Register it under number ‘d010’.
INSERT INTO departments
VALUES
( 'd010',
'Business Analysis');
--- Change the “Business Analysis” department name to “Data Analysis”.
UPDATE departments
SET dept_name = 'Data Analysis'
WHERE dept_no = 'd010';
--- Remove the department number 10 record from the “departments” table.
DELETE FROM departments
WHERE dept_no = 'd010';
--- How many departments are there in the “employees” database?
SELECT COUNT(DISTINCT dept_no)
FROM dept_emp;
--- What is the total amount of money spent on salaries for all contracts starting after the 1st of January 1997?
SELECT SUM(salary)
FROM salaries
WHERE from_date > '1997-01-01';
--- Which is the lowest & highest employee number in the database?
SELECT MIN(emp_no)
FROM employees;
SELECT MAX(emp_no)
FROM employees;
--- What is the average annual salary paid to employees who started after the 1st of January 1997?
SELECT ROUND(AVG(salary),2) as 'Average_Salary'
FROM salaries
WHERE from_date > '1997-01-01';
--- Creating table dup
DROP TABLE IF EXISTS departments_dup;
CREATE TABLE departments_dup
(dept_no CHAR(4) NULL,
dept_name VARCHAR(40) NULL);
INSERT INTO departments_dup
(dept_no,
dept_name)
SELECT *
FROM departments;
INSERT INTO departments_dup
(dept_name)
VALUES
('Public Relations');
DELETE FROM departments_dup
WHERE dept_no = 'd002';
INSERT INTO departments_dup(dept_no) VALUES ('d010'), ('d011');
SELECT *
FROM departments_dup;
DROP TABLE IF EXISTS dept_manager_dup;
CREATE TABLE dept_manager_dup (
emp_no int(11) NOT NULL,
dept_no char(4) NULL,
from_date date NOT NULL,
to_date date NULL
);
INSERT INTO dept_manager_dup
select * from dept_manager;
INSERT INTO dept_manager_dup (emp_no, from_date)
VALUES (999904, '2017-01-01'),
(999905, '2017-01-01'),
(999906, '2017-01-01'),
(999907, '2017-01-01');
DELETE FROM dept_manager_dup
WHERE
dept_no = 'd001';