-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_questions_and_solution.sql
More file actions
194 lines (142 loc) · 5.37 KB
/
2_questions_and_solution.sql
File metadata and controls
194 lines (142 loc) · 5.37 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
-- LINK : https://en.wikibooks.org/wiki/SQL_Exercises/Employee_management
-- 2.1 Select the last name of all employees.
select LastName from Employees;
-- 2.2 Select the last name of all employees, without duplicates.
select distinct LastName from employees;
-- 2.3 Select all the data of employees whose last name is "Smith".
select * from employees where lastname = 'Smith';
-- 2.4 Select all the data of employees whose last name is "Smith" or "Doe".
select * from Employees where lastname in ('Smith', 'Doe');
select * from Employees where lastname = 'Smith' or lastname = 'Doe';
-- 2.5 Select all the data of employees that work in department 14.
select * from Employees where department = 14;
-- 2.6 Select all the data of employees that work in department 37 or department 77.
select * from employees where department = 37 or department = 77;
select * from employees where department in (37, 77);
-- 2.7 Select all the data of employees whose last name begins with an "S".
select * from employees where LastName like 'S%';
-- 2.8 Select the sum of all the departments' budgets.
select sum(budget) from Departments;
select Name, sum(Budget) from Departments group by Name;
-- 2.9 Select the number of employees in each department (you only need to show the department code and the number of employees).
select Department, count(*) from employees group by department;
SELECT Department, COUNT(*)
FROM Employees
GROUP BY Department;
-- 2.10 Select all the data of employees, including each employee's department's data.
select a.*, b.* from employees a join departments b on a.department = b.code;
SELECT SSN, E.Name AS Name_E, LastName, D.Name AS Name_D, Department, Code, Budget
FROM Employees E INNER JOIN Departments D
ON E.Department = D.Code;
-- 2.11 Select the name and last name of each employee, along with the name and budget of the employee's department.
select a.name, a.lastname, b.name Department_name, b.Budget
from employees a join departments b
on a.department = b.code;
/* Without labels */
SELECT Employees.Name, LastName, Departments.Name AS DepartmentsName, Budget
FROM Employees INNER JOIN Departments
ON Employees.Department = Departments.Code;
/* With labels */
SELECT E.Name, LastName, D.Name AS DepartmentsName, Budget
FROM Employees E INNER JOIN Departments D
ON E.Department = D.Code;
-- 2.12 Select the name and last name of employees working for departments with a budget greater than $60,000.
select name, lastname
from employees
where department in (
select code from departments where Budget>60000
);
/* Without subquery */
SELECT Employees.Name, LastName
FROM Employees INNER JOIN Departments
ON Employees.Department = Departments.Code
AND Departments.Budget > 60000;
/* With subquery */
SELECT Name, LastName FROM Employees
WHERE Department IN
(SELECT Code FROM Departments WHERE Budget > 60000);
-- 2.13 Select the departments with a budget larger than the average budget of all the departments.
select *
from departments
where budget > (
select avg(budget) from departments
);
SELECT *
FROM Departments
WHERE Budget >
(
SELECT AVG(Budget)
FROM Departments
);
-- 2.14
-- Select the names of departments with more than two employees.
select b.name
from departments b
where code in (
select department
from employees
group by department
having count(*)>2
);
/* With subquery */
SELECT Name FROM Departments
WHERE Code IN
(
SELECT Department
FROM Employees
GROUP BY Department
HAVING COUNT(*) > 2
);
/* With UNION. This assumes that no two departments have
the same name */
SELECT Departments.Name
FROM Employees INNER JOIN Departments
ON Department = Code
GROUP BY Departments.Name
HAVING COUNT(*) > 2;
-- 2.15
-- Very Important
-- Select the name and last name of employees working for departments with second lowest budget.
select name, lastname
from employees
where department =(
select temp.code
from (select * from departments order by budget limit 2) temp
order by temp.budget desc limit 1
);
/* With subquery */
SELECT e.Name, e.LastName
FROM Employees e
WHERE e.Department = (
SELECT sub.Code
FROM (SELECT * FROM Departments d ORDER BY d.budget LIMIT 2) sub
ORDER BY budget DESC LIMIT 1);
-- 2.16
-- Add a new department called "Quality Assurance", with a budget of $40,000 and departmental code 11.
-- Add an employee called "Mary Moore" in that department, with SSN 847-21-9811.
insert into departments values(11, 'Quality Assurance', 40000);
insert into employees values(847219811, 'Mary', 'Moore', 11);
-- 2.17
-- Reduce the budget of all departments by 10%.
update departments
set budget = 0.9 * budget;
-- 2.18
-- Reassign all employees from the Research department (code 77) to the IT department (code 14).
update employees
set department = 14
where department = 77;
-- 2.19
-- Delete from the table all employees in the IT department (code 14).
delete from employees
where department = 14;
-- 2.20
-- Delete from the table all employees who work in departments with a budget greater than or equal to $60,000.
delete from employees
where department in (
select code
from departments
where budget>=60000
);
-- 2.21
-- Delete from the table all employees.
delete from employees;