forked from swcarpentry/sql-novice-survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-filter.html
489 lines (489 loc) · 17.1 KB
/
03-filter.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
<!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-responsive.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="stylesheet" type="text/css" href="css/swc-workshop-and-lesson.css" />
<link rel="stylesheet" type="text/css" href="css/lesson.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 container-full-width 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-fluid">
<div class="span10 offset1">
<h1 class="title">Databases and SQL</h1>
<h2 class="subtitle">Filtering</h2>
<div id="learning-objectives" class="objectives">
<h2>Learning Objectives</h2>
<ul>
<li>Write queries that select records that satisfy user-specified conditions.</li>
<li>Explain the order in which the clauses in a query are executed.</li>
</ul>
</div>
<p>One of the most powerful features of a database is the ability to <a href="reference.html#filter">filter</a> data, i.e., to select only those records that match certain criteria. For example, suppose we want to see when a particular site was visited. We can select these records from the <code>Visited</code> table by using a <code>WHERE</code> clause in our query:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> * <span class="kw">FROM</span> Visited <span class="kw">WHERE</span> site=<span class="ot">"DR-1"</span>;</code></pre>
<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">844</td>
<td align="left">DR-1</td>
<td align="left">1932-03-22</td>
</tr>
</tbody>
</table>
<p>The database manager executes this query in two stages. First, it checks at each row in the <code>Visited</code> table to see which ones satisfy the <code>WHERE</code>. It then uses the column names following the <code>SELECT</code> keyword to determine what columns to display.</p>
<p>This processing order means that we can filter records using <code>WHERE</code> based on values in columns that aren't then displayed:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> ident <span class="kw">FROM</span> Visited <span class="kw">WHERE</span> site=<span class="ot">"DR-1"</span>;</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">ident</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">619</td>
</tr>
<tr class="even">
<td align="left">622</td>
</tr>
<tr class="odd">
<td align="left">844</td>
</tr>
</tbody>
</table>
<p><img src="fig/sql-filter.svg" alt="SQL Filtering in Action" /></p>
<p>We can use many other Boolean operators to filter our data. For example, we can ask for all information from the DR-1 site collected since 1930:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> * <span class="kw">FROM</span> Visited <span class="kw">WHERE</span> (site=<span class="ot">"DR-1"</span>) <span class="kw">AND</span> (dated<=<span class="ot">"1930-00-00"</span>);</code></pre>
<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>
</tbody>
</table>
<p>(The parentheses around the individual tests aren't strictly required, but they help make the query easier to read.)</p>
<blockquote>
<p>Most database managers have a special data type for dates. In fact, many have two: one for dates, such as "May 31, 1971", and one for durations, such as "31 days". SQLite doesn't: instead, it stores dates as either text (in the ISO-8601 standard format "YYYY-MM-DD HH:MM:SS.SSSS"), real numbers (the number of days since November 24, 4714 BCE), or integers (the number of seconds since midnight, January 1, 1970). If this sounds complicated, it is, but not nearly as complicated as figuring out <a href="http://en.wikipedia.org/wiki/Swedish_calendar">historical dates in Sweden</a>.</p>
</blockquote>
<p>If we want to find out what measurements were taken by either Lake or Roerich, we can combine the tests on their names using <code>OR</code>:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> * <span class="kw">FROM</span> Survey <span class="kw">WHERE</span> person=<span class="ot">"lake"</span> <span class="kw">OR</span> person=<span class="ot">"roe"</span>;</code></pre>
<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">734</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.05</td>
</tr>
<tr class="even">
<td align="left">751</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.1</td>
</tr>
<tr class="odd">
<td align="left">752</td>
<td align="left">lake</td>
<td align="left">rad</td>
<td align="left">2.19</td>
</tr>
<tr class="even">
<td align="left">752</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.09</td>
</tr>
<tr class="odd">
<td align="left">752</td>
<td align="left">lake</td>
<td align="left">temp</td>
<td align="left">-16.0</td>
</tr>
<tr class="even">
<td align="left">752</td>
<td align="left">roe</td>
<td align="left">sal</td>
<td align="left">41.6</td>
</tr>
<tr class="odd">
<td align="left">837</td>
<td align="left">lake</td>
<td align="left">rad</td>
<td align="left">1.46</td>
</tr>
<tr class="even">
<td align="left">837</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.21</td>
</tr>
<tr class="odd">
<td align="left">837</td>
<td align="left">roe</td>
<td align="left">sal</td>
<td align="left">22.5</td>
</tr>
<tr class="even">
<td align="left">844</td>
<td align="left">roe</td>
<td align="left">rad</td>
<td align="left">11.25</td>
</tr>
</tbody>
</table>
<p>Alternatively, we can use <code>IN</code> to see if a value is in a specific set:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> * <span class="kw">FROM</span> Survey <span class="kw">WHERE</span> person <span class="kw">IN</span> (<span class="ot">"lake"</span>, <span class="ot">"roe"</span>);</code></pre>
<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">734</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.05</td>
</tr>
<tr class="even">
<td align="left">751</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.1</td>
</tr>
<tr class="odd">
<td align="left">752</td>
<td align="left">lake</td>
<td align="left">rad</td>
<td align="left">2.19</td>
</tr>
<tr class="even">
<td align="left">752</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.09</td>
</tr>
<tr class="odd">
<td align="left">752</td>
<td align="left">lake</td>
<td align="left">temp</td>
<td align="left">-16.0</td>
</tr>
<tr class="even">
<td align="left">752</td>
<td align="left">roe</td>
<td align="left">sal</td>
<td align="left">41.6</td>
</tr>
<tr class="odd">
<td align="left">837</td>
<td align="left">lake</td>
<td align="left">rad</td>
<td align="left">1.46</td>
</tr>
<tr class="even">
<td align="left">837</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.21</td>
</tr>
<tr class="odd">
<td align="left">837</td>
<td align="left">roe</td>
<td align="left">sal</td>
<td align="left">22.5</td>
</tr>
<tr class="even">
<td align="left">844</td>
<td align="left">roe</td>
<td align="left">rad</td>
<td align="left">11.25</td>
</tr>
</tbody>
</table>
<p>We can combine <code>AND</code> with <code>OR</code>, but we need to be careful about which operator is executed first. If we <em>don't</em> use parentheses, we get this:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> * <span class="kw">FROM</span> Survey <span class="kw">WHERE</span> quant=<span class="ot">"sal"</span> <span class="kw">AND</span> person=<span class="ot">"lake"</span> <span class="kw">OR</span> person=<span class="ot">"roe"</span>;</code></pre>
<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">734</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.05</td>
</tr>
<tr class="even">
<td align="left">751</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.1</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">roe</td>
<td align="left">sal</td>
<td align="left">41.6</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>
<p>which is salinity measurements by Lake, and <em>any</em> measurement by Roerich. We probably want this instead:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> * <span class="kw">FROM</span> Survey <span class="kw">WHERE</span> quant=<span class="ot">"sal"</span> <span class="kw">AND</span> (person=<span class="ot">"lake"</span> <span class="kw">OR</span> person=<span class="ot">"roe"</span>);</code></pre>
<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">734</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.05</td>
</tr>
<tr class="even">
<td align="left">751</td>
<td align="left">lake</td>
<td align="left">sal</td>
<td align="left">0.1</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">roe</td>
<td align="left">sal</td>
<td align="left">41.6</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>
</tbody>
</table>
<p>We can also filter by partial matches. For example, if we want to know something just about the site names beginning with "DR" we can use the <code>LIKE</code> keyword. The percent symbol acts as a <a href="reference.html#wildcard">wildcard</a>, matching any characters in that place. It can be used at the beginning, middle, or end of the string:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> * <span class="kw">FROM</span> Visited <span class="kw">WHERE</span> site <span class="kw">LIKE</span> <span class="ot">"DR%"</span>;</code></pre>
<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">1939-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"></td>
</tr>
<tr class="odd">
<td align="left">844</td>
<td align="left">DR-1</td>
<td align="left">1932-03-22</td>
</tr>
</tbody>
</table>
<p>Finally, we can use <code>DISTINCT</code> with <code>WHERE</code> to give a second level of filtering:</p>
<pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">SELECT</span> <span class="kw">DISTINCT</span> person, quant <span class="kw">FROM</span> Survey <span class="kw">WHERE</span> person=<span class="ot">"lake"</span> <span class="kw">OR</span> person=<span class="ot">"roe"</span>;</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">person</th>
<th align="left">quant</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">lake</td>
<td align="left">sal</td>
</tr>
<tr class="even">
<td align="left">lake</td>
<td align="left">rad</td>
</tr>
<tr class="odd">
<td align="left">lake</td>
<td align="left">temp</td>
</tr>
<tr class="even">
<td align="left">roe</td>
<td align="left">sal</td>
</tr>
<tr class="odd">
<td align="left">roe</td>
<td align="left">rad</td>
</tr>
</tbody>
</table>
<p>But remember: <code>DISTINCT</code> is applied to the values displayed in the chosen columns, not to the entire rows as they are being processed.</p>
<blockquote>
<p>What we have just done is how most people "grow" their SQL queries. We started with something simple that did part of what we wanted, then added more clauses one by one, testing their effects as we went. This is a good strategy --- in fact, for complex queries it's often the <em>only</em> strategy --- but it depends on quick turnaround, and on us recognizing the right answer when we get it.</p>
<p>The best way to achieve quick turnaround is often to put a subset of data in a temporary database and run our queries against that, or to fill a small database with synthesized records. For example, instead of trying our queries against an actual database of 20 million Australians, we could run it against a sample of ten thousand, or write a small program to generate ten thousand random (but plausible) records and use that.</p>
</blockquote>
<div id="fix-this-query" class="challenge">
<h2>Fix This Query</h2>
<p>Suppose we want to select all sites that lie more than 30 degrees from the poles. Our first query is:</p>
<pre><code>SELECT * FROM Site WHERE (lat > -60) OR (lat < 60);</code></pre>
<p>Explain why this is wrong, and rewrite the query so that it is correct.</p>
</div>
<div id="finding-outliers" class="challenge">
<h2>Finding Outliers</h2>
<p>Normalized salinity readings are supposed to be between 0.0 and 1.0. Write a query that selects all records from <code>Survey</code> with salinity values outside this range.</p>
</div>
<div id="matching-patterns" class="challenge">
<h2>Matching Patterns</h2>
<p>Which of these expressions are true?</p>
<ul>
<li><code>'a' LIKE 'a'</code></li>
<li><code>'a' LIKE '%a'</code></li>
<li><code>'beta' LIKE '%a'</code></li>
<li><code>'alpha' LIKE 'a%%'</code></li>
<li><code>'alpha' LIKE 'a%p%'</code></li>
</ul>
</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="http://software-carpentry.org/v5/js/bootstrap/bootstrap.min.js"></script>
</body>
</html>