59aa25a08e207dbf53a3fa1c5676b93f23e28c58
[swc-sql.git] / sql / novice / db.txt
1 % Databases and SQL
2 % Greg Wilson
3 % June 2013
4
5 In the late 1920s and early 1930s,
6 William Dyer,
7 Frank Pabodie,
8 and Valentina Roerich led expeditions to the
9 [Pole of Inaccessibility](http://en.wikipedia.org/wiki/Pole_of_inaccessibility)
10 in the South Pacific,
11 and then onward to Antarctica.
12 Two years ago,
13 Gina Geographer discovered their expedition journals
14 in a storage locker at Miskatonic University.
15 She has scanned and OCR'd the data they contain,
16 and wants to store that information
17 in a way that will make search and analysis easy.
18
19 Gina basically has three options:
20 text files,
21 a spreadsheet,
22 or a database.
23 Text files are easiest to create,
24 and work well with version control,
25 but she would then have to build all her search and analysis herself.
26 Spreadsheets are good for doing simple analysis,
27 but as she found in her last project,
28 they don't handle large or complex data sets very well.
29 She would therefore like to put her data in a database,
30 and this chapter will show her how.
31
32 As many scientists have found out the hard way,
33 if collecting data is the first 90% of the work,
34 managing it is the other 90%.
35 In this chapter,
36 we'll see how to use a database to store and analyze field observations.
37 The techniques we will explore apply directly to other kinds of databases as well,
38 and as we'll see,
39 knowing how to get information *out* of a database is essential to
40 figuring out how to put data *in*.
41
42 ## For Instructors {.guide}
43
44 Relational databases are not as widely used in science as in business,
45 but they are still a common way to store large data sets with complex structure.
46 Even when the data itself isn't in a database,
47 the metadata could be:
48 for example,
49 meteorological data might be stored in files on disk,
50 but data about when and where observations were made,
51 data ranges,
52 and so on could be in a database
53 to make it easier for scientists to find what they want to.
54   
55 The first few sections
56 (up to [Ordering Results](#s:sort))
57 usually go very quickly.
58 The pace usually slows down a bit when null values and aggregation are discussed,
59 mostly because learners have a lot of details to keep straight by this point.
60 Things *really* slow down during the discussion of joins,
61 but this is the key idea in the whole lesson:
62 important ideas like primary keys and referential integrity only make sense
63 once learners have seen how they're used in joins.
64 It's worth going over things a couple of times if necessary
65 (with lots of examples).
66   
67 The final three sections are independent of each other,
68 and can be dropped if time is short.
69 Of the three,
70 people seem to care most about how to add data
71 (which only takes a few minutes to demonstrate),
72 and how to use databases from inside "real" programs.
73 The material on transactions is more abstract than the rest,
74 and should be omitted if [web programming](web.html)
75 isn't being taught.
76 Overall,
77 this material takes three hours to present
78 assuming that a short exercise is done with each topic.
79   
80 ### Prerequisites {.prereq}
81
82 Everything up to the [final section](#s:programming)
83 only requires some understanding of Boolean operators,
84 data types,
85 and pipelines,
86 and what's needed can actually be introduced on the fly.
87 That [final section](#s:programming),
88 which shows how to use databases from within programs,
89 depends on most of the [basic Python material](python.html).
90
91 ### Teaching Notes {.notes}
92
93 * It isn't necessary to cover [sets and dictionaries](setdict.html)
94   before this material,
95   but if that has been discussed,
96   it's helpful to point out that a relational table is a generalized dictionary.
97 * Simple calculations are actually easier to do in a spreadsheet,
98   the advantages of using a database become clear
99   as soon as filtering and joins are needed.
100   Instructors may therefore want to show a spreadsheet
101   with the information from the four database tables
102   consolidated into a single sheet,
103   and demonstrate what's needed in both systems to answer questions like,
104   "What was the average radiation reading in 1931?"
105 * Some learners may have heard that NoSQL databases
106   (i.e., ones that don't use the relational model)
107   are the next big thing,
108   and ask why we're not teaching those.
109   The answers are:
110   * Relational databases are far more widely used than NoSQL databases.
111   * We have far more experience with relational databases
112     than with any other kind,
113     so we have a better idea of what to teach
114     and how to teach it.
115   * NoSQL databases are as different from each other
116     as they are from relational databases.
117     Until a leader emerges,
118     it isn't clear *which* NoSQL database we should teach.
119 * This discussion is a useful companion to that of vectorization
120   in the lesson on [numerical computing](numpy.html):
121   in both cases,
122   the key point is to describe *what* to do,
123   and let the computer figure out *how* to do it.
124
125 ## Selecting {#s:select}
126
127 ### Learning Objectives {.objectives}
128
129 * Explain the difference between a table, a database, and a database manager.
130 * Explain the difference between a field and a record.
131 * Select specific fields from specific tables, and display them in a specific order.
132
133 Duration: 15 minutes (not including time required to download database file and connect to it)
134
135 ### Lesson
136
137 A [relational database](../gloss.html#relational-database)
138 is a way to store and manipulate information
139 that is arranged as [tables](../gloss.html#table).
140 Each table has columns (also known as [fields](../gloss.html#field-database)) which describe the data,
141 and rows (also known as [records](../gloss.html#record-database)) which contain the data.
142   
143 <a id="a:dbms"></a>
144 When we are using a spreadsheet,
145 we put formulas into cells to calculate new values based on old ones.
146 When we are using a database,
147 we send commands
148 (usually called [queries](../gloss.html#query))
149 to a [database manager](../gloss.html#database-manager):
150 a program that manipulates the database for us.
151 The database manager does whatever lookups and calculations the query specifies,
152 returning the results in a tabular form
153 that we can then use as a starting point for further queries.
154   
155 > ### Under the Hood {.box}
156
157 > Every database manager&mdash;Oracle,
158 > IBM DB2, PostgreSQL, MySQL, Microsoft Access, and SQLite&mdash;stores
159 > data in a different way,
160 > so a database created with one cannot be used directly by another.
161 > However,
162 > every database manager can import and export data in a variety of formats,
163 > so it *is* possible to move information from one to another.
164
165 Queries are written in a language called [SQL](../gloss.html#sql),
166 which stands for "Structured Query Language".
167 SQL provides hundreds of different ways to analyze and recombine data;
168 we will only look at a handful,
169 but that handful accounts for most of what scientists do.
170   
171 [Figure 1](#f:survey_db) shows
172 a simple database that stores some of the data
173 Gina extracted from the logs of those long-ago expeditions.
174 It contains four tables:
175
176 <div class="db">  
177
178 Table                   Purpose
179 --------------------    --------------------
180 `Person`                People who took readings.
181 `Site`                  Locations of observation sites.
182 `Visited`               When readings were taken at specific sites.
183 `Survey`                The actual measurement values.
184
185 </div>
186
187 **Person**
188
189 <div class="db">
190
191 ident                   personal                family
192 --------------------    --------------------    --------------------
193 dyer                    William                 Dyer
194 pb                      Frank                   Pabodie
195 lake                    Anderson                Lake
196 roe                     Valentina               Roerich
197 danforth                Frank                   Danforth
198
199 </div>
200
201 **Survey**
202
203 <div class="db">
204
205 taken                   person                  quant                   reading
206 --------------------    --------------------    --------------------    --------------------
207 619                     dyer                    rad                     9.82
208 619                     dyer                    sal                     0.13
209 622                     dyer                    rad                     7.8
210 622                     dyer                    sal                     0.09
211 734                     pb                      rad                     8.41
212 734                     lake                    sal                     0.05
213 734                     pb                      temp                    -21.5
214 735                     pb                      rad                     7.22
215 735                     NULL                    sal                     0.06
216 735                     NULL                    temp                    -26.0
217 751                     pb                      rad                     4.35
218 751                     pb                      temp                    -18.5
219 751                     lake                    sal                     0.1
220 752                     lake                    rad                     2.19
221 752                     lake                    sal                     0.09
222 752                     lake                    temp                    -16.0
223 752                     roe                     sal                     41.6
224 837                     lake                    rad                     1.46
225 837                     lake                    sal                     0.21
226 837                     roe                     sal                     22.5
227 844                     roe                     rad                     11.25
228
229 </div>
230
231 **Site**
232
233 <div class="db">
234
235 name                    lat                     long
236 --------------------    --------------------    --------------------
237 DR-1                    -49.85                  -128.57
238 DR-3                    -47.15                  -126.72
239 MSK-4                   -48.87                  -123.4
240
241 </div>
242
243 **Visited**
244
245 <div class="db">
246
247 ident                   site                    dated
248 --------------------    --------------------    --------------------
249 619                     DR-1                    1927-02-08
250 622                     DR-1                    1927-02-10
251 734                     DR-3                    1939-01-07
252 735                     DR-3                    1930-01-12
253 751                     DR-3                    1930-02-26
254 752                     DR-3                    NULL
255 837                     MSK-4                   1932-01-14
256 844                     DR-1                    1932-03-22
257
258 </div>
259
260 <figcaption>Figure 1: Survey Database</figcaption>
261
262 Notice that three entries&mdash;one in the `Visited` table,
263 and two in the `Survey` table&mdash;are shown as `NULL`.
264 We'll return to these values [later](#s:null).
265 For now,
266 let's write an SQL query that displays scientists' names.
267 We do this using the SQL command `select`,
268 giving it the names of the columns we want and the table we want them from.
269 Our query and its output look like this:
270   
271     sqlite> select family, personal from Person;
272
273 <div class="db">
274
275 --------------------    --------------------
276 Dyer                    William
277 Pabodie                 Frank
278 Lake                    Anderson
279 Roerich                 Valentina
280 Danforth                Frank
281 --------------------    --------------------
282
283 </div>
284
285 The semi-colon at the end of the query
286 tells the database manager that the query is complete and ready to run.
287 If we enter the query without the semi-colon,
288 or press 'enter' part-way through the query,
289 the SQLite interpreter will give us a different prompt
290 to show us that it's waiting for more input:
291   
292     sqlite> select family, personal
293        ...> from Person
294        ...> ;
295
296 <div class="db">
297
298 --------------------    --------------------
299 Dyer                    William
300 Pabodie                 Frank
301 Lake                    Anderson
302 Roerich                 Valentina
303 Danforth                Frank
304 --------------------    --------------------
305
306 </div>
307
308 From now on,
309 we won't bother to display the prompt(s) with our commands.
310   
311 > ### Case and Consistency {.box}
312
313 > We have written our command and the column names in lower case,
314 > and the table name in title case,
315 > but we could use any mix:
316 > SQL is [case insensitive](../gloss.html#case-insensitive),
317 > so we could write them all in upper case,
318 > or even like this:
319
320 > ``` {.sql}
321 > SeLeCt famILY, PERSonal frOM PERson;
322 > ```
323
324 > But please don't:
325 > large SQL queries are hard enough to read
326 > without the extra cognitive load of random capitalization.
327
328 > ### Displaying Results {.box}
329
330 > Exactly *how* the database displays the query's results
331 > depends on what kind of interface we are using.
332 > If we are running SQLite directly from the shell,
333 > its default output looks like this:
334 >     
335 >     Dyer|William
336 >     Pabodie|Frank
337 >     Lake|Anderson
338 >     Roerich|Valentina
339 >     Danforth|Frank
340
341 > If we are using a graphical interface,
342 > such as the [SQLite Manager](https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/) plugin for Firefox
343 > or the [database extension](https://github.com/catherinedevlin/ipython-sql) for the IPython Notebook,
344 > our output will be displayed graphically
345 > ([Figure 2](#f:firefox_output)
346 > and [Figure 3](#f:notebook_output)).
347 > We'll use a simple table-based display in these notes.
348
349 >     <figure id="f:firefox_output">
350 >       <img src="db/firefox_output.png" alt="Firefox SQLite Manager Output" />
351 >       <figcaption>Figure 2: Firefox SQLite Manager Output</figcaption>
352 >     </figure>
353
354 >     <figure id="f:notebook_output">
355 >       <img src="db/notebook_output.png" alt="IPython Notebook Database Extension Output" />
356 >       <figcaption>Figure 3: IPython Notebook Database Extension Output</figcaption>
357 >     </figure>
358
359 Going back to our query,
360 it's important to understand that
361 the rows and columns in a database table aren't actually stored in any particular order.
362 They will always be *displayed* in some order,
363 but we can control that in various ways.
364 For example,
365 we could swap the columns in the output by writing our query as:
366   
367 ``` {.sql}
368 select personal, family from Person;
369 ```
370
371 <div class="db">
372
373 --------------------    --------------------
374 William                 Dyer
375 Frank                   Pabodie
376 Anderson                Lake
377 Valentina               Roerich
378 Frank                   Danforth
379 --------------------    --------------------
380
381 </div>
382   
383 or even repeat columns:
384   
385 ``` {.sql}
386 select ident, ident, ident from Person;
387 ```
388
389 <div class="db">
390
391 --------------------    --------------------    --------------------
392 dyer                    dyer                    dyer
393 pb                      pb                      pb
394 lake                    lake                    lake
395 roe                     roe                     roe
396 danforth                danforth                danforth
397 --------------------    --------------------    --------------------
398
399 </div>
400   
401 We will see ways to rearrange the rows [later](#s:sort).
402   
403 As a shortcut, we can select all of the columns in a table
404 using the wildcard `*`:
405   
406 ``` {.sql}
407 select * from Person;
408 ```
409
410 <div class="db">
411
412 --------------------    --------------------    --------------------
413 dyer                    William                 Dyer
414 pb                      Frank                   Pabodie
415 lake                    Anderson                Lake
416 roe                     Valentina               Roerich
417 danforth                Frank                   Danforth
418 --------------------    --------------------    --------------------
419
420 </div>
421   
422 ### Summary {.keypoints}
423
424 * A relational database stores information in tables with fields and records.
425 * A database manager is a program that manipulates a database.
426 * The commands or queries given to a database manager are usually written in a specialized language called SQL.
427 * SQL is case insensitive.
428 * The rows and columns of a database table aren't stored in any particular order.
429 * Use `select *fields* from *table*` to get all the values for specific fields from a single table.
430 * Use `select * from *table*` to select everything from a table.
431
432 ### Challenges {.challenges}
433
434 * Write a query that selects only site names from the `Site` table.
435
436 * Many people format queries as:
437
438     ```
439     SELECT personal, family FROM person;
440     ```
441
442     or as:
443
444     ```
445     select Personal, Family from PERSON;
446     ```
447
448     What style do you find easiest to read, and why?
449
450 ## Removing Duplicates {#s:distinct}
451
452 ### Learning Objectives {.objectives}
453
454 * Write queries that only display distinct results once.
455
456 Duration: 5 minutes.
457
458 ### Lesson
459
460 Data is often redundant,
461 so queries often return redundant information.
462 For example,
463 if we select the quantitites that have been measured
464 from the `survey` table,
465 we get this:
466   
467 ``` {.sql}
468 select quant from Survey;
469 ```
470
471 <div class="db">
472
473 --------------------
474 rad
475 sal
476 rad
477 sal
478 rad
479 sal
480 temp
481 rad
482 sal
483 temp
484 rad
485 temp
486 sal
487 rad
488 sal
489 temp
490 sal
491 rad
492 sal
493 sal
494 rad
495 --------------------
496
497 </div>
498   
499 We can eliminate the redundant output
500 to make the result more readable
501 by adding the `distinct` keyword
502 to our query:
503   
504 ``` {.sql}
505 select distinct quant from Survey;
506 ```
507
508 <div class="db">
509
510 --------------------
511 rad
512 sal
513 temp
514 --------------------
515
516 </div>
517   
518 If we select more than one column&mdash;for example,
519 both the survey site ID and the quantity measured&mdash;then
520 the distinct pairs of values are returned:
521   
522 ``` {.sql}
523 select distinct taken, quant from Survey;
524 ```
525
526 <div class="db">
527
528 --------------------    --------------------
529 619                     rad
530 619                     sal
531 622                     rad
532 622                     sal
533 734                     rad
534 734                     sal
535 734                     temp
536 735                     rad
537 735                     sal
538 735                     temp
539 751                     rad
540 751                     temp
541 751                     sal
542 752                     rad
543 752                     sal
544 752                     temp
545 837                     rad
546 837                     sal
547 844                     rad
548 --------------------    --------------------
549
550 </div>
551   
552 Notice in both cases that duplicates are removed
553 even if they didn't appear to be adjacent in the database.
554 Again,
555 it's important to remember that rows aren't actually ordered:
556 they're just displayed that way.
557   
558 ### Summary {.keypoints}
559
560 * Use `distinct` to eliminate duplicates from a query's output.
561
562 ### Challenges {.challenges}
563
564 * Write a query that selects distinct dates from the `Site` table.
565
566 * If you are using SQLite from the command line,
567   you can run a single query by passing it to the interpreter
568   right after the path to the database file:
569
570     ```
571     $ sqlite3 survey.db 'select * from Person;'
572     ```
573
574     <div class="db">
575
576     --------------------    --------------------    --------------------
577     dyer                    William                 Dyer
578     pb                      Frank                   Pabodie
579     lake                    Anderson                Lake
580     roe                     Valentina               Roerich
581     danforth                Frank                   Danforth
582     --------------------    --------------------    --------------------
583
584     </div>
585
586     Fill in the missing commands in the pipeline below
587     so that the output contains no redundant values.
588
589     ```
590     $ sqlite3 survey.db 'select person, quant from Survey;' | ____ | ____
591     ```
592
593     Do you think this is less efficient, just as efficient, or more efficient
594     that using `distinct` for large data?
595
596 ## Filtering {#s:filter}
597
598 ### Learning Objectives {.objectives}
599
600 * Write queries that select records based on the values of their fields.
601 * Write queries that select records using combinations of several tests on their fields' values.
602 * Build up complex filtering criteria incrementally.
603 * Explain the logical order in which filtering by field value and displaying fields takes place.
604
605 Duration: 5-10 minutes.
606
607 ### Lesson
608
609 One of the most powerful features of a database is
610 the ability to [filter](../gloss.html#filter) data,
611 i.e.,
612 to select only those records that match certain criteria.
613 For example,
614 suppose we want to see when a particular site was visited.
615 We can select these records from the `Visited` table
616 by using a `where` clause in our query:
617   
618 ``` {.sql}
619 select * from Visited where site='DR-1';
620 ```
621
622 <div class="db">
623
624 --------------------    --------------------    --------------------
625 619                     DR-1                    1927-02-08
626 622                     DR-1                    1927-02-10
627 844                     DR-1                    1932-03-22
628 --------------------    --------------------    --------------------
629
630 </div>
631   
632 The database manager executes this query in two stages
633 ([Figure 4](#f:pipeline_where)).
634 First,
635 it checks at each row in the `Visited` table
636 to see which ones satisfy the `where`.
637 It then uses the column names following the `select` keyword
638 to determine what columns to display.
639   
640   <figure id="f:pipeline_where">
641     <img src="db/pipeline_where.png" alt="Two-Stage Query Processing Pipeline" />
642     <figcaption>Figure 4: Two-Stage Query Processing Pipeline</figcaption>
643   </figure>
644
645 This processing order means that
646 we can filter records using `where`
647 based on values in columns that aren't then displayed:
648   
649 ``` {.sql}
650 select ident from Visited where site='DR-1';
651 ```
652
653 <div class="db">
654
655 --------------------
656 619
657 622
658 844
659 --------------------
660
661 </div>
662   
663 We can use many other Boolean operators to filter our data.
664 For example,
665 we can ask for all information from the DR-1 site collected since 1930:
666   
667 ``` {.sql}
668 select * from Visited where (site='DR-1') and (dated>='1930-00-00');
669 ```
670
671 <div class="db">
672
673 --------------------    --------------------    --------------------
674 844                     DR-1                    1932-03-22
675 --------------------    --------------------    --------------------
676
677 </div>
678   
679 (The parentheses around the individual tests aren't strictly required,
680 but they help make the query easier to read.)
681   
682 > ### Working With Dates {#a:dates .box}
683
684 > Most database managers have a special data type for dates.
685 > In fact, many have two:
686 > one for dates,
687 > such as "May 31, 1971",
688 > and one for durations,
689 > such as "31 days".
690 > SQLite doesn't:
691 > instead,
692 > it stores dates as either text
693 > (in the ISO-8601 standard format "YYYY-MM-DD HH:MM:SS.SSSS"),
694 > real numbers
695 > (the number of days since November 24, 4714 BCE),
696 > or integers
697 > (the number of seconds since midnight, January 1, 1970).
698 > If this sounds complicated,
699 > it is,
700 > but not nearly as complicated as figuring out
701 > [historical dates in Sweden](http://en.wikipedia.org/wiki/Swedish_calendar).
702
703 If we want to find out what measurements were taken by either Lake or Roerich,
704 we can combine the tests on their names using `or`:
705   
706 ``` {.sql}
707 select * from Survey where person='lake' or person='roe';
708 ```
709
710 <div class="db">
711
712 --------------------    --------------------    --------------------    --------------------
713 734                     lake                    sal                     0.05
714 751                     lake                    sal                     0.1
715 752                     lake                    rad                     2.19
716 752                     lake                    sal                     0.09
717 752                     lake                    temp                    -16.0
718 752                     roe                     sal                     41.6
719 837                     lake                    rad                     1.46
720 837                     lake                    sal                     0.21
721 837                     roe                     sal                     22.5
722 844                     roe                     rad                     11.25
723 --------------------    --------------------    --------------------    --------------------
724
725 </div>
726   
727 Alternatively,
728 we can use `in` to see if a value is in a specific set:
729   
730 ``` {.sql}
731 select * from Survey where person in ('lake', 'roe');
732 ```
733
734 <div class="db">
735
736 --------------------    --------------------    --------------------    --------------------
737 734                     lake                    sal                     0.05
738 751                     lake                    sal                     0.1
739 752                     lake                    rad                     2.19
740 752                     lake                    sal                     0.09
741 752                     lake                    temp                    -16.0
742 752                     roe                     sal                     41.6
743 837                     lake                    rad                     1.46
744 837                     lake                    sal                     0.21
745 837                     roe                     sal                     22.5
746 844                     roe                     rad                     11.25
747 --------------------    --------------------    --------------------    --------------------
748
749 </div>
750   
751 We can combine `and` with `or`,
752 but we need to be careful about which operator is executed first.
753 If we *don't* use parentheses,
754 we get this:
755   
756 ``` {.sql}
757 select * from Survey where quant='sal' and person='lake' or person='roe';
758 ```
759
760 <div class="db">
761
762 --------------------    --------------------    --------------------    --------------------
763 734                     lake                    sal                     0.05
764 751                     lake                    sal                     0.1
765 752                     lake                    sal                     0.09
766 752                     roe                     sal                     41.6
767 837                     lake                    sal                     0.21
768 837                     roe                     sal                     22.5
769 844                     roe                     rad                     11.25
770 --------------------    --------------------    --------------------    --------------------
771
772 </div>
773   
774 which is salinity measurements by Lake,
775 and *any* measurement by Roerich.
776 We probably want this instead:
777   
778 ``` {.sql}
779 select * from Survey where quant='sal' and (person='lake' or person='roe');
780 ```
781
782 <div class="db">
783
784 --------------------    --------------------    --------------------    --------------------
785 734                     lake                    sal                     0.05
786 751                     lake                    sal                     0.1
787 752                     lake                    sal                     0.09
788 752                     roe                     sal                     41.6
789 837                     lake                    sal                     0.21
790 837                     roe                     sal                     22.5
791 --------------------    --------------------    --------------------    --------------------
792
793 </div>
794   
795 Finally,
796 we can use `distinct` with `where`
797 to give a second level of filtering:
798   
799 ``` {.sql}
800 select distinct person, quant from Survey where person='lake' or person='roe';
801 ```
802
803 <div class="db">
804
805 --------------------    --------------------
806 lake                    sal
807 lake                    rad
808 lake                    temp
809 roe                     sal
810 roe                     rad
811 --------------------    --------------------
812
813 </div>
814   
815 But remember:
816 `distinct` is applied to the values displayed in the chosen columns,
817 not to the entire rows as they are being processed.
818   
819 > ### Growing Queries {.box}
820
821 > What we have just done is how most people "grow" their SQL queries.
822 > We started with something simple that did part of what we wanted,
823 > then added more clauses one by one,
824 > testing their effects as we went.
825 > This is a good strategy&mdash;in fact,
826 > for complex queries it's often the *only* strategy&mdash;but
827 > it depends on quick turnaround,
828 > and on us recognizing the right answer when we get it.
829 >     
830 > The best way to achieve quick turnaround is often
831 > to put a subset of data in a temporary database
832 > and run our queries against that,
833 > or to fill a small database with synthesized records.
834 > For example,
835 > instead of trying our queries against an actual database of 20 million Australians,
836 > we could run it against a sample of ten thousand,
837 > or write a small program to generate ten thousand random (but plausible) records
838 > and use that.
839
840 ### Summary {.keypoints}
841
842 * Use `where *test*` in a query to filter records based on Boolean tests.
843 * Use `and` and `or` to combine tests.
844 * Use `in` to check if a value is in a set.
845 * Build up queries a bit at a time, and test them against small data sets.
846
847 ### Challenges {.challenges}
848
849 * Gina wants to select all sites that lie within 30&deg; of the equator.
850   Her query is:
851
852     ``` {.sql}
853     select * from Site where (lat > -30) or (lat < 30);
854     ``` {.sql}
855
856     Explain why this is wrong,
857     and rewrite the query so that it is correct.
858
859 * Normalized salinity readings are supposed to be between 0.0 and 1.0.
860   Write a query that selects all records from `Survey`
861   with salinity values outside this range.
862
863 * The SQL test `*column-name* like *pattern*`
864   is true if the value in the named column
865   matches the pattern given;
866   the character '%' can be used any number of times in the pattern
867   to mean "match zero or more characters".
868
869     Expression              Value
870     --------------------    --------------------
871     `'a' like 'a'`          `True`
872     `'a' like '%a'`         `True`
873     `'b' like '%a'`         `False`
874     `'alpha' like 'a%'`     `True`
875     `'alpha' like 'a%p%'`   `True`
876     `'beta' like 'a%p%'`    `False`
877
878     The expression `*column-name* not like *pattern*`
879     inverts the test.
880     Using `like`,
881     write a query that finds all the records in `Visited`
882     that *aren't* from sites labelled 'DR-something'.
883
884 ## Calculating New Values {#s:calc}
885
886 ### Learning Objectives {.objectives}
887
888 * Write queries that do arithmetic using the values in individual records.
889
890 Duration: 5 minutes.
891
892 ### Lesson
893
894 After carefully reading the expedition logs,
895 Gina realizes that the radiation measurements they report
896 may need to be corrected upward by 5%.
897 Rather than modifying the stored data,
898 she can do this calculation on the fly
899 as part of her query:
900   
901 ``` {.sql}
902 select 1.05 * reading from Survey where quant='rad';
903 ```
904
905 <div class="db">
906
907 --------------------
908 10.311
909 8.19
910 8.8305
911 7.581
912 4.5675
913 2.2995
914 1.533
915 11.8125
916 --------------------
917
918 </div>
919   
920 When we run the query,
921 the expression `1.05 * reading` is evaluated for each row.
922 Expressions can use any of the fields,
923 all of usual arithmetic operators,
924 and a variety of common functions.
925 (Exactly which ones depends on which database manager is being used.)
926 For example,
927 we can convert temperature readings from Fahrenheit to Celsius
928 and round to two decimal places as follows:
929   
930 ``` {.sql}
931 select taken, round(5*(reading-32)/9, 2) from Survey where quant='temp';
932 ```
933
934 <div class="db">
935
936 --------------------    --------------------
937 734                     -29.72
938 735                     -32.22
939 751                     -28.06
940 752                     -26.67
941 --------------------    --------------------
942
943 </div>
944   
945 We can also combine values from different fields,
946 for example by using the string concatenation operator `||`:
947   
948 ``` {.sql}
949 select personal || ' ' || family from Person;
950 ```
951
952 <div class="db">
953
954 --------------------
955 William Dyer
956 Frank Pabodie
957 Anderson Lake
958 Valentina Roerich
959 Frank Danforth
960 --------------------
961
962 </div>
963   
964 > ### A Note on Names {.box}
965
966 > It may seem strange to use `personal` and `family` as field names
967 > instead of `first` and `last`,
968 > but it's a necessary first step toward handling cultural differences.
969 > For example,
970 > consider the following rules:
971 >     
972 > Full Name                     Alphabetized Under      Reason
973 > --------------------          --------------------    --------------------
974 > Liu Xiaobo                    Liu                     Chinese family names come first
975 > Leonardo da Vinci             Leonardo                "da Vinci" just means "from Vinci"
976 > Catherine de Medici           Medici                  family name
977 > Jean de La Fontaine           La Fontaine             family name is "La Fontaine"
978 > Juan Ponce de Leon            Ponce de Leon           full family name is "Ponce de Leon"
979 > Gabriel Garcia Marquez                Garcia Marquez          double-barrelled Spanish surnames
980 > Wernher von Braun             von *or* Braun          depending on whether he was in Germany or the US
981 > Elizabeth Alexandra May Windsor       Elizabeth               monarchs alphabetize by the name under which they reigned
982 > Thomas a Beckett              Thomas                  and saints according to the names by which they were canonized
983 >     
984 > Clearly,
985 > even a two-part division into "personal" and "family"
986 > isn't enough...
987
988 ### Summary {.keypoints}
989
990 * Use expressions as fields to calculate per-record values.</li>
991
992 ### Challenges {.challenges}
993
994 * After further reading,
995   Gina realizes that Valentina Roerich
996   was reporting salinity as percentages.
997   Write a query that returns all of her salinity measurements
998   from the `Survey` table
999   with the values divided by 100.
1000
1001 * The `union` operator combines the results of two queries:
1002
1003     ``` {.sql}
1004     select * from Person where ident='dyer' union select * from Person where ident='roe';
1005     ```
1006
1007     <div class="db">
1008
1009     --------------------    --------------------    --------------------
1010     dyer                    William                 Dyer
1011     roe                     Valentina                       Roerich
1012     --------------------    --------------------    --------------------
1013
1014     </div>
1015         
1016     Use `union` to create a consolidated list of salinity measurements
1017     in which Roerich's, and only Roerich's,
1018     have been corrected as described in the previous challenge.
1019     The output should be something like:
1020
1021     <div class="db">
1022
1023     --------------------    --------------------
1024     619                     0.13
1025     622                     0.09
1026     734                     0.05
1027     751                     0.1
1028     752                     0.09
1029     752                     0.416
1030     837                     0.21
1031     837                     0.225
1032     --------------------    --------------------
1033
1034     </div>
1035
1036 * The site identifiers in the `Visited` table have two parts
1037   separated by a '-':
1038
1039     ``` {.sql}
1040     select distinct site from Visited;
1041     ```
1042
1043     <div class="db">
1044
1045     --------------------
1046     DR-1
1047     DR-3
1048     MSK-4
1049     --------------------
1050
1051     </div>
1052   
1053     Some major site identifiers are two letters long and some are three.
1054     The "in string" function `instr(X, Y)`
1055     returns the 1-based index of the first occurrence of string Y in string X,
1056     or 0 if Y does not exist in X.
1057     The substring function `substr(X, I)`
1058     returns the substring of X starting at index I.
1059     Use these two functions to produce a list of unique major site identifiers.
1060     (For this data,
1061     the list should contain only "DR" and "MSK").
1062
1063 * Pabodie's journal notes that all his temperature measurements
1064   are in &deg;F,
1065   but Lake's journal does not report whether he used &deg;F or &deg;C.
1066   How should Gina treat his measurements,
1067   and why?
1068
1069 ## Ordering Results {#s:sort}
1070
1071 ### Learning Objectives {.objectives}
1072
1073 * Write queries that order results according to fields' values.
1074 * Write queries that order results according to calculated values.
1075 * Explain why it is possible to sort records using the values of fields that are not displayed.
1076
1077 Duration: 5 minutes.
1078
1079 ### Lesson
1080
1081 As we mentioned earlier,
1082 database records are not stored in any particular order.
1083 This means that query results aren't necessarily sorted,
1084 and even if they are,
1085 we often want to sort them in a different way,
1086 e.g., by the name of the project instead of by the name of the scientist.
1087 We can do this in SQL by adding an `order by` clause to our query:
1088   
1089 ``` {.sql}
1090 select reading from Survey where quant='rad' order by reading;
1091 ```
1092
1093 <div class="db">
1094
1095 --------------------
1096 1.46
1097 2.19
1098 4.35
1099 7.22
1100 7.8
1101 8.41
1102 9.82
1103 11.25
1104 --------------------
1105
1106 </div>
1107   
1108 By default,
1109 results are sorted in ascending order
1110 (i.e.,
1111 from least to greatest).
1112 We can sort in the opposite order using `desc` (for "descending"):
1113   
1114 ``` {.sql}
1115 select reading from Survey where quant='rad' order by reading desc;
1116 ```
1117
1118 <div class="db">
1119
1120 --------------------
1121 11.25
1122 9.82
1123 8.41
1124 7.8
1125 7.22
1126 4.35
1127 2.19
1128 1.46
1129 --------------------
1130
1131 </div>
1132   
1133 (And if we want to make it clear that we're sorting in ascending order,
1134 we can use `asc` instead of `desc`.)
1135   
1136 We can also sort on several fields at once.
1137 For example,
1138 this query sorts results first in ascending order by `taken`,
1139 and then in descending order by `person`
1140 within each group of equal `taken` values:
1141     
1142 ``` {.sql}
1143 select taken, person from Survey order by taken asc, person desc;
1144 ```
1145
1146 <div class="db">
1147
1148 --------------------    --------------------
1149 619                     dyer
1150 619                     dyer
1151 622                     dyer
1152 622                     dyer
1153 734                     pb
1154 734                     pb
1155 734                     lake
1156 735                     pb
1157 735                     
1158 735                     
1159 751                     pb
1160 751                     pb
1161 751                     lake
1162 752                     roe
1163 752                     lake
1164 752                     lake
1165 752                     lake
1166 837                     roe
1167 837                     lake
1168 837                     lake
1169 844                     roe
1170 --------------------    --------------------
1171
1172 </div>
1173   
1174 This is easier to understand if we also remove duplicates:
1175   
1176 ``` {.sql}
1177 select distinct taken, person from Survey order by taken asc, person desc;
1178 ```
1179
1180 <div class="db">
1181
1182 --------------------    --------------------
1183 619                     dyer
1184 622                     dyer
1185 734                     pb
1186 734                     lake
1187 735                     pb
1188 735                     
1189 751                     pb
1190 751                     lake
1191 752                     roe
1192 752                     lake
1193 837                     roe
1194 837                     lake
1195 844                     roe
1196 --------------------    --------------------
1197
1198 </div>
1199
1200 Since sorting happens before columns are filtered,
1201 we can sort by a field that isn't actually displayed:
1202   
1203 ``` {.sql}
1204 select reading from Survey where quant='rad' order by taken;
1205 ```
1206
1207 <div class="db">
1208
1209 --------------------
1210 9.82
1211 7.8
1212 8.41
1213 7.22
1214 4.35
1215 2.19
1216 1.46
1217 11.25
1218 --------------------
1219
1220 </div>
1221   
1222 We can also sort results by the value of an expression.
1223 In SQLite,
1224 for example,
1225 the `random` function returns a pseudo-random integer
1226 each time it is called
1227 (i.e.,
1228 once per record):
1229   
1230 ``` {.sql}
1231 select random(), ident from Person;
1232 ```
1233
1234 <div class="db">
1235
1236 --------------------    --------------------
1237 -6309766557809954936    dyer
1238 -2098461436941487136    pb
1239 -2248225962969032314    lake
1240 6062184424509295966     roe
1241 -1268956870222271271    danforth
1242 --------------------    --------------------
1243
1244 </div>
1245   
1246 So to randomize the order of our query results,
1247 e.g., when doing clinical trials,
1248 we can sort them by the value of this function:
1249   
1250 ``` {.sql}
1251 select ident from Person order by random();
1252 ```
1253
1254 <div class="db">
1255
1256 --------------------
1257 danforth
1258 pb
1259 dyer
1260 lake
1261 roe
1262 --------------------
1263
1264 </div>
1265
1266 ``` {.sql}
1267 select ident from Person order by random();
1268 ```
1269
1270 <div class="db">
1271
1272 --------------------
1273 roe
1274 dyer
1275 pb
1276 lake
1277 danforth
1278 --------------------
1279
1280 </div>
1281   
1282 Our query pipeline now has four stages
1283 ([Figure 5](#f:pipeline_sort_distinct)):
1284   
1285 * Select the rows that pass the `where` criteria.
1286 * Sort them if required.
1287 * Filter the columns according to the `select` criteria.
1288 * Remove duplicates if required.
1289
1290   <figure id="f:pipeline_sort_distinct">
1291     <img src="db/pipeline_sort_distinct.png" alt="Four-Stage Query Processing Pipeline" />
1292     <figcaption>Figure 5: Four-Stage Query Processing Pipeline</figcaption>
1293   </figure>
1294
1295 ### Summary {.keypoints}
1296
1297 * Use `order by` (with `asc` or `desc`) to order a query's results.
1298 * Use `random` to generate pseudo-random numbers.
1299
1300 ### Challenges {.challenges}
1301
1302 * Create a list of sites identifiers
1303   and their distance from the equator in kilometers,
1304   sorted from furthest to closest.
1305   (A degree of latitude corresponds to 111.12 km.)
1306
1307 * Gina needs a list of radiation measurements from all sites
1308   sorted by when they were taken.
1309   The query:
1310
1311     ``` {.sql}
1312     select * from Survey where quant='rad' order by taken;
1313     ```
1314
1315     produces the correct answer for the data used in our examples.
1316     Explain when and why it might produce the wrong answer.
1317
1318 ## Missing Data {#s:null}
1319
1320 ### Learning Objectives {.objectives}
1321
1322 * Explain what databases use the special value `NULL` to represent.
1323 * Explain why databases should *not* uses their own special values (like 9999 or "N/A") to represent missing or unknown data.
1324 * Explain what atomic and aggregate calculations involving `NULL` produce, and why.
1325 * Write queries that include or exclude records containing `NULL`.
1326
1327 Duration: 10-20 minutes
1328 (depending on whether or not the instructor includes an anecdote about
1329 what happens when you *don't* take missing data into account).
1330
1331 ### Lesson
1332
1333 Real-world data is never complete&mdash;there are always holes.
1334 Databases represent these holes using special value called `null`.
1335 `null` is not zero, `False`, or the empty string;
1336 it is a one-of-a-kind value that means "nothing here".
1337 Dealing with `null` requires a few special tricks
1338 and some careful thinking.
1339   
1340 To start,
1341 let's have a look at the `Visited` table.
1342 There are eight records,
1343 but #752 doesn't have a date&mdash;or rather,
1344 its date is null:
1345   
1346 ``` {.sql}
1347 select * from Visited;
1348 ```
1349
1350 <div class="db">
1351
1352 --------------------    --------------------    --------------------
1353 619                     DR-1                    1927-02-08
1354 622                     DR-1                    1927-02-10
1355 734                     DR-3                    1939-01-07
1356 735                     DR-3                    1930-01-12
1357 751                     DR-3                    1930-02-26
1358 752                     DR-3                    
1359 837                     MS-4                    1932-01-14
1360 844                     DR-1                    1932-03-22
1361 --------------------    --------------------    --------------------
1362
1363 </div>
1364   
1365 > ### Displaying Nulls {.box}
1366
1367 > Different databases display nulls differently.
1368 > Unfortunately,
1369 > SQLite's default is to print nothing at all,
1370 > which makes nulls easy to overlook
1371 > (particularly if they're in the middle of a long row).
1372
1373 Null doesn't behave like other values.
1374 If we select the records that come before 1930:
1375   
1376 ``` {.sql}
1377 select * from Visited where dated<'1930-00-00';
1378 ```
1379
1380 <div class="db">
1381
1382 --------------------    --------------------    --------------------
1383 619                     DR-1                    1927-02-08
1384 622                     DR-1                    1927-02-10
1385 --------------------    --------------------    --------------------
1386
1387 </div>
1388   
1389 we get two results,
1390 and if we select the ones that come during or after 1930:
1391   
1392 ``` {.sql}
1393 select * from Visited where dated>='1930-00-00';
1394 ```
1395
1396 <div class="db">
1397
1398 --------------------    --------------------    --------------------
1399 734                     DR-3                    1939-01-07
1400 735                     DR-3                    1930-01-12
1401 751                     DR-3                    1930-02-26
1402 837                     MS-4                    1932-01-14
1403 844                     DR-1                    1932-03-22
1404 --------------------    --------------------    --------------------
1405
1406 </div>
1407   
1408 we get five,
1409 but record #752 isn't in either set of results.
1410 The reason is that
1411 `null<'1930-00-00'`
1412 is neither true nor false:
1413 null means, "We don't know,"
1414 and if we don't know the value on the left side of a comparison,
1415 we don't know whether the comparison is true or false.
1416 Since databases represent "don't know" as null,
1417 the value of `null<'1930-00-00'`
1418 is actually `null`.
1419 `null>='1930-00-00'` is also null
1420 because we can't answer to that question either.
1421 And since the only records kept by a `where`
1422 are those for which the test is true,
1423 record #752 isn't included in either set of results.
1424   
1425 Comparisons aren't the only operations that behave this way with nulls.
1426 `1+null` is `null`,
1427 `5*null` is `null`,
1428 `log(null)` is `null`,
1429 and so on.
1430 In particular,
1431 comparing things to null with = and != produces null:
1432   
1433 ``` {.sql}
1434 select * from Visited where dated=NULL;
1435 ```
1436
1437 ``` {.sql}
1438 select * from Visited where dated!=NULL;
1439 ```
1440   
1441 To check whether a value is `null` or not,
1442 we must use a special test `is null`:
1443   
1444 ``` {.sql}
1445 select * from Visited where dated is NULL;
1446 ```
1447
1448 <div class="db">
1449
1450 --------------------    --------------------
1451 752                     DR-3            
1452 --------------------    --------------------
1453
1454 </div>
1455   
1456 or its inverse `is not null`:
1457   
1458 ``` {.sql}
1459 select * from Visited where dated is not NULL;
1460 ```
1461
1462 <div class="db">
1463
1464 --------------------    --------------------    --------------------
1465 619                     DR-1                    1927-02-08
1466 622                     DR-1                    1927-02-10
1467 734                     DR-3                    1939-01-07
1468 735                     DR-3                    1930-01-12
1469 751                     DR-3                    1930-02-26
1470 837                     MS-4                    1932-01-14
1471 844                     DR-1                    1932-03-22
1472 --------------------    --------------------    --------------------
1473
1474 </div>
1475   
1476 Null values cause headaches wherever they appear.
1477 For example,
1478 suppose we want to find the all of salinity measurements
1479 that weren't taken by Dyer.
1480 It's natural to write the query like this:
1481   
1482 ``` {.sql}
1483 select * from Survey where quant='sal' and person!='lake';
1484 ```
1485
1486 <div class="db">
1487
1488 --------------------    --------------------    --------------------    --------------------
1489 619                     dyer                    sal                     0.13
1490 622                     dyer                    sal                     0.09
1491 752                     roe                     sal                     41.6
1492 837                     roe                     sal                     22.5
1493 --------------------    --------------------    --------------------    --------------------
1494
1495 </div>
1496   
1497 but this query filters omits the records
1498 where we don't know who took the measurement.
1499 Once again,
1500 the reason is that when `person` is `null`,
1501 the `!=` comparison produces `null`,
1502 so the record isn't kept in our results.
1503 If we want to keep these records
1504 we need to add an explicit check:
1505   
1506 ``` {.sql}
1507 select * from Survey where quant='sal' and (person!='lake' or person is null);
1508 ```
1509
1510 <div class="db">
1511
1512 --------------------    --------------------    --------------------    --------------------
1513 619                     dyer                    sal                     0.13
1514 622                     dyer                    sal                     0.09
1515 735                                             sal                     0.06
1516 752                     roe                     sal                     41.6
1517 837                     roe                     sal                     22.5
1518 --------------------    --------------------    --------------------    --------------------
1519
1520 </div>
1521
1522   
1523 We still have to decide whether this is the right thing to do or not.
1524 If we want to be absolutely sure that
1525 we aren't including any measurements by Lake in our results,
1526 we need to exclude all the records for which we don't know who did the work.
1527   
1528 > ### What Happens When You Forget {.box}
1529
1530 > Several years ago,
1531 > I was helping a group who were looking at
1532 > the spread of drug-resistant tuberculosis (DRTB)
1533 > in industrialized countries.
1534 > In particular,
1535 > they wanted to know if it was spreading faster among less affluent people.
1536 >     
1537 > We tackled the problem by combining two data sets.
1538 > The first gave us skin and blood test results for DRTB
1539 > along with patients' postal codes
1540 > (the only identifying information we were allowed---we didn't even have gender).
1541 > The second was Canadian census data that gave us
1542 > median income per postal code.
1543 > Since a PC is about 300-800 people,
1544 > we felt justified in joining the first with the second
1545 > to estimate incomes for people with positive and negative test results.
1546 >     
1547 > To our surprise,
1548 > we didn't find a correlation between income and infection.
1549 > We were just about to publish when someone spotted the mistake I'd made.
1550 >     
1551 > Question: Who *doesn't* have a postal code?
1552 >     
1553 > Answer: Homeless people.
1554 >     
1555 > When I did the join,
1556 > I was throwing away homeless people,
1557 > which introduced a statistically significant error in my results.
1558 > But I couldn't just set the income of anyone without a postal code to zero,
1559 > because our sample included another set of people without postal codes:
1560 > 16-21 year olds whose addresses were suppressed
1561 > because they had tested positive for sexually-transmitted diseases.
1562 >     
1563 > At this point the problem is no longer a database issue,
1564 > but rather a question of statistics.
1565 > The takeaway is,
1566 > checking your queries when you're programming is as important as
1567 > checking your samples when you're doing chemistry.
1568
1569 ### Summary {.keypoints}
1570
1571 * Use `null` in place of missing information.
1572 * Almost every operation involving `null` produces `null` as a result.
1573 * Test for nulls using `is null` and `is not null`.
1574
1575 ### Challenges {.challenges}
1576
1577 * Write a query that sorts the records in `Visited` by date,
1578   omitting entries for which the date is not known
1579   (i.e., is null).
1580
1581 * What do you expect the query:
1582
1583     ``` {.sql}
1584     select * from Visited where dated in ('1927-02-08', null);
1585     ```
1586
1587     to produce?
1588     What does it actually produce?
1589
1590 * Some database designers prefer to use
1591   a [sentinel value](../gloss.html#sentinel-value)
1592   to mark missing data rather than `null`.
1593   For example,
1594   they will use the date "0000-00-00" to mark a missing date,
1595   or -1.0 to mark a missing salinity or radiation reading
1596   (since actual readings cannot be negative).
1597   What does this simplify?
1598   What burdens or risks does it introduce?
1599
1600 ## Aggregation {#s:aggregate}
1601
1602 ### Learning Objectives {.objectives}
1603
1604 * Write queries that combine values from many records to create a single aggregate value.
1605 * Write queries that put records into groups based on their values.
1606 * Write queries that combine values group by group.
1607 * Explain what is displayed for *unaggregated* fields when some fields are aggregated.
1608
1609 Duration: 10 minutes.
1610
1611 ### Lesson
1612
1613 Gina now wants to calculate ranges and averages for her data.
1614 She knows how to select all of the dates from the `Visited` table:
1615   
1616 ``` {.sql}
1617 select dated from Visited;
1618 ```
1619
1620 <div class="db">
1621
1622 --------------------
1623 1927-02-08
1624 1927-02-10
1625 1939-01-07
1626 1930-01-12
1627 1930-02-26
1628      
1629 1932-01-14
1630 1932-03-22
1631 --------------------
1632
1633 </div>
1634   
1635 but to combine them,
1636 she must use an [aggregation function](../gloss.html#aggregation-function)
1637 such as `min` or `max`.
1638 Each of these functions takes a set of records as input,
1639 and produces a single record as output:
1640   
1641 ``` {.sql}
1642 select min(dated) from Visited;
1643 ```
1644
1645 <div class="db">
1646
1647 --------------------
1648 1927-02-08
1649 --------------------
1650
1651 </div>
1652
1653 ``` {.sql}
1654 select max(dated) from Visited;
1655 ```
1656
1657 <div class="db">
1658
1659 --------------------
1660 1939-01-07
1661 --------------------
1662
1663 </div>
1664   
1665 `min` and `max` are just two of
1666 the aggregation functions built into SQL.
1667 Three others are `avg`,
1668 `count`,
1669 and `sum`:
1670   
1671 ``` {.sql}
1672 select avg(reading) from Survey where quant='sal';
1673 ```
1674
1675 <div class="db">
1676
1677 --------------------
1678 7.20333333333
1679 --------------------
1680
1681 </div>
1682
1683 ``` {.sql}
1684 select count(reading) from Survey where quant='sal';
1685 ```
1686
1687 <div class="db">
1688
1689 --------------------
1690 9
1691 --------------------
1692
1693 </div>
1694
1695 ``` {.sql}
1696 select sum(reading) from Survey where quant='sal';
1697 ```
1698
1699 <div class="db">
1700
1701 --------------------
1702 64.83
1703 --------------------
1704
1705 </div>
1706   
1707 We used `count(reading)` here,
1708 but we could just as easily have counted `quant`
1709 or any other field in the table,
1710 or even used `count(*)`,
1711 since the function doesn't care about the values themselves,
1712 just how many values there are.
1713   
1714 SQL lets us do several aggregations at once.
1715 We can,
1716 for example,
1717 find the range of sensible salinity measurements:
1718   
1719 ``` {.sql}
1720 select min(reading), max(reading) from Survey where quant='sal' and reading<=1.0;
1721 ```
1722
1723 <div class="db">
1724
1725 --------------------    --------------------
1726 0.05                    0.21
1727 --------------------    --------------------
1728
1729 </div>
1730   
1731 We can also combine aggregated results with raw results,
1732 although the output might surprise you:
1733   
1734 ``` {.sql}
1735 select person, count(*) from Survey where quant='sal' and reading<=1.0;
1736 ```
1737
1738 <div class="db">
1739
1740 --------------------    --------------------
1741 lake                    7
1742 --------------------    --------------------
1743
1744 </div>
1745   
1746 Why does Lake's name appear rather than Roerich's or Dyer's?
1747 The answer is that when it has to aggregate a field,
1748 but isn't told how to,
1749 the database manager chooses an actual value from the input set.
1750 It might use the first one processed,
1751 the last one,
1752 or something else entirely.
1753   
1754 Another important fact is that when there are no values to aggregate,
1755 aggregation's result is "don't know"
1756 rather than zero or some other arbitrary value:
1757   
1758 ``` {.sql}
1759 select person, max(reading), sum(reading) from Survey where quant='missing';
1760 ```
1761   
1762 One final important feature of aggregation functions is that
1763 they are inconsistent with the rest of SQL in a very useful way.
1764 If we add two values,
1765 and one of them is null,
1766 the result is null.
1767 By extension,
1768 if we use `sum` to add all the values in a set,
1769 and any of those values are null,
1770 the result should also be null.
1771 It's much more useful,
1772 though,
1773 for aggregation functions to ignore null values
1774 and only combine those that are non-null.
1775 This behavior lets us write our queries as:
1776   
1777 ``` {.sql}
1778 select min(dated) from Visited;
1779 ```
1780
1781 <div class="db">
1782
1783 --------------------
1784 1927-02-08
1785 --------------------
1786
1787 </div>
1788   
1789 instead of always having to filter explicitly:
1790   
1791 ``` {.sql}
1792 select min(dated) from Visited where dated is not null;
1793 ```
1794
1795 <div class="db">
1796
1797 --------------------
1798 1927-02-08
1799 --------------------
1800
1801 </div>
1802   
1803 ### Summary {.keypoints}
1804
1805 * Use aggregation functions like `sum` and `max` to combine query results.
1806 * Use `count` function to count the number of results.
1807 * If some fields are aggregated and others are not, the database manager chooses an arbitrary result for the unaggregated field.
1808 * Most aggregation functions skip nulls when combining values.
1809
1810 ### Challenges {.challenges}
1811
1812 * How many temperature readings did Frank Pabodie record,
1813   and what was their average value?
1814
1815 * The average of a set of values is the sum of the values
1816   divided by the number of values.
1817   Does this mean that the `avg` function returns 2.0 or 3.0
1818   when given the values 1.0, `null`, and 5.0?
1819
1820 * Gina wants to calculate the difference between
1821   each individual radiation reading
1822   and the average of all the radiation readings.
1823   She writes the query:
1824
1825     ``` {.sql}
1826     select reading-avg(reading) from Survey where quant='rad';
1827     ```
1828
1829     What does this actually produce, and why?
1830
1831 * The function `group_concat(field, separator)`
1832   concatenates all the values in a field
1833   using the specified separator character
1834   (or ',' if the separator isn't specified).
1835   Use this to produce a one-line list of scientists' names,
1836   such as:
1837
1838     ``` {.sql}
1839     William Dyer, Frank Pabodie, Anderson Lake, Valentina Roerich, Frank Danforth
1840     ```
1841
1842     Can you find a way to order the list by surname?
1843
1844 ## Grouping {#s:grouping}
1845
1846 ### Learning Objectives {.objectives}
1847
1848 * Group results to be aggregated separately.
1849 * Explain when grouping occurs in the processing pipeline.
1850
1851 Duration: 5 minutes.
1852
1853 ### Lesson
1854
1855 Aggregating all records at once doesn't always make sense.
1856 For example,
1857 suppose Gina suspects that there is a systematic bias in her data,
1858 and that some scientists' radiation readings are higher than others.
1859 We know that this doesn't work:
1860   
1861 ``` {.sql}
1862 select person, count(reading), round(avg(reading), 2)
1863 from  Survey
1864 where quant='rad';
1865 ```
1866
1867 <div class="db">
1868
1869 --------------------    --------------------    --------------------
1870 roe                     8                       6.56
1871 --------------------    --------------------    --------------------
1872
1873 </div>
1874   
1875 because the database manager selects a single arbitrary scientist's name
1876 rather than aggregating separately for each scientist.
1877 Since there are only five scientists,
1878 she could write five queries of the form:
1879   
1880 ``` {.sql}
1881 select person, count(reading), round(avg(reading), 2)
1882 from  Survey
1883 where quant='rad'
1884 and   person='dyer';
1885 ```
1886
1887 <div class="db">
1888
1889 --------------------    --------------------    --------------------
1890 dyer                    2                       8.81
1891 --------------------    --------------------    --------------------
1892
1893 </div>
1894   
1895 but this would be tedious,
1896 and if she ever had a data set with fifty or five hundred scientists,
1897 the chances of her getting all of those queries right is small.
1898   
1899 What we need to do is
1900 tell the database manager to aggregate the hours for each scientist separately
1901 using a `group by` clause:
1902   
1903 ``` {.sql}
1904 select   person, count(reading), round(avg(reading), 2)
1905 from     Survey
1906 where    quant='rad'
1907 group by person;
1908 ```
1909
1910 <div class="db">
1911
1912 --------------------    --------------------    --------------------
1913 dyer                    2                       8.81
1914 lake                    2                       1.82
1915 pb                      3                       6.66
1916 roe                     1                       11.25
1917 --------------------    --------------------    --------------------
1918
1919 </div>
1920   
1921 `group by` does exactly what its name implies:
1922 groups all the records with the same value for the specified field together
1923 so that aggregation can process each batch separately.
1924 Since all the records in each batch have the same value for `person`,
1925 it no longer matters that the database manager
1926 is picking an arbitrary one to display
1927 alongside the aggregated `reading` values
1928 ([Figure 6](#f:grouped_aggregation)).
1929   
1930   <figure id="f:grouped_aggregation">
1931     <img src="db/grouped_aggregation.png" alt="Grouped Aggregation" />
1932     <figcaption>Figure 6: Grouped Aggregation</figcaption>
1933   </figure>
1934
1935 Just as we can sort by multiple criteria at once,
1936 we can also group by multiple criteria.
1937 To get the average reading by scientist and quantity measured,
1938 for example,
1939 we just add another field to the `group by` clause:
1940   
1941 ``` {.sql}
1942 select   person, quant, count(reading), round(avg(reading), 2)
1943 from     Survey
1944 group by person, quant;
1945 ```
1946
1947 <div class="db">
1948
1949 --------------------    --------------------    --------------------    --------------------
1950                         sal                     1                       0.06
1951                         temp                    1                       -26.0
1952 dyer                    rad                     2                       8.81
1953 dyer                    sal                     2                       0.11
1954 lake                    rad                     2                       1.82
1955 lake                    sal                     4                       0.11
1956 lake                    temp                    1                       -16.0
1957 pb                      rad                     3                       6.66
1958 pb                      temp                    2                       -20.0
1959 roe                     rad                     1                       11.25
1960 roe                     sal                     2                       32.05
1961 --------------------    --------------------    --------------------    --------------------
1962
1963 </div>
1964   
1965 Note that we have added `person` to the list of fields displayed,
1966 since the results wouldn't make much sense otherwise.
1967   
1968 Let's go one step further and remove all the entries
1969 where we don't know who took the measurement:
1970   
1971 ``` {.sql}
1972 select   person, quant, count(reading), round(avg(reading), 2)
1973 from     Survey
1974 where    person is not null
1975 group by person, quant
1976 order by person, quant;
1977 ```
1978
1979 <div class="db">
1980
1981 --------------------    --------------------    --------------------    --------------------
1982 dyer                    rad                     2                       8.81
1983 dyer                    sal                     2                       0.11
1984 lake                    rad                     2                       1.82
1985 lake                    sal                     4                       0.11
1986 lake                    temp                    1                       -16.0
1987 pb                      rad                     3                       6.66
1988 pb                      temp                    2                       -20.0
1989 roe                     rad                     1                       11.25
1990 roe                     sal                     2                       32.05
1991 --------------------    --------------------    --------------------    --------------------
1992
1993 </div>
1994   
1995 Looking more closely,
1996 this query:
1997   
1998 * selected records from the `Survey` table
1999   where the `person` field was not null;
2000
2001 * grouped those records into subsets
2002   so that the `person` and `quant` values in each subset
2003   were the same;
2004
2005 * ordered those subsets first by `person`,
2006   and then within each sub-group by `quant`;
2007   and
2008
2009 * counted the number of records in each subset,
2010   calculated the average `reading` in each,
2011   and chose a `person` and `quant` value from each
2012   (it doesn't matter which ones,
2013   since they're all equal).
2014
2015 Our query processing pipeline now looks like
2016 [Figure 7](#f:pipeline_grouping).
2017   
2018   <figure id="f:pipeline_grouping">
2019     <img src="db/pipeline_grouping.png" alt="Query Processing Pipeline With Grouping" />
2020     <figcaption>Figure 7: Query Processing Pipeline With Grouping</figcaption>
2021   </figure>
2022
2023 ### Summary {.keypoints}
2024
2025 * Use `group by` to group values for separate aggregation.
2026
2027 ### Challenges {.challenges}
2028
2029 * Write a single query that finds the earliest and latest date
2030   that each site was visited.
2031
2032 * Show the records produced by each stage of
2033   [Figure 7](#f:pipeline_grouping)
2034   for the following query:
2035
2036     ``` {.sql}
2037     select   min(reading), max(reading) from Survey
2038     where    taken in (734, 735)
2039     and      quant='temp'
2040     group by taken, quant;
2041     ```
2042
2043 * How can the query in the previous challenge be simplified
2044   without changing its result?
2045
2046 ## Combining Data {#s:join}
2047
2048 ### Learning Objectives {.objectives}
2049
2050 * Explain what primary keys and foreign keys are.
2051 * Write queries that combine information from two or more tables by matching keys.
2052 * Write queries using aliases for table names.
2053 * Explain why the `tablename.fieldname` notation is needed when tables are joined.
2054 * Explain the logical sequence of operations that occurs when two or more tables are joined.
2055
2056 Duration: 20 minutes (and expect to have to walk through an example step-by-step).
2057
2058 ### Lesson
2059
2060 In order to submit her data to a web site
2061 that aggregates historical meteorological data,
2062 Gina needs to format it as
2063 latitude, longitude, date, quantity, and reading.
2064 However,
2065 her latitudes and longitudes are in the `Site` table,
2066 while the dates of measurements are in the `Visited` table
2067 and the readings themselves are in the `Survey` table.
2068 She needs to combine these tables somehow.
2069
2070 The SQL command to do this is `join`.
2071 To see how it works,
2072 let's start by joining the `Site` and `Visited` tables:
2073   
2074 ``` {.sql}
2075 select * from Site join Visited;
2076 ```
2077
2078 <div class="db">
2079
2080 --------------------    --------------------    --------------------    --------------------    --------------------    --------------------
2081 DR-1                    -49.85                  -128.57                 619                     DR-1                    1927-02-08
2082 DR-1                    -49.85                  -128.57                 622                     DR-1                    1927-02-10
2083 DR-1                    -49.85                  -128.57                 734                     DR-3                    1939-01-07
2084 DR-1                    -49.85                  -128.57                 735                     DR-3                    1930-01-12
2085 DR-1                    -49.85                  -128.57                 751                     DR-3                    1930-02-26
2086 DR-1                    -49.85                  -128.57                 752                     DR-3                    
2087 DR-1                    -49.85                  -128.57                 837                     MS-4                    1932-01-14
2088 DR-1                    -49.85                  -128.57                 844                     DR-1                    1932-03-22
2089 DR-3                    -47.15                  -126.72                 619                     DR-1                    1927-02-08
2090 DR-3                    -47.15                  -126.72                 622                     DR-1                    1927-02-10
2091 DR-3                    -47.15                  -126.72                 734                     DR-3                    1939-01-07
2092 DR-3                    -47.15                  -126.72                 735                     DR-3                    1930-01-12
2093 DR-3                    -47.15                  -126.72                 751                     DR-3                    1930-02-26
2094 DR-3                    -47.15                  -126.72                 752                     DR-3                    
2095 DR-3                    -47.15                  -126.72                 837                     MS-4                    1932-01-14
2096 DR-3                    -47.15                  -126.72                 844                     DR-1                    1932-03-22
2097 MS-4                    -48.87                  -123.4                  619                     DR-1                    1927-02-08
2098 MS-4                    -48.87                  -123.4                  622                     DR-1                    1927-02-10
2099 MS-4                    -48.87                  -123.4                  734                     DR-3                    1939-01-07
2100 MS-4                    -48.87                  -123.4                  735                     DR-3                    1930-01-12
2101 MS-4                    -48.87                  -123.4                  751                     DR-3                    1930-02-26
2102 MS-4                    -48.87                  -123.4                  752                     DR-3                    
2103 MS-4                    -48.87                  -123.4                  837                     MS-4                    1932-01-14
2104 MS-4                    -48.87                  -123.4                  844                     DR-1                    1932-03-22
2105 --------------------    --------------------    --------------------    --------------------    --------------------    --------------------
2106
2107 </div>
2108   
2109 `join` creates
2110 the [cross product](../gloss.html#cross-product)
2111 of two tables,
2112 i.e.,
2113 it joins each record of one with each record of the other
2114 to give all possible combinations.
2115 Since there are three records in `Site`
2116 and eight in `Visited`,
2117 the join's output has 24 records.
2118 And since each table has three fields,
2119 the output has six fields.
2120   
2121 What the join *hasn't* done is
2122 figure out if the records being joined have anything to do with each other.
2123 It has no way of knowing whether they do or not until we tell it how.
2124 To do that,
2125 we add a clause specifying that
2126 we're only interested in combinations that have the same site name:
2127   
2128 ``` {.sql}
2129 select * from Site join Visited on Site.name=Visited.site;
2130 ```
2131
2132 <div class="db">
2133
2134 --------------------    --------------------    --------------------    --------------------    --------------------    --------------------
2135 DR-1                    -49.85                  -128.57                 619                     DR-1                    1927-02-08
2136 DR-1                    -49.85                  -128.57                 622                     DR-1                    1927-02-10
2137 DR-1                    -49.85                  -128.57                 844                     DR-1                    1932-03-22
2138 DR-3                    -47.15                  -126.72                 734                     DR-3                    1939-01-07
2139 DR-3                    -47.15                  -126.72                 735                     DR-3                    1930-01-12
2140 DR-3                    -47.15                  -126.72                 751                     DR-3                    1930-02-26
2141 DR-3                    -47.15                  -126.72                 752                     DR-3                    
2142 MS-4                    -48.87                  -123.4                  837                     MS-4                    1932-01-14
2143 --------------------    --------------------    --------------------    --------------------    --------------------    --------------------
2144
2145 </div>
2146   
2147 `on` does the same job as `where`:
2148 it only keeps records that pass some test.
2149 (The difference between the two is that `on` filters records
2150 as they're being created,
2151 while `where` waits until the join is done
2152 and then does the filtering.)
2153 Once we add this to our query,
2154 the database manager throws away records
2155 that combined information about two different sites,
2156 leaving us with just the ones we want.
2157   
2158 Notice that we used `table.field` to specify field names
2159 in the output of the join.
2160 We do this because tables can have fields with the same name,
2161 and we need to be specific which ones we're talking about.
2162 For example,
2163 if we joined the `person` and `visited` tables,
2164 the result would inherit a field called `ident`
2165 from each of the original tables.
2166
2167 We can now use the same dotted notation
2168 to select the three columns we actually want
2169 out of our join:
2170   
2171 ``` {.sql}
2172 select Site.lat, Site.long, Visited.dated
2173 from   Site join Visited
2174 on     Site.name=Visited.site;
2175 ```
2176
2177 <div class="db">
2178
2179 --------------------    --------------------    --------------------
2180 -49.85                  -128.57                 1927-02-08
2181 -49.85                  -128.57                 1927-02-10
2182 -49.85                  -128.57                 1932-03-22
2183 -47.15                  -126.72                 
2184 -47.15                  -126.72                 1930-01-12
2185 -47.15                  -126.72                 1930-02-26
2186 -47.15                  -126.72                 1939-01-07
2187 -48.87                  -123.4                  1932-01-14
2188 --------------------    --------------------    --------------------
2189
2190 </div>
2191   
2192 If joining two tables is good,
2193 joining many tables must be better.
2194 In fact,
2195 we can join any number of tables
2196 simply by adding more `join` clauses to our query,
2197 and more `on` tests to filter out combinations of records
2198 that don't make sense:
2199
2200 ``` {.sql}
2201 select Site.lat, Site.long, Visited.dated, Survey.quant, Survey.reading
2202 from   Site join Visited join Survey
2203 on     Site.name=Visited.site
2204 and    Visited.ident=Survey.taken
2205 and    Visited.dated is not null;
2206 ```
2207
2208 <div class="db">
2209
2210 --------------------    --------------------    --------------------    --------------------    --------------------
2211 -49.85                  -128.57                 1927-02-08              rad                     9.82
2212 -49.85                  -128.57                 1927-02-08              sal                     0.13
2213 -49.85                  -128.57                 1927-02-10              rad                     7.8
2214 -49.85                  -128.57                 1927-02-10              sal                     0.09
2215 -47.15                  -126.72                 1939-01-07              rad                     8.41
2216 -47.15                  -126.72                 1939-01-07              sal                     0.05
2217 -47.15                  -126.72                 1939-01-07              temp                    -21.5
2218 -47.15                  -126.72                 1930-01-12              rad                     7.22
2219 -47.15                  -126.72                 1930-01-12              sal                     0.06
2220 -47.15                  -126.72                 1930-01-12              temp                    -26.0
2221 -47.15                  -126.72                 1930-02-26              rad                     4.35
2222 -47.15                  -126.72                 1930-02-26              sal                     0.1
2223 -47.15                  -126.72                 1930-02-26              temp                    -18.5
2224 -48.87                  -123.4                  1932-01-14              rad                     1.46
2225 -48.87                  -123.4                  1932-01-14              sal                     0.21
2226 -48.87                  -123.4                  1932-01-14              sal                     22.5
2227 -49.85                  -128.57                 1932-03-22              rad                     11.25
2228 --------------------    --------------------    --------------------    --------------------    --------------------
2229
2230 </div>
2231   
2232 <a id="a:keys"></a>
2233 We can tell which records from `Site`, `Visited`, and `Survey`
2234 correspond with each other
2235 because those tables contain
2236 [primary keys](../gloss.html#primary-key)
2237 and [foreign keys](../gloss.html#foreign-key).
2238 A primary key is a value,
2239 or combination of values,
2240 that uniquely identifies each record in a table.
2241 A foreign key is a value (or combination of values) from one table
2242 that identifies a unique record in another table.
2243 Another way of saying this is that
2244 a foreign key is the primary key of one table
2245 that appears in some other table.
2246 In our database,
2247 `Person.ident` is the primary key in the `Person` table,
2248 while `Survey.person` is a foreign key
2249 relating the `Survey` table's entries
2250 to entries in `Person`.
2251
2252 Most database designers believe that
2253 every table should have a well-defined primary key.
2254 They also believe that this key should be separate from the data itself,
2255 so that if we ever need to change the data,
2256 we only need to make one change in one place.
2257 One easy way to do this is
2258 to create an arbitrary, unique ID for each record
2259 as we add it to the database.
2260 This is actually very common:
2261 those IDs have names like "student numbers" and "patient numbers",
2262 and they almost always turn out to have originally been
2263 a unique record identifier in some database system or other.
2264 As the query below demonstrates,
2265 SQLite automatically numbers records as they're added to tables,
2266 and we can use those record numbers in queries:
2267   
2268 ``` {.sql}
2269 select rowid, * from Person;
2270 ```
2271
2272 <div class="db">
2273
2274 --------------------    --------------------    --------------------    --------------------
2275 1                       dyer                    William                 Dyer
2276 2                       pb                      Frank                   Pabodie
2277 3                       lake                    Anderson                Lake
2278 4                       roe                     Valentina               Roerich
2279 5                       danforth                Frank                   Danforth
2280 --------------------    --------------------    --------------------    --------------------
2281
2282 </div>
2283   
2284 ### Summary {.keypoints}
2285
2286 * Use `join` to create all possible combinations of records from two or more tables.
2287 * Use `join *tables* on *test*` to keep only those combinations that pass some test.
2288 * Use `*table*.*field*` to specify a particular field of a particular table.
2289 * Every record in a table should be uniquely identified by the value of its primary key.
2290
2291 ### Challenges {.challenges}
2292
2293 * Write a query that lists all radiation readings from the DR-1 site.
2294
2295 * Write a query that lists all sites visited by people named "Frank".
2296
2297 * Describe in your own words what the following query produces:
2298
2299     ``` {.sql}
2300     select Site.name from Site join Visited
2301     on Site.lat<-49.0 and Site.name=Visited.site and Visited.dated>='1932-00-00';
2302     ```
2303
2304 * Why does the `Person` table have an `ident` field?
2305   Why do we not just use scientists' names in the `Survey` table?
2306
2307 * Why does the table `Site` exist?
2308   Why didn't Gina just record latitudes and longitudes
2309   directly in the `Visited` and `Survey` tables?
2310
2311 ## Creating and Modifying Tables {#s:create}
2312
2313 ### Learning Objectives {.box}
2314
2315 * Write queries that create database tables with fields of common types.
2316 * Write queries that specify the primary and foreign key relationships of tables.
2317 * Write queries that specify whether field values must be unique and/or are allowed to be `null`.
2318 * Write queries that erase database tables.
2319 * Write queries that add records to database tables.
2320 * Write queries that delete specific records from tables.
2321 * Explain what referential integrity is, and how a database can become inconsistent as data is changed.
2322
2323 Duration: 10 minutes.
2324
2325 ### Lesson
2326
2327 So far we have only looked at how to get information out of a database,
2328 both because that is more frequent than adding information,
2329 and because most other operations only make sense
2330 once queries are understood.
2331 If we want to create and modify data,
2332 we need to know two other pairs of commands.
2333   
2334 The first pair are `create table` and `drop table`.
2335 While they are written as two words,
2336 they are actually single commands.
2337 The first one creates a new table;
2338 its arguments are the names and types of the table's columns.
2339 For example,
2340 the following statements create the four tables in our survey database:
2341   
2342     create table Person(ident text, personal text, family text);
2343     create table Site(name text, lat real, long real);
2344     create table Visited(ident integer, site text, dated text);
2345     create table Survey(taken integer, person text, quant real, reading real);
2346
2347 We can get rid of one of our tables using:
2348   
2349     drop table Survey;
2350
2351 Be very careful when doing this:
2352 most databases have some support for undoing changes,
2353 but it's better not to have to rely on it.
2354   
2355 Different database systems support different data types for table columns,
2356 but most provide the following:
2357
2358 <div class="db">
2359
2360 --------------------    --------------------
2361 `integer`               A signed integer.
2362 `real`                  A floating point value.
2363 `text`                  A string.
2364 `blob`                  Any "binary large object" such as an image or audio file.
2365 --------------------    --------------------
2366
2367 </div>
2368
2369 Most databases also support Booleans and date/time values;
2370 SQLite uses the integers 0 and 1 for the former,
2371 and represents the latter as discussed [earlier](#a:dates).
2372 An increasing number of databases also support geographic data types,
2373 such as latitude and longitude.
2374 Keeping track of what particular systems do or do not offer,
2375 and what names they give different data types,
2376 is an unending portability headache.
2377   
2378 When we create a table,
2379 we can specify several kinds of constraints on its columns.
2380 For example,
2381 a better definition for the `Survey` table would be:
2382   
2383     create table Survey(
2384         taken   integer not null, -- where reading taken
2385         person  text,             -- may not know who took it
2386         quant   real not null,    -- the quantity measured
2387         reading real not null,    -- the actual reading
2388         primary key(taken, quant),
2389         foreign key(taken) references Visited(ident),
2390         foreign key(person) references Person(ident)
2391     );
2392
2393 Once again,
2394 exactly what constraints are avialable
2395 and what they're called
2396 depends on which database manager we are using.
2397   
2398 Once tables have been created,
2399 we can add and remove records using our other pair of commands,
2400 `insert` and `delete`.
2401 The simplest form of `insert` statement lists values in order:
2402   
2403     insert into Site values('DR-1', -49.85, -128.57);
2404     insert into Site values('DR-3', -47.15, -126.72);
2405     insert into Site values('MSK-4', -48.87, -123.40);
2406
2407 We can also insert values into one table directly from another:
2408   
2409     create table JustLatLong(lat text, long TEXT);
2410     insert into JustLatLong select lat, long from site;
2411
2412 Deleting records can be a bit trickier,
2413 because we have to ensure that the database remains internally consistent.
2414 If all we care about is a single table,
2415 we can use the `DELETE` command with a `WHERE` clause
2416 that matches the records we want to discard.
2417 For example,
2418 once we realize that Frank Danforth didn't take any measurements,
2419 we can remove him from the `Person` table like this:
2420   
2421     delete from Person where ident = "danforth";
2422
2423 But what if we removed Anderson Lake instead?
2424 Our `Survey` table would still contain seven records
2425 of measurements he'd taken:
2426   
2427 ``` {.sql}
2428 select count(*) from Survey where person='lake';
2429 ```
2430
2431 <div class="db">
2432
2433 --------------------
2434 7
2435 --------------------
2436
2437 </div>
2438   
2439 That's never supposed to happen:
2440 `Survey.person` is a foreign key into the `Person` table,
2441 and all our queries assume there will be a row in the latter
2442 matching every value in the former.
2443   
2444 This problem is called [referential integrity](../gloss.html#referential-integrity):
2445 we need to ensure that all references between tables can always be resolved correctly.
2446 One way to do this is to delete all the records
2447 that use `'lake'` as a foreign key
2448 before deleting the record that uses it as a primary key.
2449 If our database manager supports it,
2450 we can automate this
2451 using [cascading delete](../gloss.html#cascading-delete).
2452 However,
2453 this technique is outside the scope of this chapter.
2454   
2455 > ### Other Ways to Do It {#a:hybrid .box}
2456
2457 > Many applications use a hybrid storage model
2458 > instead of putting everything into a database:
2459 > the actual data (such as astronomical images) is stored in files,
2460 > while the database stores the files' names,
2461 > their modification dates,
2462 > the region of the sky they cover,
2463 > their spectral characteristics,
2464 > and so on.
2465 > This is also how most music player software is built:
2466 > the database inside the application keeps track of the MP3 files,
2467 > but the files themselves live on disk.
2468
2469 ### Summary {.keypoints}
2470
2471 * Use `create table *name*(...)` to create a table.
2472 * Use `drop table *name*` to erase a table.
2473 * Specify field names and types when creating tables.
2474 * Specify `primary key`, `foreign key`, `not null`, and other constraints when creating tables.
2475 * Use `insert into *table* values(...)` to add records to a table.
2476 * Use `delete from *table* where *test*` to erase records from a table.
2477 * Maintain referential integrity when creating or deleting information.
2478
2479 ### Challenges {.challenges}
2480
2481 * Write an SQL statement to replace all uses of `null`
2482   in `Survey.person`
2483   with the string `'unknown'`.
2484
2485 * One of Gina's colleagues has sent her a [CSV](../gloss.html#csv) file
2486   containing temperature readings by Robert Olmstead,
2487   which is formatted like this:
2488
2489     ```
2490     Taken,Temp
2491     619,-21.5
2492     622,-15.5
2493     ```
2494
2495     Write a small Python program that reads this file in
2496     and prints out the SQL `insert` statements needed
2497     to add these records to the survey database.
2498     Note: you will need to add an entry for Olmstead
2499     to the `Person` table.
2500     If you are testing your program repeatedly,
2501     you may want to investigate SQL's `insert or replace` command.
2502
2503 * SQLite has several administrative commands that aren't part of the SQL standard.
2504   One of them is `.dump`,
2505   which prints the SQL commands needed to re-create the database.
2506   Another is `.load`,
2507   which reads a file created by `.dump` and restores the database.
2508   A colleague of yours thinks that storing dump files (which are text) in version control
2509   is a good way to track and manage changes to the database.
2510   What are the pros and cons of this approach?
2511
2512 <section id="s:transactions">
2513
2514 ## Transactions
2515
2516 ### Learning Objectives {.objectives}
2517
2518 * Explain what a race condition is.
2519 * Explain why database operations sometimes have to be placed ina transaction to ensure correct behavior.
2520 * Explain what it means to commit a transaction.</li>
2521
2522 Duration: 10 minutes.
2523
2524 ### Lesson
2525
2526 Suppose we have another table in our database that shows
2527 which pieces of equipment have been borrowed by which scientists:
2528   
2529 ``` {.sql}
2530 select * from Equipment;
2531 ```
2532
2533 <div class="db">
2534
2535 --------------------    --------------------
2536 dyer                    CX-211 oscilloscope
2537 pb                      Greenworth balance
2538 lake                    Cavorite damping plates
2539 --------------------    --------------------
2540
2541 </div>
2542     
2543 (We should actually give each piece of equipment a unique ID,
2544 and use that ID here instead of the full name,
2545 just as we created a separate table for scientists earlier in this chapter,
2546 but we will bend the rules for now.)
2547 If William Dyer gives the oscilloscope to Valentina Roerich,
2548 we need to execute two statements to update this table:
2549   
2550     delete from Equipment where person="dyer" and thing="CX-211 oscilloscope";
2551     insert into Equipment values("roe", "CX-211 oscilloscope");
2552
2553 This is all fine&mdash;unless our program happens to crash
2554 between the first statement and the second.
2555 If that happens,
2556 the `Equipment` table won't have a record for the oscilloscope at all.
2557 Such a crash may seem unlikely,
2558 but remember:
2559 if a computer can do two billion operations per second,
2560 that means there are two billion opportunities every second for something to go wrong.
2561 And if our operations take a long time to complete&mdash;as they will
2562 when we are working with large datasets,
2563 or when the database is being heavily used&mdash;the odds of failure increase.
2564
2565 What we really want is a way to ensure that every operation is [ACID](../gloss.html#acid):
2566 [atomic](../gloss.html#atomic-operation) (i.e. indivisible),
2567 consistent, isolated, and durable.
2568 The precise meanings of these terms doesn't matter;
2569 what does is the notion that
2570 every logical operation on the database should either run to completion
2571 as if nothing else was going on at the same time,
2572 or fail without having any effect at all.
2573
2574 The tool we use to ensure that this happens is called
2575 a [transaction](../gloss.html#transaction).
2576 Here's how we should actually write the statements
2577 to move the oscilloscope from one person to another:
2578   
2579     begin transaction;
2580     delete from Equipment where person="dyer" and thing="CX-211 oscilloscope";
2581     insert into Equipment values("roe", "CX-211 oscilloscope");
2582     end transaction;
2583
2584 The database manager treats everything in the transaction as one large statement.
2585 If anything goes wrong inside,
2586 then none of the changes made in the transaction will actually be written to the database&mdash;it
2587 will be as if the transaction had never happened.
2588 Changes are only stored permanently
2589 when we [commit](../gloss.html#commit) them at the end of the transaction.
2590   
2591 > ### Transactions and Commits {.box}
2592
2593 > We first used the term "transaction" in
2594 > [our discussion of version control](svn.html#b:basics:transaction).
2595 > That's not a coincidence:
2596 > behind the scenes,
2597 > tools like Subversion are using many of the same algorithms as database managers
2598 > to ensure that either everything happens consistently
2599 > or nothing happens at all.
2600 > We [use the term "commit"](svn.html#a:commit) for the same reason:
2601 > just as our changes to local files aren't written back to the version control repository
2602 > until we commit them,
2603 > our (apparent) changes to a database aren't written to disk
2604 > until we say so.
2605
2606 Transactions serve another purpose as well.
2607 Suppose there is another table in the database called `Exposure`
2608 that records the number of days each scientist was exposed to
2609 higher-than-normal levels of radiation:
2610   
2611 ``` {.sql}
2612 select * from Exposure;
2613 ```
2614
2615 <div class="db">
2616
2617 --------------------    --------------------
2618 pb                      4
2619 dyer                    1
2620 lake                    5
2621 --------------------    --------------------
2622
2623 </div>
2624   
2625 After going through the journal entries for 1932,
2626 Gina wants to add two days to Lake's count:
2627
2628 ``` {.sql}  
2629 update Exposure set days = days + 2 where person='lake';
2630 ```
2631
2632 However,
2633 her labmate has been doing through the journal entries for 1933
2634 to help Gina meet a paper deadline.
2635 At the same moment as Gina runs her command,
2636 her labmate runs this
2637 to add one more day to Lake's exposure:
2638
2639 ``` {.sql}
2640 update Exposure set days = days + 1 where person='lake';
2641 ```
2642
2643 After both operations have completed,
2644 the database should show that Lake was exposed for eight days
2645 (the original five, plus two from Gina, plus one from her labmate).
2646 However,
2647 there is a small chance that it won't.
2648 To see why,
2649 let's break the two queries into their respective read and write steps
2650 and place them side by side:
2651
2652 --------------------            --------------------  
2653 `X = read Exposure('lake', __)` `Y = read Exposure('lake', __)`
2654 `write Exposure('lake', X+2)`   `write Exposure('lake', Y+1)`
2655 --------------------            --------------------  
2656   
2657 The database can only actually do one thing at once,
2658 so it must put these four operations into some sequential order.
2659 That order has to respect the original order within each column,
2660 but the database can interleave the two columns any way it wants.
2661 If it orders them like this:
2662   
2663 --------------------            --------------------  
2664 `X = read Exposure('lake', __)` `X` is 5
2665 `write Exposure('lake', X+2)`   database contains 7
2666 `Y = read Exposure('lake', __)` `Y` is 7
2667 `write Exposure('lake', Y+1)`   database contains 8
2668 --------------------            --------------------  
2669   
2670 then all is well.
2671 But what if it interleaves the operations like this:
2672   
2673 --------------------            --------------------  
2674 `X = read Exposure('lake', __)` `X` is 5
2675 `Y = read Exposure('lake', __)` `Y` is 5
2676 `write Exposure('lake', X+2)`   database contains 7
2677 `write Exposure('lake', Y+1)`   database contains 6
2678 --------------------            --------------------  
2679   
2680 This ordering puts the initial value, 5, into both `X` and `Y`.
2681 It then writes 7 back to the database (the third statement),
2682 and then overwrites that with 6,
2683 since `Y` holds 5.
2684   
2685 This is called a [race condition](../gloss.html#race-condition),
2686 since the final result depends on a race between the two operations.
2687 Race conditions are part of what makes programming large systems with many components a nightmare:
2688 they are difficult to spot in advance
2689 (since they are caused by the interactions between components,
2690 rather than by anything in any one of those components),
2691 and can be almost impossible to debug
2692 (since they usually occur intermittently and infrequently).
2693
2694 Transactions come to our rescue once again.
2695 If Gina and her labmate put their statements in transactions,
2696 the database will act as if it executed all of one and then all of the other.
2697 Whether or not it *actually* does this is up to whoever wrote the database manager:
2698 modern databases use very sophisticated algorithms to determine
2699 which operations actually have to be run sequentially,
2700 and which can safely be run in parallel to improve performance.
2701 The key thing is that
2702 every transaction will appear to have had the entire database to itself.
2703   
2704 ### Summary {.keypoints}
2705
2706 * Place operations in a transaction to ensure that they appear to be atomic, consistent, isolated, and durable.
2707
2708 ### Challenges {.challenges}
2709
2710 * A friend of yours manages a database of aerial photographs.
2711   New records are added all the time,
2712   but existing records are never modified or updated.
2713   Your friend claims that because of this,
2714   he doesn't need to put his queries in transactions.
2715   Is he right or wrong, and why?
2716
2717 ## Programming With Databases {#s:programming}
2718
2719 ### Learning Objectives {.objectives}
2720
2721 * Write a Python program that queries a database and processes the results.
2722 * Explain what an SQL injection attack is.
2723 * Write a program that safely interpolates values into queries.
2724
2725 Duration: 20 minutes.
2726
2727 ### Lesson
2728
2729 To end this chapter,
2730 let's have a look at how to access a database from
2731 a general-purpose programming language like Python.
2732 Other languages use almost exactly the same model:
2733 library and function names may differ,
2734 but the concepts are the same.
2735
2736 Here's a short Python program that selects latitudes and longitudes
2737 from an SQLite database stored in a file called `survey.db`:
2738
2739 ``` {.python}  
2740 import sqlite3
2741 connection = sqlite3.connect("survey.db")
2742 cursor = connection.cursor()
2743 cursor.execute("select site.lat, site.long from site;")
2744 results = cursor.fetchall()
2745 for r in results:
2746     print r
2747 cursor.close()
2748 connection.close()
2749 ```
2750
2751 The program starts by importing the `sqlite3` library.
2752 If we were connecting to MySQL, DB2, or some other database,
2753 we would import a different library,
2754 but all of them provide the same functions,
2755 so that the rest of our program does not have to change
2756 (at least, not much)
2757 if we switch from one database to another.
2758
2759 Line 2 establishes a connection to the database.
2760 Since we're using SQLite,
2761 all we need to specify is the name of the database file.
2762 Other systems may require us to provide a username and password as well.
2763 Line 3 then uses this connection to create
2764 a [cursor](../gloss.html#cursor);
2765 just like the cursor in an editor,
2766 its role is to keep track of where we are in the database.
2767
2768 On line 4, we use that cursor to ask the database to execute a query for us.
2769 The query is written in SQL,
2770 and passed to `cursor.execute` as a string.
2771 It's our job to make sure that SQL is properly formatted;
2772 if it isn't,
2773 or if something goes wrong when it is being executed,
2774 the database will report an error.
2775
2776 The database returns the results of the query to us
2777 in response to the `cursor.fetchall` call on line 5.
2778 This result is a list with one entry for each record in the result set;
2779 if we loop over that list (line 6) and print those list entries (line 7),
2780 we can see that each one is a tuple
2781 with one element for each field we asked for.
2782
2783 Finally, lines 8 and 9 close our cursor and our connection,
2784 since the database can only keep a limited number of these open at one time.
2785 Since establishing a connection takes time,
2786 though,
2787 we shouldn't open a connection,
2788 do one operation,
2789 then close the connection,
2790 only to reopen it a few microseconds later to do another operation.
2791 Instead,
2792 it's normal to create one connection that stays open for the lifetime of the program.
2793   
2794 > ### What Are The u's For? {.box}
2795
2796 > You may have noticed that
2797 > each of the strings in our output has a lower-case 'u' in front of it.
2798 > That is Python's way of telling us that the string is stored in
2799 > [Unicode](../gloss.html#unicode).
2800
2801 Queries in real applications will often depend on values provided by users.
2802 For example,
2803 a program might take a user ID as a command-line parameter
2804 and display the user's full name:
2805
2806 ``` {.python}  
2807 import sys
2808 import sqlite3
2809
2810 query = "select personal, family from Person where ident='%s';"
2811 user_id = sys.argv[1]
2812
2813 connection = sqlite3.connect("survey.db")
2814 cursor = connection.cursor()
2815
2816 cursor.execute(query % user_id)
2817 results = cursor.fetchall()
2818 print results[0][0], results[0][1]
2819
2820 cursor.close()
2821 connection.close()
2822 ```
2823
2824 The variable `query` holds the statement we want to execute
2825 with a `%s` format string where we want to insert
2826 the ID of the person we're looking up.
2827 It seems simple enough,
2828 but what happens if someone gives the program this input?
2829   
2830     dyer"; drop table Survey; select "
2831
2832 It looks like there's garbage after the name of the project,
2833 but it is very carefully chosen garbage.
2834 If we insert this string into our query,
2835 the result is:
2836
2837 ``` {.sql}  
2838 select personal, family from Person where ident='dyer'; drop table Survey; select '';
2839 ```
2840
2841 Whoops:
2842 if we execute this,
2843 it will erase one of the tables in our database.
2844   
2845 This technique is called [SQL injection](../gloss.html#sql-injection),
2846 and it has been used to attack thousands of programs over the years.
2847 In particular,
2848 many web sites that take data from users insert values directly into queries
2849 without checking them carefully first.
2850   
2851 Since a villain might try to smuggle commands into our queries in many different ways,
2852 the safest way to deal with this threat is
2853 to replace characters like quotes with their escaped equivalents,
2854 so that we can safely put whatever the user gives us inside a string.
2855 We can do this by using a [prepared statement](../gloss.html#prepared-statement)
2856 instead of formatting our statements as strings.
2857 Here's what our example program looks like if we do this:
2858
2859 ``` {.python}
2860 import sys
2861 import sqlite3
2862
2863 query = "select personal, family from Person where ident=?;"
2864 user_id = sys.argv[1]
2865
2866 connection = sqlite3.connect("survey.db")
2867 cursor = connection.cursor()
2868
2869 cursor.execute(query, [user_id])
2870 results = cursor.fetchall()
2871 print results[0][0], results[0][1]
2872
2873 cursor.close()
2874 connection.close()
2875 ```
2876
2877 The key changes are in the query string and the `execute` call.
2878 Instead of formatting the query ourselves,
2879 we put question marks in the query template where we want to insert values.
2880 When we call `execute`,
2881 we provide a list
2882 that contains as many values as there are question marks in the query.
2883 The library matches values to question marks in order,
2884 and translates any special characters in the values
2885 into their escaped equivalents
2886 so that they are safe to use.
2887
2888 ### Summary {.keypoints}
2889
2890 * Most applications that use databases embed SQL in a general-purpose programming language.
2891 * Database libraries use connections and cursors to manage interactions.
2892 * Programs can fetch all results at once, or a few results at a time.
2893 * If queries are constructed dynamically using input from users, malicious users may be able to inject their own commands into the queries.
2894 * Dynamically-constructed queries can use SQL's native formatting to safeguard against such attacks.
2895
2896 ### Challenges {.challenges}
2897
2898 * Write a Python program that creates a new database
2899   in a file called `original.db`
2900   containing a single table called `Pressure`,
2901   with a single field called `reading`,
2902   and inserts 100,000 random numbers between 10.0 and 25.0.
2903   How long does it take this program to run?
2904   How long does it take to run a program
2905   that simply writes those random numbers to a file?
2906
2907 * Write a Python program that creates a new database
2908   called `backup.db`
2909   with the same structure as `original.db`
2910   and copies all the values greater than 20.0
2911   from `original.db` to `backup.db`.
2912   Which is faster:
2913   filtering values in the query,
2914   or reading everything into memory and filtering in Python?
2915
2916 ## Summing Up {#s:summary}
2917
2918 There are many things databases can't do,
2919 or can't do well
2920 (which is why we have general-purpose programming languages like Python).
2921 However,
2922 they are still the best tool available
2923 for managing many kinds of complex, structured data.
2924 Thousands of programmer-years have gone into their design and implementation
2925 so that they can handle very large datasets&mdash;terabytes or more&mdash;quickly and reliably.
2926 Queries allow for great flexibility in how you are able to analyze your data,
2927 which makes databases a good choice when you are exploring data.