-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathslides_abridged.html
More file actions
1155 lines (880 loc) · 42.4 KB
/
slides_abridged.html
File metadata and controls
1155 lines (880 loc) · 42.4 KB
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Ladies Learning Code</title>
<link rel="stylesheet" href="framework/css/inspire.css" data-noprefix>
<link rel="stylesheet" href="framework/css/fonts.css" data-noprefix>
<link rel="stylesheet" href="framework/css/highlightjs/github.css" data-noprefix>
<link rel="stylesheet" href="framework/css/styles.css" data-noprefix>
<link rel="stylesheet" href="framework/css/a11y/plugin.css" data-noprefix>
<link rel="shortcut icon" href="framework/img/favicon.ico">
<!-- Takes care of CSS3 prefixes -->
<script src="framework/scripts/prefixfree.min.js"></script>
<!-- opens all links in a new window -->
<base target="_blank">
</head>
<body class="en">
<section>
<article class="slide welcome highlight">
<h1><img class="logo-stacked" src="framework/img/llc-logo-stacked-white.png" alt="Ladies Learning Code logo">Welcome!</h1>
<div class="instructions">
<h2>Open,<br>Download <br>or Install</h2>
<ol class="downloads">
<!--<li>Learner files (zip file): <a href="https://github.com/ladieslearningcode/llc-sql/archive/master.zip">http://bit.ly/clc-sql</a>
<ul>
<li>unzip the learner file (<em>extract all</em> if you’re on a PC)</li>
<li>open <em>slides_abridged.html</em> in the browser to view the slides</li>
</ul>
</li>-->
<li>Slides: <a href="https://ladieslearningcode.github.io/llc-sql/slides_abridged.html">https://ladieslearningcode.github.io/llc-sql/slides_abridged.html</a></li>
<li>Data file: <a href="https://bit.ly/cnf_db">https://bit.ly/cnf_db</a></li>
<li>SQLite Browser: <a href="https://sqliteonline.com/">https://sqliteonline.com/</a></li>
</ol>
<h2>New to Zoom?</h2>
<ul class="downloads">
<li><a href="https://bit.ly/en-zoom-main">https://bit.ly/en-zoom-main</a></li>
</ul>
</div>
<footer>
<a class="left" rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/"><img alt="Creative Commons License" src="framework/img/cc-by-nc.png" /></a>
<p class="left">Content created by <a href="https://ca.linkedin.com/in/matt-baum">Matthew Baum</a> for <a prefix="cc: http://creativecommons.org/ns#" href="http://ladieslearningcode.com" property="cc:attributionName" rel="cc:attributionURL">Ladies Learning Code</a></p>
<p class="right">Use the left <span class="arrow">←</span> and right <span class="arrow">→</span> arrow keys to navigate</p>
</footer>
</article>
<article class="slide intro">
<img class="logo" src="framework/img/llc-logo-white.png" alt="Ladies Learning Code logo">
<h1 class="heading-bg">
<span>Managing Data with SQL</span>
</h1>
<ul>
<li><a href="http://twitter.com/learningcode">@learningcode</a></li>
<li><a href="https://twitter.com/search?q=%23ladieslearningcode&src=typd">#ladieslearningcode</a></li>
</ul>
<!-- INSTRUCTOR DETAILS -->
<!--<img class="instructor" src="http://placehold.it/225x225" alt="Instructor Name">
<h2><span class="cursive">with</span> Instructor name</h2>
<ul>
<li><a href="mailto:">hello@email.com</a></li>
<li><a href="http://website.com">website.com</a></li>
<li><a href="http://twitter.com/">@twitter</a></li>
</ul>-->
<!--<div class="sponsor">
<p>In partnership with<br> <img src="framework/img/google-org-logo.svg" alt="Google.org"></p>
</div>-->
<footer>
<a class="left" rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/"><img alt="Creative Commons License" src="framework/img/cc-by-nc.png" /></a>
<p class="left">Content created by <a href="https://ca.linkedin.com/in/matt-baum">Matthew Baum</a> for <a prefix="cc: http://creativecommons.org/ns#" href="http://ladieslearningcode.com" property="cc:attributionName" rel="cc:attributionURL">Ladies Learning Code</a></p>
<p class="right">Use the left <span class="arrow">←</span> and right <span class="arrow">→</span> arrow keys to navigate</p>
</footer>
</article>
</section>
<header class="slide title" data-toc>
<h2>Introductions</h2>
</header>
<section>
<article class="slide" data-markdown>
<script type="text/template">
#Today's Project
Data is ever present in our lives and, as we'll see today, SQL is a powerful language that allows us to extract useful insights from large quantities of information.
We'll be working with real data from the Canadian government, called the [Canadian Nutrient File (CNF)](https://www.canada.ca/en/health-canada/services/food-nutrition/healthy-eating/nutrient-data.html)!
This database contains nutritional information on over 5000 foods published by Health Canada.
We'll start with SQL basics and then explore the data through some fun challenges.
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
#How might you use SQL?
SQL is used in the databases serving many business roles, including:
* Customer Support
* Marketing
* Sales
* Finance
* Have you heard about Business Intelligence (BI)?
> What do **you** think? What motivated you to join us today?
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
#What we'll cover today
1. What are data, databases and SQL?
1. Searching for text and keywords
1. Finding the data we want
1. Ordering data logically
1. Grouping data into categories
1. Calculating simple statistics
1. Joining together related data
</script>
</article>
<section class="slide" data-markdown>
<script type="text/template">
# Getting started with Zoom

</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
# Getting started with Zoom

</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
# Getting started with Zoom

</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
# Getting started with Zoom

</script>
</section>
<article class="slide" data-markdown>
<script type="text/template">
# Coders' code
1. Be friendly and welcoming
1. Be respectful of others by choosing words and actions with care
1. Be mindful of how much space you take up in conversations
1. Ask questions
1. Take care of your body
1. No photos or audio/video recordings
</script>
</article>
<section class="slide title" data-markdown>
<script type="text/template">
#Land acknowledgement
</script>
</section>
<article class="slide" data-markdown>
<script type="text/template">
#Getting Set Up
1. Open SQLite in your web browser ([https://sqliteonline.com/](https://sqliteonline.com/))
1. Click the book icon at the bottom right to hide the right pane

1. Click "File" and "Open DB"

1. Navigate to /data/cnf.db <!--inside the course folder you unzipped-->
1. Click "Open"
</script>
</article>
<article class="slide" data-markdown>
<script type="text/template">
You should see a list of tables on the right of your screen:

</script>
</article>
<article class="slide" data-markdown>
<script type="text/template">
#Good to go!
Now that we're set up, you can switch back to the slides.
We'll come back to the browser later.
</script>
</article>
</section>
<section>
<header class="slide title" data-toc>
<h1>Data</h1>
</header>
<article class="slide" data-markdown>
<script type="text/template">
#What is data?
Information technology and data are everywhere around us. In fact, more information is generated each year than was
ever produced by humanity prior to the Internet.
But, **what exactly *is* data?**
What do you think (share with the group)?
</script>
</article>
<article class="slide" data-markdown>
<script type="text/template">
#Data is
* units of information/facts (people's ages, the prices of homes, etc.)
* can be structured (a shopping list, a credit card statement, etc.)
* can have a **type** (a number, some text, etc.)
* digital data: can be encoded and stored on a computer, and sent between them
</script>
</article>
<article class="slide" data-markdown>
<script type="text/template">
# Uses for Data
Now that we have an idea of what data is, can you think of any common uses of data
in your daily life?
* Email: Sending and receiving textual data<!-- .element: class="delayed" -->
* Excel: Storing and analyzing data in tables<!-- .element: class="delayed" -->
* Voice calls: Exchanging audio data via phone/the Internet<!-- .element: class="delayed" -->
* Video streaming: Reading sequences of image data<!-- .element: class="delayed" -->
</script>
</article>
<article class="slide" data-markdown>
<script type="text/template">
# Decolonizing Data
It is important to critically assess data by considering its source: where does this data come from? Who is controlling it?
Today's data is not exempt from bias and colonialism. Nutrition policy in Canada was informed by the scientist who authored Canada's initial Food Rules, who used data which exploited Indigenous children in residential schools by performing experiments on them.
According to Abigail Echo-Hawk, "decolonizing data means that the community itself is the one determining what is the information they want us to gather." Abigail reminds us that when collecting data, we do it in a way that truly serves our communities.
<br><br>
> <strong>Reference</strong><br><ul><li><a href="https://crosscut.com/2019/05/abigail-echo-hawk-art-and-science-decolonizing-data">Abigail Echo-Hawk on the art and science of 'decolonizing data' (Crosscut)</a></li><li><a href="https://www.cbc.ca/radio/unreserved/how-food-in-canada-is-tied-to-land-language-community-and-colonization-1.5989764/the-dark-history-of-canada-s-food-guide-how-experiments-on-indigenous-children-shaped-nutrition-policy-1.5989785">The dark history of Canada's Food Guide: How experiments on Indigenous children shaped nutrition policy (CBC)</a></li><li><a href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3941673/">Canada's shameful history of nutrition research on residential school children: The need for strong medical ethics in Aboriginal health research.</a></li></ul>
</script>
</article>
<article class="slide" data-markdown>
<script type="text/template">
#Manipulating Data
The various ways in which we can manipulate data are best remembered with the acronym CRUD:
* **C**reate
* **R**ead
* **U**pdate
* **D**elete
</script>
</article>
<article class="slide" data-markdown>
<script type="text/template">

</script>
</article>
</section>
<section>
<header class="slide title" data-toc>
<h1>Databases</h1>
</header>
<article class="slide" data-markdown>
<script type="text/template">
#What is a Database?
By this point, we have an idea of what data is, but what is a database?
* Structured data that can be queried (we can ask questions of the data)<!-- .element: class="delayed" -->
* Related data (data are linked. Ex.: a person and their job details)<!-- .element: class="delayed" -->
<div>
> There are others, but today we will learn about a common type called **relational databases!**
</div><!-- .element: class="delayed" -->
</script>
</article>
<article class="slide" data-markdown>
<script type="text/template">
#Data in a Table
Here's an example of data on People, identified by social insurance numbers

</script>
</article>
<article class="slide" data-markdown>
<script type="text/template">
#Relational Databases
* **Similar** data is stored in a **table**, called a **relation** (ex.: a Person table)
* Table **columns** describe **attributes** of the data (ex.: Person's name)
* Table **rows** represent a **data object** (ex.: an individual person)
* Rows are uniquely identified by an **ID** (ex.: social insurance number)
* These IDs are known as **primary keys**
</script>
</article>
<article class="slide" data-markdown>
<script type="text/template">
#Today's Database
We'll be working with **SQLite**, a simple relational database management system supporting the SQL language.
This is a free tool requiring little installation and commonly used by apps on iOS and Android.
It's a good learning tool and everything you learn will transfer to other SQL-based systems.
Now, let's dig into the SQL language!
</script>
</article>
</section>
<section>
<header class="slide title" data-toc>
<h1>SQL</h1>
</header>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Who Uses SQL?
SQL is the **most widely used** database query language today and **millions of companies** and products rely on it.
Here are a few you may know:
* Facebook
* Youtube
* Netflix
* Uber
* Twitter
Source: <a href="https://www.mysql.com/customers/industry/?id=">MySQL Clients</a>
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# That's right! SQL everywhere!

</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# SQL intro
With a general understanding of databases, it's time to make use of our data!
**S**tructured **Q**uery **L**anguage (**SQL**) lets us define, modify and read our data (CRUD).
We'll focus on the part of SQL that asks questions of existing data (querying).
Let's see what that looks like!
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Anatomy of a SQL Query
A "question" to the database, formed of **clauses** (like individual sentences).
Clauses include SQL **keywords** with **column** and **table** names to locate the data.
1. `SELECT` starts the 1st clause, followed by the columns we want.
2. `FROM` starts the 2nd clause, followed by the tables holding the data.
3. `;` terminates the statement
> NOTE : SQL is **case insensitive**
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Example of a SELECT Query
This most basic query looks something like this:
```sql
SELECT FoodDescription
FROM FOOD_NAME;
```
This returns the value of `FoodDescription` for **every row** in `FOOD_NAME`.
> But what if we want all of the columns in the table? Do we have to know each of the column names?
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# SQL Wildcards
**Wildcards** are special characters (letters, numbers, etc.) that can replace any other character in a query. They're like cards that can take on any value in a card game.
We'll see two wildcards today, the first is:
* `*`, which is typically used to match any column name in a `SELECT` clause
```sql
SELECT *
FROM table_name;
```
> Other more precise wildcards exist, as described [here](https://www.w3schools.com/sql/sql_wildcards.asp).
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Exercise 1
1. Type the following query and click 
```sql
SELECT *
FROM FOOD_NAME;
```
You should see a table in the result pane below where you entered the query:
<!-- .element: class="delayed" -->
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# The WHERE Clause
What if we want to get only specific entries from our data?
The `WHERE` clause **can** follow the `FROM` clause, and allows to do this, like so:
```sql
SELECT *
FROM FOOD_NAME
WHERE column operator value;
```
Where:
1. **column** is the attribute we want to test
1. **operator** is the type of comparison (ex.: `=`, `<`, `>`, `<=`, `>=`)
1. **value** is the value we want to match (ex.: a **number**)
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Exercise 2a
Let's refine our previous query by adding a `WHERE` clause:
Use the condition `FoodID = 5` and `SELECT` only the food with ID `5` :
```sql
SELECT *
FROM FOOD_NAME
WHERE FoodID = 5;
```
<!-- .element: class="delayed" -->
<div>
The result should look like this:

</div><!-- .element: class="delayed" -->
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Exercise 2b
Design a query to `SELECT` those foods with IDs **up to and including** `10`. List your favorite food from the results in the chat!
```sql
SELECT *
FROM FOOD_NAME
WHERE FoodID <= 10;
```
<!-- .element: class="delayed" -->
<div>
The result should look like this:

</div><!-- .element: class="delayed" -->
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Matching text with LIKE
We filtered data by comparing numbers, but what if we want to search for text?
`LIKE` exists for this:
```sql
WHERE column_name LIKE "some text here"
```
1. Use `"` around the required text
2. Use `%` to match anything (wild card)
* `"apple%"` matches a string **beginning** in `"apple"`
* `"%apple"` must **end** in `"apple"`
* `"%apple%"` must contain `"apple"` **anywhere**
* `"apple"` matches that string **exactly**
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Exercise 3
Take 5 minutes to:
1. Pick a **term** to search for in the `FoodDescription` column in the `FOOD_NAME` table
2. Experiment with selecting data by matching different strings
3. Experiment with the use of wildcards
```sql
SELECT *
FROM FOOD_NAME
WHERE FoodDescription LIKE "%apple%";
```
<!-- .element: class="delayed" -->
<!-- .element: class="delayed" -->
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Ordering Results
Data can be ordered using the `ORDER BY` clause, following the `FROM` and `WHERE` clauses:
```sql
SELECT *
FROM my_table
ORDER BY my_column (ASC or DESC)
```
Where:
* **my_column** is the column to order by
* **(ASC or DESC)**: `ASC` to sort in ascending order, or `DESC` to sort in descending order
Let's try that out!
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Exercise 4
Let's reuse our last query with `LIKE` to:
1. Sort elements by `FoodID`, from largest to smallest
2. Sort elements in ascending order on `FoodDescription`
```sql
SELECT FoodID, FoodGroupID, FoodSourceID, FoodDescription
FROM FOOD_NAME
WHERE FoodDescription LIKE "%apple%"
ORDER BY FoodID DESC;
```
<!-- .element: class="delayed" -->
<!-- .element: class="delayed" -->
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Grouping Results
The `GROUP BY` clause combines rows and calculates total values for these groups.
```sql
GROUP BY column_name
```
Combines rows with identical values in `column_name`, or "throws them in the same bucket".
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
To illustrate, the following grouping of the Professors table
places results into two buckets, but what value should appear
for the value of Salary in each group?

</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# SQL Functions
Functions return interesting stats on query results.
What might we want to know about the contents of a bucket grouping together many rows?
<ul>
<li>The **number of** elements in the bucket: `COUNT(*)`</li><!-- .element: class="delayed" -->
<li>The **average value** of a column: `AVG(column)`</li><!-- .element: class="delayed" -->
<li>The **sum** of a column: `SUM(column)`</li><!-- .element: class="delayed" -->
<li>The **smallest** value in a column: `MIN(column)`</li><!-- .element: class="delayed" -->
<li>The **largest** value in a column: `MAX(column)`</li><!-- .element: class="delayed" -->
</ul>
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Back to GROUP BY
How can we determine how many foods exist in each food group?
```sql
SELECT FoodGroupID, COUNT(*) AS "Foods in Group"
FROM FOOD_NAME
GROUP BY FoodGroupID;
```
Columns that appear **outside** of functions in `SELECT` normally appear in `GROUP BY`.
Other columns are calculations over the whole group.
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Joining Tables
We want to manipulate related data from **different** tables by **matching ID values**.
There are different ways to do this. We'll focus on `INNER JOIN` (or simply `JOIN`) :
```sql
SELECT columns
FROM table1 JOIN table2 ON table1.column = table2.column;
```
> For other joins, look up `OUTER JOIN` (`LEFT`, `RIGHT` or `FULL`) and `NATURAL JOIN`. Tables can also be joined in the `WHERE` clause.
</script>
</article>
<article class="slide" data-markdown>
<script type="text/template">
#Related Tables
Data in two tables can be related by corresponding IDs:


</script>
</article>
<article class="slide" data-markdown>
<script type="text/template">
#Relationships
Lines in two tables can be linked together using their primary keys (PK), establishing a **relationship**. For ex.:
<ol>
<li>A line in the `PERSON` table describes `Mary`. Her PK is her SIN: `123456789`</li><!-- .element: class="delayed" -->
<li>A line in the `CITY` table describes `Montreal`. Its PK is the code: `YUL`</li><!-- .element: class="delayed" -->
<li>Values in the `HOMETOWN` column of `PERSON` **match** PKs from `CITY`</li><!-- .element: class="delayed" -->
<li>In Mary's case the value is `YUL`</li><!-- .element: class="delayed" -->
<li>Thus Mary's data links to her hometown data through this **relationship**</li><!-- .element: class="delayed" -->
</ol>
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Visualizing JOIN
This Venn diagram illustrates the `INNER JOIN` operation, which returns **only** those rows that have **matching values** in the chosen column in **both tables**:

> Diagrams for other types of JOINs can be seen [here](https://www.w3schools.com/sql/sql_join.asp).
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Exercise 5: Prep
In the left pane of SQLite Browser, expand the following tables to view their columns:
1. `NUTRIENT_NAME`
1. `NUTRIENT_AMOUNT`

> Which column do they have in common that would allow us to `JOIN` them on matching values?
Answer: `NutrientID`<!-- .element: class="delayed" -->
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Exercise 5: The JOIN statement
Let's join the `NUTRIENT_NAME` and `NUTRIENT_AMOUNT` tables, to get nutrient breakdown for food ID `20` (brie cheese):
```sql
SELECT NutrientName, NutrientValue
FROM NUTRIENT_NAME
JOIN NUTRIENT_AMOUNT ON NUTRIENT_NAME.NutrientID = NUTRIENT_AMOUNT.NutrientID
WHERE NUTRIENT_AMOUNT.FoodID = 20;
```
<!-- .element: class="delayed" -->
<!-- .element: class="delayed" -->
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Joining more than two tables
We can join more than two tables at a time.
Let's practice by joining 3 tables to get the names of the nutrients:
```sql
SELECT NutrientName, NutrientValue, FoodDescription
FROM NUTRIENT_NAME
JOIN NUTRIENT_AMOUNT ON NUTRIENT_NAME.NutrientID = NUTRIENT_AMOUNT.NutrientID
JOIN FOOD_NAME ON FOOD_NAME.FoodID = NUTRIENT_AMOUNT.FoodID
WHERE FOOD_NAME.FoodID = 20;
```
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Table Aliases
A query with many repeating table names can be simplified as follows:
```sql
SELECT NutrientName, NutrientValue, FoodDescription
FROM NUTRIENT_NAME nn
JOIN NUTRIENT_AMOUNT na ON nn.NutrientID = na.NutrientID
JOIN FOOD_NAME fn ON fn.FoodID = na.FoodID
WHERE fn.FoodID = 20;
```
> Notice the use of table name **aliases** (**fn**, **na** and **nn**) to shorten the query.
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Stretch Goal: Exercise 6 (optional)
Let's get a bit more practice joining tables:
1. Take a look at the `FOOD_NAME`, `REFUSE_AMOUNT` and `REFUSE_NAME` tables
2. How can these tables be joined?
3. Select a food you like
4. Write a query to print the amounts of each type of refuse (waste) produced by a portion of that food
5. Each line should list: the **food name**, the **type of refuse**, the **amount of refuse**
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Exercise 6: Solutions
Your query should look like:
```sql
SELECT FoodDescription, RefuseDescription, RefuseAmount
FROM FOOD_NAME fn
JOIN REFUSE_AMOUNT ra ON fn.FoodID = ra.FoodID
JOIN REFUSE_NAME rn ON ra.RefuseID = rn.RefuseID
WHERE fn.FoodID = 600;
```
<!-- .element: class="delayed" -->
<div>
The result should look like:

</div><!-- .element: class="delayed" -->
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# SQL Math
Finally, let's look at running some math on column values before returning a result!
This is useful to change a number column for reporting.
The common math operators are:
* Addition: `+`
* Subtraction: `-`
* Multiplication: `*`
* Division: `/`
* Modulo: `%` (remainder after integer division)
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
# Math Example
CNF lists nutrients for a portion of 100g. What if we want the amount/gram?
We can get nutrients/gram for brie cheese by modifying our query as follows:
```sql
SELECT NutrientName, (NutrientValue/100) AS "Nutrient per gram"
FROM FOOD_NAME fn
JOIN NUTRIENT_AMOUNT na ON fn.FoodID = na.FoodID
JOIN NUTRIENT_NAME nn ON na.NutrientID = nn.NutrientID
WHERE fn.FoodID = 20;
```
<!-- .element: class="delayed" -->
<div>
> We used a fixed number here, but we could have used another column in the calculation.
</div><!-- .element: class="delayed" -->
</script>
</article>
</section>
<section>
<header class="slide title" data-toc>
<h1>Challenge Problems</h1>
</header>
<article class="slide small-type" data-markdown>
<script type="text/template">
#Challenge Intro
The world of nutrition can be confusing!
As an expert in the field, you rely on data to guide the colorful characters that show up to your practice with concerns.
Try your hand at solving the following problems **with a single query** to test your SQL skills.
Don't forget to have fun!
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
#Challenge 1: Critical Groups
Ms. Flamm Boyant, France's foremost food critic is visiting your city!
She has heard of Canada's intriguing "food groups" and her request is simple.
She requires:
* A list of the food groups available
* The number of foods in each group
* The groups listed from largest (most foods) to smallest
> Find her this data before she writes a bad review on social media!
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
#Challenge 1 Solution
```sql
SELECT FoodGroupName, COUNT(*) AS FoodCount
FROM FOOD_NAME fn JOIN FOOD_GROUP fg
ON fn.FoodGroupID = fg.FoodGroupID
GROUP BY fg.FoodGroupID
ORDER BY FoodCount DESC;
```
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
#Challenge 2: I Want my Greens
Brock O. Lee is adopting a new low-carb diet.
He requires foods matching the following criteria:
* Must be a **vegetable**
* Must include the word **green** in its description
* Must have **less than 10 grams of carbs** for a 100 gram portion
Your client wants a simple table listing all of these foods with the name of the food and the grams of carbs, from least to most.
> **Hint**: What is the food group for vegetables? What is the nutrient ID for carbs? Which 2 tables should you join in this query?
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
#Challenge 2 Solution
```sql
SELECT FoodDescription, NutrientValue AS "Grams of Carbs"
FROM FOOD_NAME fn JOIN NUTRIENT_AMOUNT na ON fn.FoodID = na.FoodID
WHERE FoodGroupID = 11
AND FoodDescription LIKE "%green%"
AND NutrientID = 205
AND NutrientValue < 10
ORDER BY NutrientValue ASC;
```
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
#Challenge 3: Zigzag Meal Plan
Amanda A. Appleton has always hated the letter "z".
This unusual phobic state has now resulted in a health condition, as she suffers from a zinc deficiency!
A woman her age requires `8mg` of zinc daily.
Her doctor recommends the following treatment:
* Eat foods providing **at least half** the daily requirement per 100g portion
* Eat foods containing the letter "z" n their descriptions, to desensitize yourself from the phobia
> Run a quick query to provide her with a list of suitable foods!
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
#Challenge 3 Solution
```sql
SELECT FoodDescription, NutrientValue
FROM FOOD_NAME fn JOIN NUTRIENT_AMOUNT na
ON fn.FoodID = na.FoodID
WHERE NutrientID = 309
AND NutrientValue >= 4
AND FoodDescription LIKE '%z%';
```
</script>
</article>
<article class="slide small-type" data-markdown>
<script type="text/template">
#Challenge 4: No Leftovers!
Your highschool math teacher bursts through the door!