-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclass4.html
646 lines (583 loc) · 21.9 KB
/
class4.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Intro to Databases - Girl Develop It</title>
<meta name="description" content="Introduction to Databases.
The course is meant to be taught in four two-hour workshops.">
<meta name="author" content="Girl Develop It">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/gdicool.css" id="theme">
<!-- For syntax highlighting -->
<!-- light editor--><link rel="stylesheet" href="lib/css/light.css">
<!-- dark editor<link rel="stylesheet" href="lib/css/dark.css">-->
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="css/print/pdf.css" type="text/css" media="print">
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h3>Introduction to Databases</h3>
<h4>Class 4 - Joins & More</h4>
<img src="images/gdi_logo_badge.png" alt="" />
</section>
<!-- Welcome-->
<section>
<h3>Welcome!</h3>
<div class = "left-align">
<p>Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.</p>
<p class ="green">Some "rules"</p>
<ul>
<li>We are here for you!</li>
<li>Every question is important</li>
<li>Help each other</li>
<li>Have fun</li>
</ul>
</div>
</section>
<section>
<h3>Overview</h3>
<ul>
<!--<li>Brief Relational Algebra Overview</li>-->
<li>SQL Joins</li>
<li>Bonus Material (time permitting):
<ul>
<li>Functions</li>
<li>Indexes</li>
<li>Views</li>
<li>Triggers</li>
<li>Transactions</li>
</ul></li>
</ul>
</section>
<section>
<h3>Joining Tables</h3>
<p>Finally, the real power of SQL!</p>
<p>The SQL JOIN clause combines records from two or more tables in a database.
You will primarly use two types of joins, with some variations on these.</p>
<ul>
<li>Inner Join</li>
<li>Outer Join</li>
</ul>
</section>
<section>
<h3>Our Tables</h3>
<p>To illustrate these joins, let's say we have TableA and TableB.
We want to join these tables in a variety of ways.</p>
<pre><code contenteditable class="JavaScript">TableA TableB
================= =================
id name id name
================= =================
1 Pirate 1 Rutabaga
2 Monkey 2 Pirate
3 Ninja 3 Darth Vader
4 Spaghetti 4 Ninja
</code></pre>
</section>
<section>
<h3>INNER JOIN</h3>
<ul>
<li>The Inner Join is the most common type of join.</li>
<li>It's so common that it's the default join type for SQLite.</li>
<li>Selects only records that match both of the tables you are joining.</li>
</ul>
<pre><code contenteditable class="JavaScript">TableA TableB
================= =================
id name id name
================= =================
1 Pirate 1 Rutabaga
2 Monkey 2 Pirate
3 Ninja 3 Darth Vader
4 Spaghetti 4 Ninja
</code></pre>
</section>
<section>
<h3>Inner Join</h3>
<p>Selects only the records that match both TableA and TableB</p>
<img style="height:250px" src="images/innerjoin.png"/>
<pre><code contenteditable class="JavaScript">SELECT * from TableA
INNER JOIN TableB
ON TableA.name = TableB.name;
================= =================
id name id name
================= =================
1 Pirate 2 Pirate
3 Ninja 4 Ninja
</code></pre>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>Write a SQL Query that returns the following from the Chinook Database.</p>
<ul>
<li>Album Id</li>
<li>Album Title</li>
<li>Artist Name</li>
</ul>
</section>
<section>
<h3>Solution</h3>
<pre><code contenteditable class="JavaScript">SELECT Album.AlbumId, Album.Title as AlbumTitle,
Artist.Name as ArtistName
FROM Album
JOIN Artist
ON Album.ArtistId = Artist.ArtistId;
</code></pre>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>Write a SQL Query that returns the following from the Chinook Database.</p>
<ul>
<li>Artist Name</li>
<li>Album Title</li>
<li>Track Name</li>
<li>Genre Name</li>
<li>Composer</li>
<li>Unit Price</li>
</ul>
</section>
<section>
<h3>Solution</h3>
<pre><code contenteditable class="JavaScript">SELECT Artist.Name, Album.Title as AlbumTitle,
Track.Name as TrackName, Genre.Name,
Track.Composer, Track.UnitPrice
FROM Album
JOIN Artist
ON Album.ArtistId = Artist.ArtistId
JOIN Track
ON Track.AlbumId = Album.AlbumId
JOIN Genre
ON Genre.GenreId = Track.GenreId;
</code></pre>
</section>
<section>
<h3>FULL OUTER JOIN</h3>
<ul>
<li>The Full Outer Join produces all sets of records in TableA and TableB.</li>
<li>Records from both sides are included, where available.</li>
<li>If there is no matching record, the missing side will have a null value.</li>
<li><em>Note: Full Outer Joins are not supported by SQLite</em></li>
</ul>
<pre><code contenteditable class="JavaScript">TableA TableB
================= =================
id name id name
================= =================
1 Pirate 1 Rutabaga
2 Monkey 2 Pirate
3 Ninja 3 Darth Vader
4 Spaghetti 4 Ninja
</code></pre>
</section>
<section>
<h3>FULL OUTER JOIN</h3>
<p>Selects all sets of records from TableA and TableB.</p>
<img style="height:250px" src="images/fullouterjoin.png"/>
<pre><code contenteditable class="JavaScript">SELECT * from TableA
FULL OUTER JOIN TableB
ON TableA.name = TableB.name;
================= =================
id name id name
================= =================
1 Pirate 2 Pirate
2 Monkey null null
3 Ninja 4 Ninja
4 Spaghetti null null
null null 1 Rutabaga
null null 3 Darth Vader
</code></pre>
</section>
<section>
<h3>LEFT OUTER JOIN</h3>
<ul>
<li>The Left Outer Join produces all records from TableA, with matching records
from TableB if they are available.</li>
<li>If there is no matching record in TableB, the right side will have a null value.</li>
</ul>
<pre><code contenteditable class="JavaScript">TableA TableB
================= =================
id name id name
================= =================
1 Pirate 1 Rutabaga
2 Monkey 2 Pirate
3 Ninja 3 Darth Vader
4 Spaghetti 4 Ninja
</code></pre>
</section>
<section>
<h3>LEFT OUTER JOIN</h3>
<p>Selects all sets of records from TableA and TableB.</p>
<img style="height:250px" src="images/leftouterjoin.png"/>
<pre><code contenteditable class="JavaScript">SELECT * from TableA
LEFT OUTER JOIN TableB
ON TableA.name = TableB.name;
================= =================
id name id name
================= =================
1 Pirate 2 Pirate
2 Monkey null null
3 Ninja 4 Ninja
4 Spaghetti null null
</code></pre>
</section>
<section>
<h3>Records in TableA, but not TableB</h3>
<ul>
<li>Let's say we want to know all of the records in TableA that don't also appear in TableB.</li>
<li>We can do this by adding a WHERE clause to our left outer join.</li>
</ul>
<pre><code contenteditable class="JavaScript">TableA TableB
================= =================
id name id name
================= =================
1 Pirate 1 Rutabaga
2 Monkey 2 Pirate
3 Ninja 3 Darth Vader
4 Spaghetti 4 Ninja
</code></pre>
</section>
<section>
<h3>Records in TableA, but not TableB</h3>
<p>Remembere that the left outer join gave us null values for the right
side if there was no corresponding record in TableB. We can now use
this information to isolate the records we want.</p>
<img style="height:250px" src="images/in_a_but_not_b.png"/>
<pre><code contenteditable class="JavaScript">SELECT * from TableA
LEFT OUTER JOIN TableB
ON TableA.name = TableB.name
WHERE TableB.id IS null;
================= =================
id name id name
================= =================
2 Monkey null null
4 Spaghetti null null
</code></pre>
</section>
<section>
<h3>Records in TableA OR TableB, but not in both</h3>
<ul>
<li>Let's say we want to know all of the records in TableA or TableB,
but want to exclude records that appear in both.</li>
<li>We can do this by adding a WHERE clause to a full outer join.</li>
</ul>
<pre><code contenteditable class="JavaScript">TableA TableB
================= =================
id name id name
================= =================
1 Pirate 1 Rutabaga
2 Monkey 2 Pirate
3 Ninja 3 Darth Vader
4 Spaghetti 4 Ninja
</code></pre>
</section>
<section>
<h3>Records in TableA OR TableB, but not in both</h3>
<p>This will give us records unique to TableA OR TableB.</p>
<img style="height:250px" src="images/unique_to_a_and_b.png"/>
<pre><code contenteditable class="JavaScript">SELECT * from TableA
FULL OUTER JOIN TableB
ON TableA.name = TableB.name
WHERE TableA.id IS null
OR TableB.id IS null;
================= =================
id name id name
================= =================
2 Monkey null null
4 Spaghetti null null
null null 1 Rutabaga
null null 3 Darth Vader
</code></pre>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>Write a SQL query that returns album and track information for
tracks where the composer information is missing.</p>
</section>
<section>
<h3>Solution</h3>
<pre><code contenteditable class="JavaScript">SELECT * from Album
JOIN Track
ON Track.AlbumId = Album.AlbumId
WHERE Track.Composer IS null;
</code></pre>
</section>
<section>
<h3>Ordering Data</h3>
<p>Data from a SELECT statement can be ordered with the ORDER BY clause.</p>
<pre><code contenteditable class="JavaScript">SELECT * from Invoice
ORDER BY InvoiceDate DESC;
</code></pre>
<p>The results will be ordered from newest to oldest. Following the
keyword with ASC gives us the reverse order.</p>
</section>
<section>
<h3>Group By</h3>
<p>Data can also be grouped by identical values using the GROUP BY clause.</p>
<pre><code contenteditable class="JavaScript">SELECT * from Invoice
GROUP BY CustomerId;
</code></pre>
<p>This query will return all invoices grouped by customer.</p>
</section>
<section>
<h3>Functions</h3>
<p>SQLite has three types of functions:</p>
<ul>
<li>Core functions</li>
<li>Aggregate functions</li>
<li>Date & Time functions</li>
</ul><br><br>
<p>Functions give you built-in functionality that allow you to do more with your data.</p>
</section>
<section>
<h3>Core Functions</h3>
<table>
<tr>
<td style="width:30%">sqlite_version()</td>
<td>Returns the version of the SQLite library</td>
</tr>
<tr>
<td>random()</td>
<td>Returns a pseudo-random integer</td>
</tr>
<tr>
<td>abs()</td>
<td>Returns the absolute value of a numerical argument</td>
</tr>
<tr>
<td>max()</td>
<td>Returns the maximum value</td>
</tr>
<tr>
<td>min()</td>
<td>Returns the minimum value</td>
</tr>
<tr>
<td>upper()</td>
<td>Converts characters into upper case</td>
</tr>
<tr>
<td>lower()</td>
<td>Converts characters into lower case</td>
</tr>
<tr>
<td>length()</td>
<td>Returns the length of a string of text</td>
</tr>
<tr>
<td>total_changes()</td>
<td>Returns the number of row changes since the current database connection was opened.</td>
</tr>
</table>
</section>
<section>
<h3>Aggregate Functions</h3>
<table>
<tr>
<td style="width:40%">count(*)</td>
<td>Returns the number of rows matching the query</td>
</tr>
<tr>
<td>count(id)</td>
<td>Returns the number of rows matching the query where the given column is not null</td>
</tr>
<tr>
<td>count(DISTINCT col)</td>
<td>Using DISTINCT with count returns the unique count</td>
</tr>
<tr>
<td>avg()</td>
<td>Returns the average value of all non-null values</td>
</tr>
<tr>
<td>sum()</td>
<td>Returns the sum of all non-null values</td>
</tr>
</table>
</section>
<section>
<h3>Date & Time Functions</h3>
<table>
<tr>
<td>date('now')</td>
<td>Returns the current date (Example: 2014-01-01)</td>
</tr>
<tr>
<td>datetime('now')</td>
<td>Returns the current date and time (Example: 2014-01-01 13:12:01)</td>
</tr>
<tr>
<td>time('now')</td>
<td>Return the current time (Example: 13:12:01)</td>
</tr>
</table>
</section>
<section>
<h3>Date & Time Modifiers</h3>
<p>You can pass one or more modifiers to the date(), time() and datetime() functions.</p>
<pre><code contenteditable class="JavaScript">sqlite> SELECT date('now', '2 months');
2014-07-29
</code></pre>
<p>Months and days can be passed as a modifier to add to the current date.</p>
<pre><code contenteditable class="JavaScript">sqlite> SELECT date('now', '-55 days');
2014-04-04
</code></pre>
<p>Likewise, you can subtract months and days.</p>
</section>
<section>
<h3>Date & Time Modifiers</h3>
<p>Other modifiers include:</p>
<table>
<tr>
<td style="width:30%">'start of year'</td>
<td>Returns the start of the year</td>
</tr>
<tr>
<td>'start of day'</td>
<td>Returns the start of the day (00:00:00)</td>
</tr>
<tr>
<td>'weekday 0'</td>
<td>Advances to to the next date where the day of the week matches the number passed,
where 0 is Sunday, 1 is Monday, ..., Saturday is 6.</td>
</tr>
</table>
</section>
<section>
<h3>Date & Time Modifiers</h3>
<p>What is the first Monday of next year?</p>
<pre><code contenteditable class="JavaScript">sqlite> SELECT datetime('now', '12 months', 'start of year',
...> 'weekday 1');
2015-01-05 00:00:00
</code></pre>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>Write SQL queries to answer the following questions about the Chinook data.</p>
<ul>
<li>If you bought one copy of every track, how much would you spend?</li>
<li>What is the average track price in our store?</li>
<li>How many genres of music are represented by tracks sold in our store?</li>
<li>How many artists have albums in our store?</li>
</ul>
</section>
<section>
<h3>Solutions</h3>
<ul>
<li>If you bought one copy of every track, how much would you spend?<br>
<pre><code contenteditable class="JavaScript">sqlite> SELECT sum(UnitPrice) from Track;
3680.969999999704
</code></pre>
</li>
<li>What is the average track price in our store?<br>
<pre><code contenteditable class="JavaScript">sqlite> SELECT avg(UnitPrice) as 'Average Price' FROM Track;
1.0508050242648312
</code></pre></li>
</ul>
</section>
<section>
<h3>Solutions</h3>
<ul>
<li>How many genres of music are represented by tracks sold in our store?<br>
<pre><code contenteditable class="JavaScript">sqlite> SELECT count(DISTINCT GenreId) from Track;
25
</code></pre></li>
<li>How many artists have albums in our store?<br>
<pre><code contenteditable class="JavaScript">sqlite> SELECT count(DISTINCT ArtistId) from Album;
204
</code></pre></li>
</ul>
</section>
<section>
<h3>Advanced Topics</h3>
<h4>Indexes</h4>
<p>Indexes are a special lookup table that an RDBMS uses to speed up data retrieval.</p>
<p>We can think of an index in a database as being similar to an index in the back of a book.</p>
<pre><code contenteditable class="JavaScript">sqlite> CREATE INDEX salary_index ON COMPANY (salary);
</code></pre>
<p>You can list all of the indexes on a TableBy using the <em>.indices TABLE</em> command.
<pre><code contenteditable class="JavaScript">sqlite> .indices Employee
IFK_EmployeeReportsTo
IPK_Employee
</code></pre>
</section>
<section>
<h3>Advanced Topics</h3>
<h4>Views</h4>
<p>Views are a specific look at data from one or more tables.</p>
<p>You can think of a view as the results of a query that can be accessed as a virtual table</p>
<p>A view is not a part of the physical schema of a database, and
its values are computed dynamically from the data in the database.</p>
<pre><code contenteditable class="JavaScript">sqlite> CREATE VIEW cheap_tracks AS SELECT * FROM Track WHERE UnitPrice <= 0.99;
</code></pre>
</section>
<section>
<h3>Advanced Topics</h3>
<h4>Triggers</h4>
<p>Triggers are database operations that are automatically performed when a specific
event occurs in your database.</p>
<p>Among other things, triggers often used to insert records into log tables when changes are made to data.</p>
<p>Creating triggers is beyond the scope of this class, but know they exist.</p>
</section>
<section>
<h3>Advanced Topics</h3>
<h4>Transactions</h4>
<p>A transaction is an atomic unit of database operations.</p>
<p>The effect of a transaction is that either all of the SQL statements
in a transaction are committed to the database, or all of them are rolled back (no changes).</p>
<p>A transaction begins with the BEGIN TRANSACTION statement, followed by COMMIT or ROLLBACK.</p>
<p>Transactions are beyond the scope of this class, but good to learn if you are dealing with
a group of changes that are "all or nothing".</p>
</section>
<section>
<h3>Resources</h3>
<ul>
<li><a href="http://sql.learncodethehardway.org/">Learn SQL the Hard Way</a></li>
<li>Jeff Atwood's blog post: <a href="http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/">
A Visual Explanation of SQL Joins</a>, from which the join explanations in this class were borrowed.</li>
<li><a href="http://mivuletech.wordpress.com/2011/03/22/an-overview-of-relational-algebra-operators-and-their-sql-translations/">
An Overview of Relational Algebra Operators and Their SQL Translations</a></li>
</ul>
</section>
<section>
<h2>Questions?</h2>
<div style = "font-size:1200%; height:100%; margin-top:40%" class ="blue">?
<div class ="clear"></div></div>
</section>
</div>
<footer>
<div class="copyright">
Intro to Databases -- Girl Develop It --
<a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc/3.0/80x15.png" /></a>
</div>
</footer>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: false,
history: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>