-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql_queries.sql
249 lines (239 loc) · 6.43 KB
/
sql_queries.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
# Q1) Which industries, sectors or state have highest mean salaries?
## industry
SELECT
A.industry, ROUND(AVG(avg_salary), 0) AS avg_salary
FROM
(SELECT
jp.jobpostingID,
jp.title,
jp.companyID,
i.industry,
se.sector,
jp.lower_salary,
jp.upper_salary,
ROUND(((jp.lower_salary + jp.upper_salary) / 2),
0) AS avg_salary,
st.state
FROM
jobpostings jp
JOIN
states st ON st.stateID = jp.stateID
JOIN
companies c ON c.companyID = jp.companyID
JOIN
industries i ON i.industryID = c.industryID
JOIN
sectors se ON se.sectorID = i.sectorID
WHERE
jp.lower_salary > 0) A
GROUP BY A.industry
ORDER BY avg_salary DESC;
## Sector
SELECT
A.sector, ROUND(AVG(avg_salary), 0) AS avg_salary
FROM
(SELECT
jp.jobpostingID,
jp.title,
jp.companyID,
i.industry,
se.sector,
jp.lower_salary,
jp.upper_salary,
ROUND(((jp.lower_salary + jp.upper_salary) / 2), 0) AS avg_salary,
st.state
FROM
jobpostings jp
JOIN states st ON st.stateID = jp.stateID
JOIN companies c ON c.companyID = jp.companyID
JOIN industries i ON i.industryID = c.industryID
JOIN sectors se ON se.sectorID = i.sectorID
WHERE
jp.lower_salary > 0) A
GROUP BY A.sector
ORDER BY avg_salary DESC;
## State
SELECT
A.state, ROUND(AVG(avg_salary), 0) AS avg_salary
FROM
(SELECT
jp.jobpostingID,
jp.title,
jp.companyID,
i.industry,
jp.lower_salary,
jp.upper_salary,
ROUND(((jp.lower_salary + jp.upper_salary) / 2), 0) AS avg_salary,
st.state
FROM
jobpostings jp
JOIN states st ON st.stateID = jp.stateID
JOIN companies c ON c.companyID = jp.companyID
JOIN industries i ON i.industryID = c.industryID
WHERE
jp.lower_salary > 0) A
GROUP BY A.state
ORDER BY avg_salary DESC;
# Q2) Which companies offer highest salaries?
SELECT
jp.companyID,
c.companyName,
ROUND(AVG((jp.lower_salary + jp.upper_salary) / 2),
0) AS avg_salary
FROM
jobpostings jp
JOIN
companies c ON c.companyID = jp.companyID
WHERE
jp.lower_salary > 0
GROUP BY jp.companyID
ORDER BY avg_salary DESC;
# Q3) Show the size, revenue, rating, and the average salary for each company
SELECT
c.companyName,
si.size,
r.revenue,
c.rating,
ROUND(AVG((jp.lower_salary + jp.upper_salary) / 2),
0) AS avg_salary
FROM
companies c
JOIN
sizes si ON si.sizeID = c.sizeID
JOIN
revenues r ON r.revenueID = c.revenueID
JOIN
jobpostings jp ON jp.companyID = c.companyID
WHERE
(jp.lower_salary > 0)
AND c.rating IS NOT NULL
GROUP BY c.companyID;
# Q4) Which software skills are most sought after?
SELECT
sk.skill, COUNT(sk.skillID) AS '# of postings'
FROM
jobpostings_skills js
JOIN
skills sk ON sk.skillID = js.skillID
GROUP BY sk.skill
ORDER BY COUNT(sk.skillID) DESC;
# Q5) Show the number of skills required and avg_salary for each job posting
SELECT
jp.jobpostingID,
jp.title,
jp.companyID,
ROUND((jp.lower_salary + jp.upper_salary) / 2,
0) AS avg_salary,
COUNT(js.skillID) AS Number_of_Skills
FROM
jobpostings jp
JOIN
jobpostings_skills js ON js.jobpostingID = jp.jobpostingID
JOIN
companies c ON c.companyID = jp.companyID
WHERE
jp.lower_salary > 0
AND js.skillID NOT IN (5 , 11)
GROUP BY jp.jobpostingID;
# Q6) Create Dummies for each skill
SELECT
jp.jobpostingID,
jp.title,
ROUND((jp.lower_salary + jp.upper_salary) / 2, 0) AS avg_salary,
SUM(CASE
WHEN js.skillID = 1 THEN 1
ELSE 0
END) AS R,
SUM(CASE
WHEN js.skillID = 2 THEN 1
ELSE 0
END) AS Agile,
SUM(CASE
WHEN js.skillID = 3 THEN 1
ELSE 0
END) AS AWS,
SUM(CASE
WHEN js.skillID = 4 THEN 1
ELSE 0
END) AS Azure,
SUM(CASE
WHEN js.skillID = 6 THEN 1
ELSE 0
END) AS Excel,
SUM(CASE
WHEN js.skillID = 7 THEN 1
ELSE 0
END) AS Java,
SUM(CASE
WHEN js.skillID = 8 THEN 1
ELSE 0
END) AS Oracle,
SUM(CASE
WHEN js.skillID = 9 THEN 1
ELSE 0
END) AS Power_BI,
SUM(CASE
WHEN js.skillID = 10 THEN 1
ELSE 0
END) AS Powerpoint,
SUM(CASE
WHEN js.skillID = 12 THEN 1
ELSE 0
END) AS Python,
SUM(CASE
WHEN js.skillID = 13 THEN 1
ELSE 0
END) AS SAP,
SUM(CASE
WHEN js.skillID = 14 THEN 1
ELSE 0
END) AS SAS,
SUM(CASE
WHEN js.skillID = 15 THEN 1
ELSE 0
END) AS 'SQL',
SUM(CASE
WHEN js.skillID = 16 THEN 1
ELSE 0
END) AS Tableau
FROM
jobpostings jp
JOIN jobpostings_skills js ON js.jobpostingID = jp.jobpostingID
JOIN skills sk ON sk.skillID = js.skillID
WHERE
jp.lower_salary > 0
GROUP BY jp.jobpostingID;
# Q7) Create a View to show the original scraped data before inserting into SQL
CREATE VIEW v_original_table AS
SELECT
jp.jobpostingID,
jp.title,
c.companyName,
s.size,
r.revenue,
i.industry,
se.sector,
jp.lower_salary,
jp.upper_salary,
jp.city,
st.state,
sk.skill
FROM
jobpostings_skills js
JOIN
jobpostings jp ON jp.jobpostingID = js.jobpostingID
JOIN
skills sk ON sk.skillID = js.skillID
JOIN
companies c ON c.companyID = jp.companyID
JOIN
states st ON st.stateID = jp.stateID
JOIN
sizes s ON s.sizeID = c.sizeID
JOIN
revenues r ON r.revenueID = c.revenueID
JOIN
industries i ON i.industryID = c.industryID
JOIN
sectors se ON se.sectorID = i.sectorID
ORDER BY jp.jobpostingID , sk.skillID;