forked from swcarpentry/sql-novice-survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-select.html
525 lines (525 loc) · 16.1 KB
/
01-select.html
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: Databases and SQL</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container card">
<div class="banner">
<a href="http://software-carpentry.org" title="Software Carpentry">
<img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
</a>
</div>
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<a href="index.html"><h1 class="title">Databases and SQL</h1></a>
<h2 class="subtitle">Selecting Data</h2>
<section class="objectives panel panel-warning">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
</div>
<div class="panel-body">
<ul>
<li>Explain the difference between a table, a record, and a field.</li>
<li>Explain the difference between a database and a database manager.</li>
<li>Write a query to select all values for specific fields from a single table.</li>
</ul>
</div>
</section>
<p>A <a href="reference.html#relational-database">relational database</a> is a way to store and manipulate information. Databases are arranged as <a href="reference.html#table">tables</a>. Each table has columns (also known as <a href="reference.html#field">fields</a>) that describe the data, and rows (also known as <a href="reference.html#record">records</a>) which contain the data.</p>
<p>When we are using a spreadsheet, we put formulas into cells to calculate new values based on old ones. When we are using a database, we send commands (usually called <a href="reference.html#query">queries</a>) to a <a href="reference.html#database-manager">database manager</a>: a program that manipulates the database for us. The database manager does whatever lookups and calculations the query specifies, returning the results in a tabular form that we can then use as a starting point for further queries.</p>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pushpin"></span>Changing database managers</h2>
</div>
<div class="panel-body">
<p>Every database manager — Oracle, IBM DB2, PostgreSQL, MySQL, Microsoft Access, and SQLite — stores data in a different way, so a database created with one cannot be used directly by another. However, every database manager can import and export data in a variety of formats, like .csv, so it <em>is</em> possible to move information from one to another.</p>
</div>
</aside>
<p>Queries are written in a language called <a href="reference.html#sql">SQL</a>, which stands for “Structured Query Language”. SQL provides hundreds of different ways to analyze and recombine data. We will only look at a handful of queries, but that handful accounts for most of what scientists do.</p>
<p>The tables below show the database we will use in our examples:</p>
<blockquote>
<p><strong>Person</strong>: people who took readings.</p>
<table>
<thead>
<tr class="header">
<th align="left">ident</th>
<th align="left">personal</th>
<th align="left">family</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">dyer</td>
<td align="left">William</td>
<td align="left">Dyer</td>
</tr>
<tr class="even">
<td align="left">pb</td>
<td align="left">Frank</td>
<td align="left">Pabodie</td>
</tr>
<tr class="odd">
<td align="left">lake</td>
<td align="left">Anderson</td>
<td align="left">Lake</td>
</tr>
<tr class="even">
<td align="left">roe</td>
<td align="left">Valentina</td>
<td align="left">Roerich</td>
</tr>
<tr class="odd">
<td align="left">danforth</td>
<td align="left">Frank</td>
<td align="left">Danforth</td>
</tr>
</tbody>
</table>
</blockquote>
<blockquote>
<p><strong>Site</strong>: locations where readings were taken.</p>
<table>
<thead>
<tr class="header">
<th align="left">name</th>
<th align="left">lat</th>
<th align="left">long</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">DR-1</td>
<td align="left">-49.85</td>
<td align="left">-128.57</td>
</tr>
<tr class="even">
<td align="left">DR-3</td>
<td align="left">-47.15</td>
<td align="left">-126.72</td>
</tr>
<tr class="odd">
<td align="left">MSK-4</td>
<td align="left">-48.87</td>
<td align="left">-123.4</td>
</tr>
</tbody>
</table>
</blockquote>
<blockquote>
<p><strong>Visited</strong>: when readings were taken at specific sites.</p>
<table>
<thead>
<tr class="header">
<th align="left">ident</th>
<th align="left">site</th>
<th align="left">dated</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">619</td>
<td align="left">DR-1</td>
<td align="left">1927-02-08</td>
</tr>
<tr class="even">
<td align="left">622</td>
<td align="left">DR-1</td>
<td align="left">1927-02-10</td>
</tr>
<tr class="odd">
<td align="left">734</td>
<td align="left">DR-3</td>
<td align="left">1930-01-07</td>
</tr>
<tr class="even">
<td align="left">735</td>
<td align="left">DR-3</td>
<td align="left">1930-01-12</td>
</tr>
<tr class="odd">
<td align="left">751</td>
<td align="left">DR-3</td>
<td align="left">1930-02-26</td>
</tr>
<tr class="even">
<td align="left">752</td>
<td align="left">DR-3</td>
<td align="left">-null-</td>
</tr>
<tr class="odd">
<td align="left">837</td>
<td align="left">MSK-4</td>
<td align="left">1932-01-14</td>
</tr>
<tr class="even">
<td align="left">844</td>
<td align="left">DR-1</td>
<td align="left">1932-03-22</td>
</tr>
</tbody>
</table>
</blockquote>
<blockquote>
<p><strong>Survey</strong>: the actual readings.</p>
<table>
<thead>
<tr class="header">
<th align="left">taken</th>
<th align="left">person</th>
<th align="left">quant</th>
<th align="left">reading</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">619</td>
<td align="left">dyer</td>
<td align="left">rad</td>
<td align="left">9.82</td>
</tr>
<tr class="even">
<td align="left">619</td>
<td align="left">dyer</td>
<td align="left">sal</td>
<td align="left">0.13</td>
</tr>
<tr class="odd">
<td align="left">622</td>
<td align="left">dyer</td>
<td align="left">rad</td>
<td align="left">7.8</td>
</tr>
<tr class="even">
<td align="left">622</td>
<td align="left">dyer</td>
<td align="left">sal</td>
<td align="left">0.09</td>
</tr>
<tr class="odd">
<td align="left">734</td>
<td align="left">pb</td>
<td align="left">rad</td>
<td align="left">8.41</td>
</tr>
<tr class="even">
<td align="left">734</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.05</td>
</tr>
<tr class="odd">
<td align="left">734</td>
<td align="left">pb</td>
<td align="left">temp</td>
<td align="left">-21.5</td>
</tr>
<tr class="even">
<td align="left">735</td>
<td align="left">pb</td>
<td align="left">rad</td>
<td align="left">7.22</td>
</tr>
<tr class="odd">
<td align="left">735</td>
<td align="left">-null-</td>
<td align="left">sal</td>
<td align="left">0.06</td>
</tr>
<tr class="even">
<td align="left">735</td>
<td align="left">-null-</td>
<td align="left">temp</td>
<td align="left">-26.0</td>
</tr>
<tr class="odd">
<td align="left">751</td>
<td align="left">pb</td>
<td align="left">rad</td>
<td align="left">4.35</td>
</tr>
<tr class="even">
<td align="left">751</td>
<td align="left">pb</td>
<td align="left">temp</td>
<td align="left">-18.5</td>
</tr>
<tr class="odd">
<td align="left">751</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.1</td>
</tr>
<tr class="even">
<td align="left">752</td>
<td align="left">lake</td>
<td align="left">rad</td>
<td align="left">2.19</td>
</tr>
<tr class="odd">
<td align="left">752</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.09</td>
</tr>
<tr class="even">
<td align="left">752</td>
<td align="left">lake</td>
<td align="left">temp</td>
<td align="left">-16.0</td>
</tr>
<tr class="odd">
<td align="left">752</td>
<td align="left">roe</td>
<td align="left">sal</td>
<td align="left">41.6</td>
</tr>
<tr class="even">
<td align="left">837</td>
<td align="left">lake</td>
<td align="left">rad</td>
<td align="left">1.46</td>
</tr>
<tr class="odd">
<td align="left">837</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.21</td>
</tr>
<tr class="even">
<td align="left">837</td>
<td align="left">roe</td>
<td align="left">sal</td>
<td align="left">22.5</td>
</tr>
<tr class="odd">
<td align="left">844</td>
<td align="left">roe</td>
<td align="left">rad</td>
<td align="left">11.25</td>
</tr>
</tbody>
</table>
</blockquote>
<p>Notice that three entries — one in the <code>Visited</code> table, and two in the <code>Survey</code> table — don’t contain any actual data, but instead have a special <code>-null-</code> entry: we’ll return to these missing values <a href="05-null.html">later</a>. For now, let’s write an SQL query that displays scientists’ names. We do this using the SQL command <code>SELECT</code>, giving it the names of the columns we want and the table we want them from. Our query and its output look like this:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> family, personal <span class="kw">FROM</span> Person;</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">family</th>
<th align="left">personal</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Dyer</td>
<td align="left">William</td>
</tr>
<tr class="even">
<td align="left">Pabodie</td>
<td align="left">Frank</td>
</tr>
<tr class="odd">
<td align="left">Lake</td>
<td align="left">Anderson</td>
</tr>
<tr class="even">
<td align="left">Roerich</td>
<td align="left">Valentina</td>
</tr>
<tr class="odd">
<td align="left">Danforth</td>
<td align="left">Frank</td>
</tr>
</tbody>
</table>
<p>The semicolon at the end of the query tells the database manager that the query is complete and ready to run. We have written our commands and column names in lower case, and the table name in Title Case, but we don’t have to: as the example below shows, SQL is <a href="reference.html#case-insensitive">case insensitive</a>.</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SeLeCt</span> FaMiLy, PeRsOnAl <span class="kw">FrOm</span> PeRsOn;</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">family</th>
<th align="left">personal</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Dyer</td>
<td align="left">William</td>
</tr>
<tr class="even">
<td align="left">Pabodie</td>
<td align="left">Frank</td>
</tr>
<tr class="odd">
<td align="left">Lake</td>
<td align="left">Anderson</td>
</tr>
<tr class="even">
<td align="left">Roerich</td>
<td align="left">Valentina</td>
</tr>
<tr class="odd">
<td align="left">Danforth</td>
<td align="left">Frank</td>
</tr>
</tbody>
</table>
<p>You can use SQL’s case insensitivity to your advantage. For instance, some people choose to write SQL keywords (such as <code>SELECT</code> and <code>FROM</code>) in capital letters and <strong>field</strong> and <strong>table</strong> names in lower case. This can make it easier to locate parts of an SQL statement. For instance, you can scan the statement, quickly locate the prominent <code>FROM</code> keyword and know the table name follows. Whatever casing convention you choose, please be consistent: complex queries are hard enough to read without the extra cognitive load of random capitalization. One convention is to use UPPER CASE for SQL statements, to distinguish them from tables and column names. This is the convention that we will use for this lesson.</p>
<p>Going back to our query, it’s important to understand that the rows and columns in a database table aren’t actually stored in any particular order. They will always be <em>displayed</em> in some order, but we can control that in various ways. For example, we could swap the columns in the output by writing our query as:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> personal, family <span class="kw">FROM</span> Person;</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">personal</th>
<th align="left">family</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">William</td>
<td align="left">Dyer</td>
</tr>
<tr class="even">
<td align="left">Frank</td>
<td align="left">Pabodie</td>
</tr>
<tr class="odd">
<td align="left">Anderson</td>
<td align="left">Lake</td>
</tr>
<tr class="even">
<td align="left">Valentina</td>
<td align="left">Roerich</td>
</tr>
<tr class="odd">
<td align="left">Frank</td>
<td align="left">Danforth</td>
</tr>
</tbody>
</table>
<p>or even repeat columns:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> ident, ident, ident <span class="kw">FROM</span> Person;</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">ident</th>
<th align="left">ident</th>
<th align="left">ident</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">dyer</td>
<td align="left">dyer</td>
<td align="left">dyer</td>
</tr>
<tr class="even">
<td align="left">pb</td>
<td align="left">pb</td>
<td align="left">pb</td>
</tr>
<tr class="odd">
<td align="left">lake</td>
<td align="left">lake</td>
<td align="left">lake</td>
</tr>
<tr class="even">
<td align="left">roe</td>
<td align="left">roe</td>
<td align="left">roe</td>
</tr>
<tr class="odd">
<td align="left">danforth</td>
<td align="left">danforth</td>
<td align="left">danforth</td>
</tr>
</tbody>
</table>
<p>As a shortcut, we can select all of the columns in a table using <code>*</code>:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> * <span class="kw">FROM</span> Person;</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">ident</th>
<th align="left">personal</th>
<th align="left">family</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">dyer</td>
<td align="left">William</td>
<td align="left">Dyer</td>
</tr>
<tr class="even">
<td align="left">pb</td>
<td align="left">Frank</td>
<td align="left">Pabodie</td>
</tr>
<tr class="odd">
<td align="left">lake</td>
<td align="left">Anderson</td>
<td align="left">Lake</td>
</tr>
<tr class="even">
<td align="left">roe</td>
<td align="left">Valentina</td>
<td align="left">Roerich</td>
</tr>
<tr class="odd">
<td align="left">danforth</td>
<td align="left">Frank</td>
<td align="left">Danforth</td>
</tr>
</tbody>
</table>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pencil"></span>Selecting Site Names</h2>
</div>
<div class="panel-body">
<p>Write a query that selects only site names from the <code>Site</code> table.</p>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pencil"></span>Query Style</h2>
</div>
<div class="panel-body">
<p>Many people format queries as:</p>
<pre><code>SELECT personal, family FROM person;</code></pre>
<p>or as:</p>
<pre><code>select Personal, Family from PERSON;</code></pre>
<p>What style do you find easiest to read, and why?</p>
</div>
</section>
</div>
</div>
</article>
<div class="footer">
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="https://github.com/swcarpentry/sql-novice-survey">Source</a>
<a class="label swc-blue-bg" href="mailto:admin@software-carpentry.org">Contact</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
</body>
</html>