-
Notifications
You must be signed in to change notification settings - Fork 0
/
wec19b_assignment2.sql
298 lines (267 loc) · 4.4 KB
/
wec19b_assignment2.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
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
/*wec19b Assignment 2*/
/*I affirm that this is *MY* code!*/
/*Problem 1*/
/*1a*/
SELECT
*
FROM
employees
INNER JOIN
jobs
ON
employees.job_id = jobs.job_id
ORDER BY
job_id ASC;
/*1b*/
SELECT
*
FROM
employees
LEFT OUTER JOIN
jobs
ON
employees.job_id = jobs.job_id
ORDER BY
job_id ASC;
/*1c*/
/*All job titles are present and accounted for in both the jobs and employees table.
As both tables have same amount of job_ids, and at least one employee has each possible
job id, it follows that the methods from 1a and 1b will match in this respect. */
--Extra note; I wrote job_id ASC so I could also manually count job ids and make sure
--this is true.
/*1d*/
SELECT
*
FROM
jobs
LEFT OUTER JOIN
employees
ON
jobs.job_id = employees.job_id
ORDER BY
job_id ASC;
/*1e*/
SELECT
i.first_name,
i.last_name,
i.phone_number,
c.job_id,
c.job_title,
c.max_salary
FROM
employees AS i
INNER JOIN
jobs AS c
ON
i.job_id = c.job_id;
/*1f*/
SELECT
i.first_name,
i.last_name,
i.phone_number,
c.job_id,
c.job_title,
c.max_salary
FROM
employees AS i
INNER JOIN
jobs AS c
ON
i.job_id = c.job_id
WHERE
c.max_salary >= 9000
ORDER BY
c.job_id ASC
LIMIT 15;
/*Problem 2*/
/*2a*/
SELECT
c.location_id,
c.city,
c.country_id,
b.department_id,
b.department_name,
a.first_name,
a.last_name,
a.email
FROM
employees AS a
INNER JOIN
departments AS b
ON
a.department_id = b.department_id
INNER JOIN
locations AS c
ON
b.location_id = c.location_id;
/*2b*/
SELECT
d.country_name,
c.location_id,
c.city,
c.country_id,
b.department_id,
b.department_name,
a.first_name,
a.last_name,
a.email
FROM
employees AS a
INNER JOIN
departments AS b
ON
a.department_id = b.department_id
INNER JOIN
locations AS c
ON
b.location_id = c.location_id
INNER JOIN
countries AS d
ON
c.country_id = d.country_id;
-- WELL, it does have country_name as asked. It just happens to have country_id as well.
/*2c*/
SELECT
*
FROM
employees
LEFT OUTER JOIN
dependents
ON
employees.employee_id = dependents.employee_id
WHERE
dependent_id IS NULL;
/*2d*/
--First query
SELECT
*
FROM
countries
WHERE
country_name LIKE '%s%';
--Second query
SELECT
*
FROM
countries
WHERE
region_id=1 OR region_id=2
ORDER BY
region_id ASC;
--Combined QUERY
SELECT
*
FROM
countries
WHERE
country_name LIKE '%s%'
UNION ALL
SELECT
*
FROM
countries
WHERE
region_id=1 OR region_id=2
ORDER BY
region_id ASC;
/*2e*/
SELECT
*
FROM
countries
WHERE
country_name LIKE '%s%'
INTERSECT
SELECT
*
FROM
countries
WHERE
region_id=1 OR region_id=2
ORDER BY
region_id ASC;
--The table using UNION ALL stacks both tables on top of each other, resulting in duplicate records.
--The table using INTERSECT gives us a table that only gives us records present in both tables.
/*Problem 3*/
/*3a*/
SELECT
COUNT(salary) AS [under_10]
FROM
employees
WHERE
salary < 10000;
/*3b*/
SELECT
last_name,
LENGTH(last_name) AS [Last Name Length]
FROM
employees
WHERE
LENGTH(last_name) > 8
ORDER BY
[Last Name Length] ASC;
/*3c*/
SELECT
(SUBSTR(first_name, 1, 1) || '.' || last_name || ':' || email) AS [Name + Email]
FROM
employees;
--I added spaces in the next version of the query for the sake of kerning and legibility.
SELECT
(SUBSTR(first_name, 1, 1) || '. ' || last_name || ': ' || email) AS [Name + Email]
FROM
employees;
/*3d*/
SELECT
STRFTIME('%Y-%m-%d', '1977-08-16') -
STRFTIME('%Y-%m-%d', '1935-01-08') AS [Age at Death];
/*Problem 4*/
/*4a*/
SELECT
AVG(salary) AS [Average Salary],
MIN(salary) AS [Minimum Salary],
MAX(salary) AS [Maximum Salary]
FROM
employees;
/*4b*/
SELECT
department_id,
AVG(salary) AS [Average Salary]
FROM
employees
GROUP BY
department_id
ORDER BY
department_id ASC;
/*4c*/
SELECT
department_id,
ROUND(AVG(salary),1) AS [Average Salary]
FROM
employees
GROUP BY
department_id
ORDER BY
department_id ASC;
/*4d*/
SELECT
department_id,
ROUND(AVG(salary),1) AS [Average Salary]
FROM
employees
GROUP BY
department_id
HAVING
AVG(salary) > 9000
ORDER BY
department_id ASC;
/*4e*/
SELECT
department_id,
job_id,
ROUND(AVG(salary),1) AS [Average Salary]
FROM
employees
GROUP BY
department_id, job_id
ORDER BY
department_id, job_id;