forked from datacarpentry/sql-ecology-lesson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstructor-notes.html
697 lines (659 loc) · 29.4 KB
/
instructor-notes.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
<!DOCTYPE html>
<!-- START: inst/pkgdown/templates/layout.html --><!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>Data Management with SQL for Ecologists: Instructor Notes</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="assets/styles.css">
<script src="assets/scripts.js" type="text/javascript"></script><!-- mathjax --><script type="text/x-mathjax-config">
MathJax.Hub.Config({
config: ["MMLorHTML.js"],
jax: ["input/TeX","input/MathML","output/HTML-CSS","output/NativeMML", "output/PreviewHTML"],
extensions: ["tex2jax.js","mml2jax.js","MathMenu.js","MathZoom.js", "fast-preview.js", "AssistiveMML.js", "a11y/accessibility-menu.js"],
TeX: {
extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
},
tex2jax: {
inlineMath: [['\\(', '\\)']],
displayMath: [ ['$$','$$'], ['\\[', '\\]'] ],
processEscapes: true
}
});
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><!-- Responsive Favicon for The Carpentries --><link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
<link rel="manifest" href="site.webmanifest">
<link rel="mask-icon" href="safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
</head>
<body>
<header id="top" class="navbar navbar-expand-md navbar-light bg-white top-nav data"><a class="visually-hidden-focusable skip-link" href="#main-content">Skip to main content</a>
<div class="container-fluid top-nav-container">
<div class="col-md-6">
<div class="large-logo">
<img alt="Data Carpentry" src="assets/images/data-logo.svg">
</div>
</div>
<div class="selector-container">
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle bordered-button" type="button" id="dropdownMenu1" data-bs-toggle="dropdown" aria-expanded="false">
<i aria-hidden="true" class="icon" data-feather="eye"></i> Learner View <i data-feather="chevron-down"></i>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li><button class="dropdown-item" type="button" onclick="window.location.href='instructor/instructor-notes.html';">Instructor View</button></li>
</ul>
</div>
</div>
</div>
<hr></header><nav class="navbar navbar-expand-xl navbar-light bg-white bottom-nav data" aria-label="Main Navigation"><div class="container-fluid nav-container">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
<span class="menu-title">Menu</span>
</button>
<div class="nav-logo">
<img class="small-logo" alt="Data Carpentry" src="assets/images/data-logo-sm.svg">
</div>
<div class="lesson-title-md">
Data Management with SQL for Ecologists
</div>
<div class="search-icon-sm">
<!-- TODO: do not show until we have search
<i role="img" aria-label="search button" data-feather="search"></i>
-->
</div>
<div class="desktop-nav">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<span class="lesson-title">
Data Management with SQL for Ecologists
</span>
</li>
<li class="nav-item">
<a class="nav-link" href="key-points.html">Key Points</a>
</li>
<li class="nav-item">
<a class="nav-link" href="reference.html#glossary">Glossary</a>
</li>
<li class="nav-item">
<a class="nav-link" href="profiles.html">Learner Profiles</a>
</li>
<li class="nav-item dropdown">
<button class="nav-link dropdown-toggle" id="navbarDropdown" data-bs-toggle="dropdown" aria-expanded="false">
More <i data-feather="chevron-down"></i>
</button>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="discuss.html">Discussion</a></li>
<li><a class="dropdown-item" href="reference.html">Reference</a></li>
</ul>
</li>
</ul>
</div>
<form class="d-flex col-md-2 search-form">
<fieldset disabled>
<input class="form-control me-2 searchbox" type="search" placeholder="Search" aria-label="Search"><button class="btn btn-outline-success tablet-search-button" type="submit">
<i class="search-icon" data-feather="search" role="img" aria-label="search button"></i>
</button>
</fieldset>
</form>
</div>
<!--/div.container-fluid -->
</nav><div class="col-md-12 mobile-title">
Data Management with SQL for Ecologists
</div>
<aside class="col-md-12 lesson-progress"><div style="width: %" class="percentage">
%
</div>
<div class="progress data">
<div class="progress-bar data" role="progressbar" style="width: %" aria-valuenow="" aria-label="Lesson Progress" aria-valuemin="0" aria-valuemax="100">
</div>
</div>
</aside><div class="container">
<div class="row">
<!-- START: inst/pkgdown/templates/navbar.html -->
<div id="sidebar-col" class="col-lg-4">
<div id="sidebar" class="sidebar">
<nav aria-labelledby="flush-headingEleven"><button role="button" aria-label="close menu" alt="close menu" aria-expanded="true" aria-controls="sidebar" class="collapse-toggle">
<i class="search-icon" data-feather="x" role="img"></i>
</button>
<div class="sidebar-inner">
<div class="row mobile-row">
<div class="col">
<div class="sidenav-view-selector">
<div class="accordion accordion-flush" id="accordionFlush9">
<div class="accordion-item">
<h2 class="accordion-header" id="flush-headingNine">
<button class="accordion-button collapsed" id="instructor" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseNine" aria-expanded="false" aria-controls="flush-collapseNine">
<i id="eye" aria-hidden="true" class="icon" data-feather="eye"></i> Learner View
</button>
</h2>
<div id="flush-collapseNine" class="accordion-collapse collapse" aria-labelledby="flush-headingNine" data-bs-parent="#accordionFlush2">
<div class="accordion-body">
<a href="instructor/instructor-notes.html">Instructor View</a>
</div>
</div>
</div>
<!--/div.accordion-item-->
</div>
<!--/div.accordion-flush-->
</div>
<!--div.sidenav-view-selector -->
</div>
<!--/div.col -->
<hr>
</div>
<!--/div.mobile-row -->
<div class="accordion accordion-flush" id="accordionFlush11">
<div class="accordion-item">
<button id="chapters" class="accordion-button show" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseEleven" aria-expanded="false" aria-controls="flush-collapseEleven">
<h2 class="accordion-header chapters" id="flush-headingEleven">
EPISODES
</h2>
</button>
<div id="flush-collapseEleven" class="accordion-collapse show collapse" aria-labelledby="flush-headingEleven" data-bs-parent="#accordionFlush11">
<div class="accordion-body">
<div class="accordion accordion-flush" id="accordionFlush1">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading1">
<a href="index.html">Summary and Setup</a>
</div>
<!--/div.accordion-header-->
</div>
<!--/div.accordion-item-->
</div>
<!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush2">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading2">
<a href="00-sql-introduction.html">1. Introducing Databases and SQL</a>
</div>
<!--/div.accordion-header-->
</div>
<!--/div.accordion-item-->
</div>
<!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush3">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading3">
<a href="01-sql-basic-queries.html">2. Accessing Data With Queries</a>
</div>
<!--/div.accordion-header-->
</div>
<!--/div.accordion-item-->
</div>
<!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush4">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading4">
<a href="02-sql-aggregation.html">3. Aggregating and Grouping Data</a>
</div>
<!--/div.accordion-header-->
</div>
<!--/div.accordion-item-->
</div>
<!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush5">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading5">
<a href="03-sql-joins.html">4. Combining Data With Joins</a>
</div>
<!--/div.accordion-header-->
</div>
<!--/div.accordion-item-->
</div>
<!--/div.accordion-flush-->
</div>
</div>
</div>
<hr class="half-width">
<div class="accordion accordion-flush resources" id="accordionFlush12">
<div class="accordion-item">
<h2 class="accordion-header" id="flush-headingTwelve">
<button class="accordion-button collapsed" id="resources" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseTwelve" aria-expanded="false" aria-controls="flush-collapseTwelve">
RESOURCES
</button>
</h2>
<div id="flush-collapseTwelve" class="accordion-collapse collapse" aria-labelledby="flush-headingTwelve" data-bs-parent="#accordionFlush12">
<div class="accordion-body">
<ul>
<li>
<a href="key-points.html">Key Points</a>
</li>
<li>
<a href="reference.html#glossary">Glossary</a>
</li>
<li>
<a href="profiles.html">Learner Profiles</a>
</li>
<li><a href="discuss.html">Discussion</a></li>
<li><a href="reference.html">Reference</a></li>
</ul>
</div>
</div>
</div>
</div>
<hr class="half-width resources">
<a href="aio.html">See all in one page</a>
<hr class="d-none d-sm-block d-md-none">
<div class="d-grid gap-1">
</div>
</div>
<!-- /div.accordion -->
</div>
<!-- /div.sidebar-inner -->
</nav>
</div>
<!-- /div.sidebar -->
</div>
<!-- /div.sidebar-col -->
<!-- END: inst/pkgdown/templates/navbar.html-->
<!-- START: inst/pkgdown/templates/content-extra.html -->
<div class="col-xl-8 col-lg-12 primary-content">
<main id="main-content" class="main-content"><div class="container lesson-content">
<h1>Instructor Notes</h1>
<section id="learning-objectives"><h2 class="section-heading">Learning objectives<a class="anchor" aria-label="anchor" href="#learning-objectives"></a>
</h2>
<hr class="half-width">
<ul>
<li>Understand concept of relational database</li>
<li>Be able to perform simple queries in SQL from a single table</li>
<li>Understand how to filter and sort results from a query</li>
<li>Use aggregate functions to combine data</li>
<li>Perform queries across tables</li>
</ul></section><section id="data"><h2 class="section-heading">Data<a class="anchor" aria-label="anchor" href="#data"></a>
</h2>
<hr class="half-width">
<p>As for all of the <a href="https://github.com/datacarpentry?utf8=%E2%9C%93&query=ecology" class="external-link">Data
Carpentry ecology lessons</a>, this lesson uses the Portal Project
Teaching Database. The data is available at
<a href="https://doi.org/10.6084/m9.figshare.1314459" class="external-link"></a><a href="https://doi.org/10.6084/m9.figshare.1314459" class="external-link">https://doi.org/10.6084/m9.figshare.1314459</a>
and the download includes a SQLite database file (portal_mammals.sqlite)
as well as three .csv files (species.csv, plots.csv, surveys.csv) that
can be imported into SQLite.</p>
<p><strong>Note</strong> that the figshare download is an archive (.zip)
file that rudely explodes all of the files into your current
directory.</p>
</section><section id="motivation-and-framing"><h2 class="section-heading">Motivation and Framing<a class="anchor" aria-label="anchor" href="#motivation-and-framing"></a>
</h2>
<hr class="half-width">
<p>See this slide deck as a sample intro for the lesson: <a href="https://speakerdeck.com/christinalk/data-carpentry-sql-introduction" class="external-link">SQL
Intro Deck</a></p>
<p>Key points:</p>
<ul>
<li>Want to query data, instead of editing directly</li>
<li>Need a solution that is scalable and reproducible</li>
<li>Introduce the typical ways of dealing with rectangular data
(subsetting, split-apply-combine)</li>
</ul>
<p>If you’ve written up a diagram of the data analysis pipeline (raw
data -> clean data -> import and analyze -> results ->
visualization), it can be helpful to identify that you’re now somewhere
between clean data and analysis.</p>
</section><section id="common-difficult-concepts"><h2 class="section-heading">Common Difficult Concepts<a class="anchor" aria-label="anchor" href="#common-difficult-concepts"></a>
</h2>
<hr class="half-width">
<ul>
<li>Making the leap from a research question to a query (seen in some of
the challenges)</li>
<li>Data import
<ul>
<li>Not necessarily a concept, but we always have at least a handful of
people that struggle with the table import step – especially in the
virtual context with window changing</li>
<li>Data type options in SQLite (Integer, text, blob, real, numeric)
when importing from CSV. (Maybe have a table of SQLite date types in the
student material).</li>
</ul>
</li>
<li>Good to make sure that a comparison is drawn between joins in
different languages, e.g. SQL vs tidyverse</li>
<li>HAVING, and why it’s different to WHERE…. - especially when teaching
online</li>
<li>given the dataset is <em>relatively</em> complex, some students (and
instructors) find it difficult to remember what data is where,
especially when they’re from a totally different domain</li>
<li>How NULLs behave in different circumstances (when did a NULL change
your result and how do you know?)</li>
</ul></section><section id="lesson-outline"><h2 class="section-heading">Lesson outline<a class="anchor" aria-label="anchor" href="#lesson-outline"></a>
</h2>
<hr class="half-width">
<div class="section level3">
<h3 id="sql-introduction">00-sql-introduction<a class="anchor" aria-label="anchor" href="#sql-introduction"></a>
</h3>
<ul>
<li>Introduce relational databases, database management systems, DB
Browser for SQLite, and the Portal dataset</li>
<li>Import .csv files into sqlite</li>
<li>Structuring data for database import</li>
<li>Discuss the different SQL data types</li>
</ul>
<p><strong>Tips</strong></p>
<ul>
<li>
<strong>Importing data</strong>: Note how cleanly the csv files
import into SQL. If you have also taught the spreadsheet lesson, it
would be a good idea to compare the format of the csv files with the
messy spreadsheet and ask “Remember that messy spreadsheet? What would
have happened if we tried to load that in to SQL?”</li>
</ul>
</div>
<div class="section level3">
<h3 id="supplement-database-design-md">00-supplement-database-design.md<a class="anchor" aria-label="anchor" href="#supplement-database-design-md"></a>
</h3>
<p>(optional) The first lesson includes a brief introduction to data
design and choosing database systems. This material expands on the
database design in the first section.</p>
</div>
<div class="section level3">
<h3 id="sql-basic-queries">01-sql-basic-queries<a class="anchor" aria-label="anchor" href="#sql-basic-queries"></a>
</h3>
<ul>
<li>Write basic queries using SELECT and FROM</li>
<li>Filter results using DISTINCT, WHERE</li>
<li>Change how results are displayed using ORDER BY and by doing
calculations on results</li>
</ul>
</div>
<div class="section level3">
<h3 id="sql-aggregation">02-sql-aggregation<a class="anchor" aria-label="anchor" href="#sql-aggregation"></a>
</h3>
<ul>
<li>Combine results using COUNT and GROUP BY</li>
<li>Filtering based on aggregation with HAVING</li>
<li>Saving queries using VIEW</li>
</ul>
</div>
<div class="section level3">
<h3 id="sql-joins-aliases">03-sql-joins-aliases<a class="anchor" aria-label="anchor" href="#sql-joins-aliases"></a>
</h3>
<ul>
<li>combining data from two tables using JOIN, ON, USING</li>
<li>depending on time, could introduce different types of JOINs</li>
<li>using aliases with AS to simplify queries</li>
</ul>
</div>
</section><section id="alternative-activities"><h2 class="section-heading">Alternative activities<a class="anchor" aria-label="anchor" href="#alternative-activities"></a>
</h2>
<hr class="half-width">
<div class="section level3">
<h3 id="queries-on-the-board">Queries on the board<a class="anchor" aria-label="anchor" href="#queries-on-the-board"></a>
</h3>
<p>As you teach the lesson, it can be helpful to pause and write up the
query keywords on the board. This could look like this:</p>
<ul>
<li>After 01-sql-basic queries</li>
</ul>
<pre><code>SELECT column
FUNCTION(column)
FROM table
WHERE (conditional statement, applies to row values)
(AND/OR)
ORDER BY column/FUNCTION(column) (ASC/DESC)</code></pre>
<ul>
<li>After 02-sql-aggregation</li>
</ul>
<pre><code>SELECT column
FUNCTION(column)
AGGREGATE_FUNCTION(column)
FROM table
WHERE (conditional statement, applies to row values)
(AND/OR)
(IS (NOT) NULL)
GROUP BY column
HAVING (conditional statement, applies to group)
ORDER BY column/FUNCTION(column) (ASC/DESC)</code></pre>
<ul>
<li>After 03-sql-joins-aliases</li>
</ul>
<pre><code>SELECT column
FUNCTION(column)
AGGREGATE_FUNCTION(column)
FROM table
JOIN table ON table.col = table.col
WHERE (conditional statement, applies to row values)
(AND/OR)
(IS (NOT) NULL)
GROUP BY column
HAVING (conditional statement, applies to group)
ORDER BY column/FUNCTION(column) (ASC/DESC)</code></pre>
<p>As a bonus, if you can leave this on the board, it translates nicely
into the <code>dplyr</code> portion of the <code>R</code> lesson,
i.e.:</p>
<pre><code>SQL: dplyr:
SELECT column select(col)
FUNCTION(column) mutate(col = fcn(col))
AGGREGATE_FUNCTION(column) summarize(col = fcn(col))
FROM table
JOIN table ON table.col = table.col
WHERE (conditional statement, applies to row values) filter(condition)
(AND/OR)
(IS (NOT) NULL) is.na()
GROUP BY column group_by(col)
HAVING (conditional statement, applies to group)
ORDER BY column/FUNCTION(column) (ASC/DESC) arrange()</code></pre>
</div>
<div class="section level3">
<h3 id="interactive-database">“Interactive” database<a class="anchor" aria-label="anchor" href="#interactive-database"></a>
</h3>
<p>If you want to try something more active (esp. if you’re teaching SQL
in the afternoon!), this is a an interactive activity to try.</p>
<ul>
<li>Give each student six cards. Four of the cards should have the
following labels:
<ul>
<li>name</li>
<li>height</li>
<li>dept</li>
<li>DoC</li>
<li>The remaining two cards should be blank (for now!)</li>
</ul>
</li>
<li>Have students fill out their cards:
<ul>
<li>name: first name</li>
<li>height: height in INCHES</li>
<li>dept: department (if none, just pick one)</li>
<li>DoC: choose from <code>Dog</code>, <code>Cat</code>,
<code>Both</code>, <code>Neither</code>, or leave blank</li>
</ul>
</li>
<li>Each student is now a <em>record</em> in an interactive “students”
database, where each of the cards they hold is a <em>field</em> in that
database.</li>
<li>At various points in the lesson, stop and “query” the student
database. To do this:
<ul>
<li>On a slide (or in a text editor), show or type in a sample query.
Something like:</li>
</ul>
<pre><code>SELECT name, dept FROM students WHERE height > 66;</code></pre>
<ul>
<li>If the query applies to a record (student), that student should
stand, and display (hold up) the appropriate field (card)</li>
<li>For some queries, the student may have to fill in a blank card with
calculated data. For example:</li>
</ul>
<pre><code>SELECT name, name, height*2.54 AS height_cm FROM students;</code></pre>
<ul>
<li>See the following slide deck for a list of sample queries. <a href="https://speakerdeck.com/christinalk/query-live-database" class="external-link">Sample
queries</a>
</li>
</ul>
</li>
</ul>
</div>
</section><section id="exercise-solutions"><h2 class="section-heading">Exercise Solutions<a class="anchor" aria-label="anchor" href="#exercise-solutions"></a>
</h2>
<hr class="half-width">
<div class="section level5">
<h5 id="warning">WARNING<a class="anchor" aria-label="anchor" href="#warning"></a>
</h5>
<p>This file may not be always up to date with regards to the exact
exercises instructions and the naming of the columns and tables in the
database. Check before you run the workshop!</p>
<p><strong>EXERCISE</strong></p>
<p>Write a query that returns the year, month, day, species ID and
weight (in mg).</p>
<p><strong>SOLUTION</strong></p>
<pre><code>SELECT day, month, year, species_id, weight * 1000
FROM surveys;</code></pre>
<p><strong>EXERCISE</strong></p>
<p>Write a query that returns the day, month, year, species ID, and
weight (in kg) for individuals caught on Plot 1 that weigh more than 75
g.</p>
<p><strong>SOLUTION</strong></p>
<pre><code>SELECT day, month, year, species_id, weight / 1000
FROM surveys
WHERE plot_id = 1
AND weight > 75;</code></pre>
<p><strong>EXERCISE</strong></p>
<p>Write a query that returns the day, month, year, species ID, and
weight (in kg) for individuals caught on Plot 1 that weigh more than 75
g.</p>
<p><strong>SOLUTION</strong></p>
<pre><code>SELECT
surveys.day,
surveys.month,
surveys.year,
species.species_id,
surveys.weight / 1000
FROM surveys
JOIN species ON surveys.species_id = species.species_id
WHERE surveys.weight > 75
AND surveys.plot_id = 1;</code></pre>
<p><strong>EXERCISE</strong></p>
<p>Write a query that returns day, month, year, species ID for
individuals caught in January, May and July.</p>
<p><strong>SOLUTION</strong></p>
<pre><code>SELECT day, month, year, species_id
FROM surveys
WHERE month IN (1, 5, 7);</code></pre>
<p><strong>EXERCISE</strong></p>
<p>Write a query that returns year, species ID, and weight in kg from
the surveys table, sorted with the largest weights at the top.</p>
<p><strong>SOLUTION</strong></p>
<pre><code>SELECT year, species_id, weight / 1000
FROM surveys ORDER BY weight DESC;</code></pre>
<p><strong>EXERCISE</strong></p>
<p>Let’s try to combine what we’ve learned so far in a single query.
Using the surveys table write a query to display the three date fields,
species ID, and weight in kilograms (rounded to two decimal places), for
rodents captured in 1999, ordered alphabetically by the species ID.</p>
<p><strong>SOLUTION</strong></p>
<pre><code>SELECT year, month, day, species_id, ROUND(weight / 1000, 2)
FROM surveys
WHERE year = 1999
ORDER BY species_id;</code></pre>
<p><strong>EXERCISE</strong></p>
<p>Write queries that return:</p>
<ol style="list-style-type: decimal">
<li><p>How many individuals were counted in each year.</p></li>
<li><p>Average weight of each species in each year.</p></li>
</ol>
<p><strong>SOLUTION</strong></p>
<pre><code>SELECT year, COUNT(*)
FROM surveys
GROUP BY year;
SELECT year, species_id, ROUND(AVG(weight), 2)
FROM surveys
GROUP BY year, species_id;</code></pre>
<p><strong>EXERCISE</strong></p>
<p>Write a query that returns the number of each species caught in each
year sorted from most often caught species to the least occurring ones
within each year starting from the most recent records.</p>
<p><strong>SOLUTION</strong></p>
<pre><code>SELECT year, species_id, COUNT(*)
FROM surveys
GROUP BY year, species_id
ORDER BY year DESC, COUNT(*) DESC;</code></pre>
<p><strong>EXERCISE</strong></p>
<p>Write a query that returns the genus, the species, and the weight of
every individual captured at the site.</p>
<p><strong>SOLUTION</strong></p>
<pre><code>SELECT species.genus, species.species_id, surveys.weight
FROM surveys
JOIN species ON surveys.species_id = species.species_id;</code></pre>
<p><strong>EXERCISE</strong></p>
<p>Write a query that returns the number of genus of the animals caught
in each plot in descending order.</p>
<p><strong>SOLUTION</strong></p>
<pre><code>SELECT surveys.plot_id, species.genus, COUNT(*)
FROM surveys
JOIN species ON surveys.species_id = species.species_id
GROUP BY species.genus, surveys.plot_id
ORDER BY surveys.plot_id, COUNT(*) DESC</code></pre>
</div>
</section><section id="aggregate-instructor-notes"></section>
</div>
</main>
</div>
<!-- END : inst/pkgdown/templates/content-extra.html -->
</div>
<!--/div.row-->
<footer class="row footer mx-md-3"><hr>
<div class="col-md-6">
<p>This lesson is subject to the <a href="CODE_OF_CONDUCT.html">Code of Conduct</a></p>
<p>
<a href="https://github.com/datacarpentry/sql-ecology-lesson/edit/main/README.md" class="external-link">Edit on GitHub</a>
| <a href="https://github.com/datacarpentry/sql-ecology-lesson/blob/main/CONTRIBUTING.md" class="external-link">Contributing</a>
| <a href="https://github.com/datacarpentry/sql-ecology-lesson/" class="external-link">Source</a></p>
<p><a href="https://github.com/datacarpentry/sql-ecology-lesson/blob/main/CITATION" class="external-link">Cite</a> | <a href="mailto:[email protected]">Contact</a> | <a href="https://carpentries.org/about/" class="external-link">About</a></p>
</div>
<div class="col-md-6">
<p>Materials licensed under <a href="LICENSE.html">CC-BY 4.0</a> by the authors</p>
<p><a href="https://creativecommons.org/licenses/by-sa/4.0/" class="external-link">Template licensed under CC-BY 4.0</a> by <a href="https://carpentries.org" class="external-link">The Carpentries</a></p>
<p>Built with <a href="https://github.com/carpentries/sandpaper/tree/0.14.0" class="external-link">sandpaper (0.14.0)</a>,
<a href="https://github.com/carpentries/pegboard/tree/0.7.1" class="external-link">pegboard (0.7.1)</a>,
and <a href="https://github.com/carpentries/varnish/tree/0.3.1" class="external-link">varnish (0.3.1)</a>.</p>
</div>
</footer>
</div> <!-- / div.container -->
<div id="to-top">
<a href="#top">
<i class="search-icon" data-feather="arrow-up" role="img" aria-label="Back to top"></i><br><span class="d-none d-sm-none d-md-none d-lg-none d-xl-block">Back</span> To Top
</a>
</div>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "TrainingMaterial",
"@id": "https://datacarpentry.github.io/sql-ecology-lesson/instructor-notes.html",
"dct:conformsTo": "https://bioschemas.org/profiles/TrainingMaterial/1.0-RELEASE",
"description": "A Carpentries Lesson teaching foundational data and coding skills to researchers worldwide",
"keywords": "software, data, lesson, The Carpentries",
"name": "Instructor Notes",
"creativeWorkStatus": "active",
"url": "https://datacarpentry.github.io/sql-ecology-lesson/instructor-notes.html",
"identifier": "https://datacarpentry.github.io/sql-ecology-lesson/instructor-notes.html",
"dateCreated": "2015-02-10",
"dateModified": "2023-10-10",
"datePublished": "2023-10-10"
}
</script><script>
feather.replace();
</script><!-- Matomo
2022-11-07: we have gotten a notification that we have an overage for our
tracking and I'm pretty sure this has to do with Workbench usage.
Considering that I am not _currently_ using this tracking because I do not
yet know how to access the data, I am turning this off for now.
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(["setDocumentTitle", document.domain + "/" + document.title]);
_paq.push(["setDomains", ["*.preview.carpentries.org","*.datacarpentry.github.io","*.datacarpentry.org","*.librarycarpentry.github.io","*.librarycarpentry.org","*.swcarpentry.github.io", "*.carpentries.github.io"]]);
_paq.push(["setDoNotTrack", true]);
_paq.push(["disableCookies"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://carpentries.matomo.cloud/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '1']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src='https://cdn.matomo.cloud/carpentries.matomo.cloud/matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
End Matomo Code -->
</body>
</html><!-- END: inst/pkgdown/templates/layout.html-->