-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1_solutions.tex
406 lines (308 loc) · 7.28 KB
/
day1_solutions.tex
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
% From https://www.sharelatex.com/learn/Typing_exams_in_LaTeX
%
% To produce pdf run:
% $ pdflatex paper.tex
%
\documentclass[10pt]{exam}
\usepackage{listings}
\lstset{basicstyle=\footnotesize\ttfamily,breaklines=true}
% These do what you would expect:
\printanswers
%\noprintanswers
\begin{document}
\title{SQL Workshop Day 1}
\author{UC Davis DSI}
\date{2018}
\maketitle
\section{Selecting and Sorting}
\begin{questions}
\question How many tables are in the database?
\begin{solution}
7. Look under `Database Structure' tab.
\end{solution}
\question Select all columns in the \texttt{company\_info} table.
\begin{solution}
\begin{lstlisting}
SELECT *
FROM company_info
;
\end{lstlisting}
\end{solution}
\question Which table has the most rows? The fewest rows?
\begin{solution}
Count rows using:
\begin{lstlisting}
SELECT COUNT(*)
FROM daily_share_prices -- replace with name of each table
;
\end{lstlisting}
\end{solution}
\question Write a query so that the columns in \texttt{financial\_ratios} table
appear in alphabetical order.
\begin{solution}
\begin{lstlisting}
SELECT beta
, dividend_yield
, market_cap_in_millions
, price_earnings_ratio
, price_to_book_ratio
, return_on_equity
, share_price
, ticker
FROM financial_ratios
;
\end{lstlisting}
\end{solution}
\question Which company has the least expensive stock in the
\texttt{financial\_ratios} table?
\begin{solution}
\begin{lstlisting}
SELECT ticker, share_price
FROM financial_ratios
ORDER BY share_price
LIMIT 1
;
\end{lstlisting}
\end{solution}
\question Which company has the most expensive stock in the
\texttt{financial\_ratios} table?
\begin{solution}
\begin{lstlisting}
SELECT ticker, share_price
FROM financial_ratios
ORDER BY share_price DESC
LIMIT 1
;
\end{lstlisting}
\end{solution}
\question How many different stocks do we have daily share prices for?
\begin{solution}
\begin{lstlisting}
SELECT COUNT(DISTINCT ticker)
FROM daily_share_prices
;
\end{lstlisting}
\end{solution}
\question How many different days do we have daily share prices for?
\begin{solution}
\begin{lstlisting}
SELECT COUNT(DISTINCT date)
FROM daily_share_prices
;
\end{lstlisting}
\end{solution}
\question Which 5 states have the highest population in 2017? lowest?
\begin{solution}
\begin{lstlisting}
SELECT *
FROM state_populations
ORDER BY popnEstimate2017 -- DESC for lowest
LIMIT 5
;
\end{lstlisting}
\end{solution}
\question How many different 4 digit industry sector codes are possible?
\begin{solution}
\begin{lstlisting}
SELECT COUNT(DISTINCT sic)
FROM sic
;
\end{lstlisting}
\end{solution}
\question How many different industry sectors appear in the company
information?
\begin{solution}
\begin{lstlisting}
SELECT COUNT(DISTINCT sic_code)
FROM company_info
;
\end{lstlisting}
\end{solution}
\question What are all the different prices that each company closed at?
\begin{solution}
\begin{lstlisting}
SELECT DISTINCT ticker, close
FROM daily_share_prices
;
\end{lstlisting}
\end{solution}
\question Which states (in this database) have companies in them?
\begin{solution}
\begin{lstlisting}
SELECT DISTINCT state
FROM company_locations
\end{lstlisting}
\end{solution}
\question How many companies are in each of the different tables? What can
you conclude?
\begin{solution}
\begin{lstlisting}
SELECT COUNT(DISTINCT ticker)
FROM company_info -- Similar for others
;
\end{lstlisting}
company\_info: 450, company\_locations: 500, company\_name: 505,
daily\_share\_prices: 505. Different numbers mean that not all companies
are in all of the tables.
\end{solution}
\end{questions}
% Part 2 ------------------------------------------------------------
\newpage
\section{Filtering and Calculations}
\begin{questions}
\question Which company is located in Oregon?
\begin{solution}
\begin{lstlisting}
SELECT *
FROM company_locations
WHERE state = 'Oregon'
;
\end{lstlisting}
\end{solution}
\question Which companies have a price-earnings ratio below 7.0?
\begin{solution}
\begin{lstlisting}
SELECT ticker, share_price
FROM financial_ratios
WHERE price_earnings_ratio < 7
;
\end{lstlisting}
\end{solution}
\question Which companies have an asset turnover ($\textrm{revenue} /
\textrm{assets}$) greater than 5.0?
\begin{solution}
\begin{lstlisting}
SELECT ticker, (revenue / asset) AS asset_turnover
FROM company_info
WHERE asset_turnover > 5.0
;
\end{lstlisting}
\end{solution}
\question Which states had a 2010 population of at least 10,000,000?
\begin{solution}
\begin{lstlisting}
SELECT *
FROM state_populations
WHERE popn2010 >= 10000000
;
\end{lstlisting}
\end{solution}
\question How many days did Apple's (AAPL) share price close above 150?
\begin{solution}
\begin{lstlisting}
SELECT COUNT(*)
FROM daily_share_prices
WHERE ticker == 'AAPL'
AND close > 150
;
\end{lstlisting}
\end{solution}
\question Which New York companies are not located in New York city?
\begin{solution}
\begin{lstlisting}
SELECT *
FROM company_locations
WHERE state = 'New York'
AND city != 'New York'
;
\end{lstlisting}
\end{solution}
\question Which California companies are located in Irvine, San Jose, or
Thousand Oaks?
\begin{solution}
\begin{lstlisting}
SELECT *
FROM company_locations
WHERE state = 'California'
AND city IN ('Irvine', 'San Jose', 'Thousand Oaks')
;
\end{lstlisting}
\end{solution}
\question Which companies have assets of more than 1 trillion dollars or are in the
software industry?
\begin{solution}
\begin{lstlisting}
SELECT *
FROM company_info
WHERE asset > 1e12
OR industry = 'Software'
;
\end{lstlisting}
\end{solution}
\question Which division has SIC codes that mention the word ``tree''?
\begin{solution}
\begin{lstlisting}
SELECT division, description
FROM sic
WHERE description LIKE '%tree %' -- The space is important!
;
\end{lstlisting}
\end{solution}
\question Which companies have web pages that do not have ``.com'' in the address?
\begin{solution}
\begin{lstlisting}
SELECT company_name, web_page
FROM company_info
WHERE web_page NOT LIKE '%.com%'
;
\end{lstlisting}
\end{solution}
\question Which companies are classified as construction (SIC codes 1500 --
1799)?
\begin{solution}
\begin{lstlisting}
SELECT *
FROM company_info
WHERE sic_code BETWEEN 1500 AND 1799
;
\end{lstlisting}
\end{solution}
\question Which companies are in Silicon Valley, which is approximately bounded
by the box (-122.996, 38.092) to (-121.479, 37.039)?
\begin{solution}
\begin{lstlisting}
SELECT *
FROM company_locations
WHERE xcoord BETWEEN -122.996 AND -121.479
AND ycoord BETWEEN 37.039 AND 38.092
;
\end{lstlisting}
\end{solution}
\question Are any rows in the daily share prices table missing the opening or
closing price?
\begin{solution}
\begin{lstlisting}
SELECT *
FROM daily_share_prices
WHERE open IS NULL
OR close IS NULL
;
\end{lstlisting}
\end{solution}
\question Are any states missing population estimates?
\begin{solution}
\begin{lstlisting}
SELECT *
FROM state_populations
WHERE popn2010 IS NULL
OR popnestimate2017 IS NULL
;
\end{lstlisting}
\end{solution}
\question How many California cities have a company whose address is on a
``street'' or ``road''?
\begin{solution}
\begin{lstlisting}
SELECT DISTINCT city
FROM company_locations
WHERE state = 'California'
AND (
-- Be careful: some streets might not end with '.'
street LIKE '%st.'
OR street LIKE '%rd.'
)
;
\end{lstlisting}
\end{solution}
\end{questions}
\end{document}