forked from swcarpentry/sql-novice-survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-sort-dup.html
490 lines (490 loc) · 13.3 KB
/
02-sort-dup.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
<!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>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<h1 class="title">Databases and SQL</h1>
<h2 class="subtitle">Sorting and Removing Duplicates</h2>
<div id="learning-objectives" 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>Write queries that display results in a particular order.</li>
<li>Write queries that eliminate duplicate values from data.</li>
</ul>
</div>
</div>
<p>Data is often redundant, so queries often return redundant information. For example, if we select the quantitites that have been measured from the <code>survey</code> table, we get this:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> quant <span class="kw">FROM</span> Survey;</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">quant</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">rad</td>
</tr>
<tr class="even">
<td align="left">sal</td>
</tr>
<tr class="odd">
<td align="left">rad</td>
</tr>
<tr class="even">
<td align="left">sal</td>
</tr>
<tr class="odd">
<td align="left">rad</td>
</tr>
<tr class="even">
<td align="left">sal</td>
</tr>
<tr class="odd">
<td align="left">temp</td>
</tr>
<tr class="even">
<td align="left">rad</td>
</tr>
<tr class="odd">
<td align="left">sal</td>
</tr>
<tr class="even">
<td align="left">temp</td>
</tr>
<tr class="odd">
<td align="left">rad</td>
</tr>
<tr class="even">
<td align="left">temp</td>
</tr>
<tr class="odd">
<td align="left">sal</td>
</tr>
<tr class="even">
<td align="left">rad</td>
</tr>
<tr class="odd">
<td align="left">sal</td>
</tr>
<tr class="even">
<td align="left">temp</td>
</tr>
<tr class="odd">
<td align="left">sal</td>
</tr>
<tr class="even">
<td align="left">rad</td>
</tr>
<tr class="odd">
<td align="left">sal</td>
</tr>
<tr class="even">
<td align="left">sal</td>
</tr>
<tr class="odd">
<td align="left">rad</td>
</tr>
</tbody>
</table>
<p>We can eliminate the redundant output to make the result more readable by adding the <code>DISTINCT</code> keyword to our query:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> <span class="kw">DISTINCT</span> quant <span class="kw">FROM</span> Survey;</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">quant</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">rad</td>
</tr>
<tr class="even">
<td align="left">sal</td>
</tr>
<tr class="odd">
<td align="left">temp</td>
</tr>
</tbody>
</table>
<p>If we select more than one column --- for example, both the survey site ID and the quantity measured --- then the distinct pairs of values are returned:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> <span class="kw">DISTINCT</span> taken, quant <span class="kw">FROM</span> Survey;</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">taken</th>
<th align="left">quant</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">619</td>
<td align="left">rad</td>
</tr>
<tr class="even">
<td align="left">619</td>
<td align="left">sal</td>
</tr>
<tr class="odd">
<td align="left">622</td>
<td align="left">rad</td>
</tr>
<tr class="even">
<td align="left">622</td>
<td align="left">sal</td>
</tr>
<tr class="odd">
<td align="left">734</td>
<td align="left">rad</td>
</tr>
<tr class="even">
<td align="left">734</td>
<td align="left">sal</td>
</tr>
<tr class="odd">
<td align="left">734</td>
<td align="left">temp</td>
</tr>
<tr class="even">
<td align="left">735</td>
<td align="left">rad</td>
</tr>
<tr class="odd">
<td align="left">735</td>
<td align="left">sal</td>
</tr>
<tr class="even">
<td align="left">735</td>
<td align="left">temp</td>
</tr>
<tr class="odd">
<td align="left">751</td>
<td align="left">rad</td>
</tr>
<tr class="even">
<td align="left">751</td>
<td align="left">temp</td>
</tr>
<tr class="odd">
<td align="left">751</td>
<td align="left">sal</td>
</tr>
<tr class="even">
<td align="left">752</td>
<td align="left">rad</td>
</tr>
<tr class="odd">
<td align="left">752</td>
<td align="left">sal</td>
</tr>
<tr class="even">
<td align="left">752</td>
<td align="left">temp</td>
</tr>
<tr class="odd">
<td align="left">837</td>
<td align="left">rad</td>
</tr>
<tr class="even">
<td align="left">837</td>
<td align="left">sal</td>
</tr>
<tr class="odd">
<td align="left">844</td>
<td align="left">rad</td>
</tr>
</tbody>
</table>
<p>Notice in both cases that duplicates are removed even if they didn't appear to be adjacent in the database. Again, it's important to remember that rows aren't actually ordered: they're just displayed that way.</p>
<p>As we mentioned earlier, database records are not stored in any particular order. This means that query results aren't necessarily sorted, and even if they are, we often want to sort them in a different way, e.g., by the name of the project instead of by the name of the scientist. We can do this in SQL by adding an <code>ORDER BY</code> clause to our query:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> * <span class="kw">FROM</span> Person <span class="kw">ORDER</span> <span class="kw">BY</span> ident;</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">danfort</td>
<td align="left">Frank</td>
<td align="left">Danforth</td>
</tr>
<tr class="even">
<td align="left">dyer</td>
<td align="left">William</td>
<td align="left">Dyer</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">pb</td>
<td align="left">Frank</td>
<td align="left">Pabodie</td>
</tr>
<tr class="odd">
<td align="left">roe</td>
<td align="left">Valentina</td>
<td align="left">Roerich</td>
</tr>
</tbody>
</table>
<p>By default, results are sorted in ascending order (i.e., from least to greatest). We can sort in the opposite order using <code>DESC</code> (for "descending"):</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> * <span class="kw">FROM</span> person <span class="kw">ORDER</span> <span class="kw">BY</span> ident <span class="kw">DESC</span>;</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">roe</td>
<td align="left">Valentina</td>
<td align="left">Roerich</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">dyer</td>
<td align="left">William</td>
<td align="left">Dyer</td>
</tr>
<tr class="odd">
<td align="left">danfort</td>
<td align="left">Frank</td>
<td align="left">Danforth</td>
</tr>
</tbody>
</table>
<p>(And if we want to make it clear that we're sorting in ascending order, we can use <code>ASC</code> instead of <code>DESC</code>.)</p>
<p>We can also sort on several fields at once. For example, this query sorts results first in ascending order by <code>taken</code>, and then in descending order by <code>person</code> within each group of equal <code>taken</code> values:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> taken, person <span class="kw">FROM</span> Survey <span class="kw">ORDER</span> <span class="kw">BY</span> taken <span class="kw">ASC</span>, person <span class="kw">DESC</span>;</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">taken</th>
<th align="left">person</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">619</td>
<td align="left">dyer</td>
</tr>
<tr class="even">
<td align="left">619</td>
<td align="left">dyer</td>
</tr>
<tr class="odd">
<td align="left">622</td>
<td align="left">dyer</td>
</tr>
<tr class="even">
<td align="left">622</td>
<td align="left">dyer</td>
</tr>
<tr class="odd">
<td align="left">734</td>
<td align="left">pb</td>
</tr>
<tr class="even">
<td align="left">734</td>
<td align="left">pb</td>
</tr>
<tr class="odd">
<td align="left">734</td>
<td align="left">lake</td>
</tr>
<tr class="even">
<td align="left">735</td>
<td align="left">pb</td>
</tr>
<tr class="odd">
<td align="left">735</td>
<td align="left">-null-</td>
</tr>
<tr class="even">
<td align="left">735</td>
<td align="left">-null-</td>
</tr>
<tr class="odd">
<td align="left">751</td>
<td align="left">pb</td>
</tr>
<tr class="even">
<td align="left">751</td>
<td align="left">pb</td>
</tr>
<tr class="odd">
<td align="left">751</td>
<td align="left">lake</td>
</tr>
<tr class="even">
<td align="left">752</td>
<td align="left">roe</td>
</tr>
<tr class="odd">
<td align="left">752</td>
<td align="left">lake</td>
</tr>
<tr class="even">
<td align="left">752</td>
<td align="left">lake</td>
</tr>
<tr class="odd">
<td align="left">752</td>
<td align="left">lake</td>
</tr>
<tr class="even">
<td align="left">837</td>
<td align="left">roe</td>
</tr>
<tr class="odd">
<td align="left">837</td>
<td align="left">lake</td>
</tr>
<tr class="even">
<td align="left">837</td>
<td align="left">lake</td>
</tr>
<tr class="odd">
<td align="left">844</td>
<td align="left">roe</td>
</tr>
</tbody>
</table>
<p>This is easier to understand if we also remove duplicates:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> <span class="kw">DISTINCT</span> taken, person <span class="kw">FROM</span> Survey <span class="kw">ORDER</span> <span class="kw">BY</span> taken <span class="kw">ASC</span>, person <span class="kw">DESC</span>;</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">taken</th>
<th align="left">person</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">619</td>
<td align="left">dyer</td>
</tr>
<tr class="even">
<td align="left">622</td>
<td align="left">dyer</td>
</tr>
<tr class="odd">
<td align="left">734</td>
<td align="left">pb</td>
</tr>
<tr class="even">
<td align="left">734</td>
<td align="left">lake</td>
</tr>
<tr class="odd">
<td align="left">735</td>
<td align="left">pb</td>
</tr>
<tr class="even">
<td align="left">735</td>
<td align="left">-null-</td>
</tr>
<tr class="odd">
<td align="left">751</td>
<td align="left">pb</td>
</tr>
<tr class="even">
<td align="left">751</td>
<td align="left">lake</td>
</tr>
<tr class="odd">
<td align="left">752</td>
<td align="left">roe</td>
</tr>
<tr class="even">
<td align="left">752</td>
<td align="left">lake</td>
</tr>
<tr class="odd">
<td align="left">837</td>
<td align="left">roe</td>
</tr>
<tr class="even">
<td align="left">837</td>
<td align="left">lake</td>
</tr>
<tr class="odd">
<td align="left">844</td>
<td align="left">roe</td>
</tr>
</tbody>
</table>
<div id="finding-distinct-dates" class="challenge panel panel-success">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pencil"></span>Finding Distinct Dates</h2>
</div>
<div class="panel-body">
<p>Write a query that selects distinct dates from the <code>Visited</code> table.</p>
</div>
</div>
<div id="displaying-full-names" class="challenge panel panel-success">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pencil"></span>Displaying Full Names</h2>
</div>
<div class="panel-body">
<p>Write a query that displays the full names of the scientists in the <code>Person</code> table, ordered by family name.</p>
</div>
</div>
</div>
</div>
<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>