forked from okapishomapi/intro-to-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
2298 lines (2276 loc) · 101 KB
/
index.html
File metadata and controls
2298 lines (2276 loc) · 101 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 name="generator" content="HTML Tidy for HTML5 for Mac OS X version 5.4.0">
<meta charset="utf-8">
<title>
Intro to SQL - Annye Driscoll
</title>
<meta name="description" content=
"Girl Develop It Intro to SQL course">
<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/simple.css" id="theme"><!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css"><!-- light editor-->
<link rel="stylesheet" href="lib/css/light.css"><!-- dark editor<link rel="stylesheet" href="lib/css/dark.css">-->
<link rel="stylesheet" href="plugin/accessibility-helper/css/accessibility-helper.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script><!-- 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]-->
<style type="text/css">
img.c60 {border-width:0}
ul.c59 {list-style-type: none; font-size: 30px}
div.c58 {max-width: 500px; margin: 30px}
span.c57 {color:violet:}
p.c56 {background-color: OrangeRed}
span.c55 {color: OrangeRed}
span.c54 {background-color: OrangeRed; color:white}
ul.c53 {list-style-type: none;max-width: 600px}
li.c52 {background-color: yellow; margin-bottom: 6px;line-height: 22px;font-family: Courier; font-size: 22px;}
table.c51 {width:100%; font-size: 28px}
span.c50 {color:orange}
li.c49 {font-weight=900}
span.c48 {font-weight: 900}
span.c47 {background-color: yellow}
p.c46 {font-size: 12px}
p.c45 {font-size: 30px}
p.c44 {background-color: orange; font-weight: 900}
h3.c43 {font-size: 49px}
h3.c42 {font-size: 50px}
p.c41 {background-color: orange; font-size: 24px;font-weight: 900}
span.c40 {font-weight: 900px}
p.c39 {font-size: 32px}
table.c38 {border-collapse:collapse;border-spacing:0}
td.c37 {font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;vertical-align:top}
th.c36 {font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;vertical-align:top}
p.c35 {font-size: 24px}
div.c34 {text-align: center}
table.c33 {border-collapse:collapse;border-spacing:0;border-color:#aaa}
td.c32 {font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aaa;color:#333;background-color:#fff;vertical-align:top}
th.c31 {font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aaa;color:#333333;background-color:#3166ff;vertical-align:top}
p.c30 {text-align: left}
p.c29 {background-color: orange;font-size: 24px}
li.c28 {font-family:Courier}
span.c27 {font-family:Courier}
p.c26 {color:violet; font-size: 30px; line-height:30px}
h3.c25 {font-size: 48px}
p.c24 {background-color: orange; font-size: 25px}
div.c23 {background-color: LightGray}
p.c22 {color:blue}
div.c21 {font-family:Courier;font-size: 24px;line-height: 24px; margin-left:250px}
table.c20 {width: 100%}
span.c19 {color:red}
p.c18 {background-color: orange}
div.c17 {font-size: 30px}
span.c16 {background-color: orange}
span.c15 {color:blue;text-decoration: underline}
table.c14 {margin-left:150px}
div.c13 {font-family:Courier;font-size: 24px;line-height: 24px}
ul.c12 {list-style-type: none}
span.c11 {background-color:orange}
div.c10 {font-size:24px; line-height:24px}
span.c9 {font-weight:900}
div.c8 {max-width: 500px; font-size: 20px; margin: 20px}
li.c7 {list-style-type: disc}
ul.c6 {list-style-type: circle}
div.c5 {float:left; width:50%;}
ul.c4 {list-style-type: square}
li.c3 {list-style-type: circle}
h3.c2 {color: black}
span.c1 {color: violet}
</style>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- Opening -->
<section>
<h1>
Intro to SQL
</h1><img src="img/circle-gdi-logo.png" alt="GDI Logo">
<p>
<small>Annye Driscoll | <a href="http://annyedriscoll.com/">annyedriscoll.com</a></small>
</p>
</section><!-- Compatibility-->
<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><br>
<p>
<a href="https://www.girldevelopit.com/code-of-conduct">Our code of conduct</a>
</p>
<p>
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>
Tell us about yourself
</h3>
<ul>
<li>What's your name?
</li>
<li>What's your experience level with SQL?
</li>
<li>What do you hope to get out of the class?
</li>
<li>What is your favorite movie and how many times have you watched it?
</li>
</ul><br>
<br>
<br>
<p>
We're going to make a table out of this info in part 2!
</p>
</section>
<section>
<h2>
About me
</h2>
<ul>
<li>Full-stack developer
</li>
<li>SQL education is from the "field"
</li>
<li>Favorite movie is Return of the King, watched 20 times.
</li>
</ul>
</section><!-- Examples of basic html based slides -->
<section>
<h3>
What to expect from the class
</h3>
<ul>
<li>This is a complement to the database design class.
</li>
<li>We'll be using the same terminology and building on those concepts...
</li>
<li>...but we'll be hands-on!
</li>
</ul><img src="https://media.giphy.com/media/Dkp3uyouK71xm/giphy.gif" alt="Jim Jazz Hands" width="400px">
<p>
By the end of the class, you will be able to query and modify a SQL table to make sense of the data you have.
</p>
</section>
<section>
<h3>
Plan for the day: CRUD
</h3>
<div id="column1" class="c5">
<h3 class="c2">
<small>Pre-break: c<span class="c1">R</span>ud</small>
</h3>
<ul>
<li>
<p>
<small>What's a database? What's SQL?</small>
</p>
</li>
<li>
<small>How to query a database:</small>
</li>
<ul>
<li class="c3">
<small>Select statement (from statement)</small>
</li>
<ul class="c4">
<li>
<small>Select, Distinct</small>
</li>
<li>
<small>Count</small>
</li>
</ul>
<li class="c3">
<small>Group By, Order By</small>
</li>
<li class="c3">
<small>Clauses</small>
</li>
<ul class="c4">
<li>
<small>Where</small>
</li>
<li>
<small>Like</small>
</li>
</ul>
<li class="c3">
<small>Joins (Inner/Outer Left/Right)</small>
</li>
<li class="c3">
<small>Practice</small>
</li>
</ul>
</ul>
</div>
<div id="column2" class="c5">
<h3 class="c2">
<small>Post-break: <span class="c1">C</span>r<span class="c1">UD</span></small>
</h3>
<ul>
<ul>
<li>
<small>Subqueries</small>
</li>
<li>
<small>Practice</small>
</li>
<li>
<small>How to modify a database</small>
</li>
<ul class="c6">
<li>
<small>Creating a table</small>
</li>
<li>
<small>Updating a table with rows</small>
</li>
<li>
<small>Deleting rows vs. deleting tables</small>
</ul>
<li class="c7">
<small>Optimizing queries</small>
</li>
<li>
<small>Resources and links</small>
</li>
<li>
<small>Practice</small>
</li>
</ul>
</ul>
</div>
<div class="left c8"></div>
</section>
<section>
<h3>
What is a database?
</h3>
<p>
A set of related data and the way it is organized!
</p>
</section>
<section>
<h3>What is a database?</h3>
<p>A collection of tables, schemas, queries, reports, views, and other objects (it’s not just tables of data!) often organized in a way that supports
processes or reflects real life in order to facilitate business or functions.</p>
</section>
<section>
<h3>What is a database - what are those things?!</h3>
<p>
<em>A collection of tables, schemas, queries, views…</em>
<ul>
<li><b>Table</b> = collection of related data arranged in columns and rows.</li>
<li><b>Schema</b> = logical grouping of objects (tables, etc.), often in a functional way.</li>
<li><b>Query</b> = request for information from a database.</li>
<li><b>View</b> = result of a stored query.</li>
<aside class="notes">So for 1 database you may have an HR schema and a sales schema. A view looks like a table, but a view’s data is calculated
in real-time. Also provide pretty significant advantages to a
table - can restrict access to just views (can be a subset of data), can simplify or aggregate data, take very little space to
store since a database stores just the definition of the view, not a copy of all the data it has.</aside>
</ul>
</p>
</section>
<!-- <section>
<span class="c1">CRUD (Create, Read, Update, Delete):</span> How you change the databases and tables in the database
</section> -->
<section>
<h3>Our database model of choice</h3>
<p><b>Relational database</b> = organizes data into one or more tables of columns and rows with a unique key identifying each row (record).</p>
</section>
<section>
<h4>
The language of relational databases
</h4>
<div class="c15">
<ul>
<li>
<b>Database keys (primary and foreign):</b> The pointers in each table that can link to other tables
</li><br>
<li>
<b>Indexes:</b> Make databases easier to search
</li><br>
<li>
<b>Queries:</b> How to ask questions of the tables and databases
</li><br>
</ul>
</div>
</section>
<section>
<h3>
Primary and Foreign Keys
</h3>
<p>
Tables are joined on special fields called keys
</p>
<ul class="c12">
<li>Primary Key (PK)
</li>
<ul>
<li>A field (or set of fields) that uniquely identify a row
</li>
<!-- <li>In math speak, the minimal set of fields that the row is <span class="c48">functionally dependent</span> upon
</li> -->
</ul>
</ul><br>
<br>
<ul class="c12">
<li>Foreign Reference Key (FK)
</li>
<ul>
<li>A field (or set of fields) that is a PK in some other table
</li>
<li>There can be multiple FK in a table
</li>
<li>Points to a row in another table
</li>
</ul>
</ul>
</section>
<!-- <section>
<h3>
Databases, schemas, and tables
</h3>
<p>
DB Schema→ Table → Row → Value
</p><img src="https://s-media-cache-ak0.pinimg.com/originals/15/4e/a5/154ea5bcf4a430980f137eae6f328075.jpg" alt="Nesting Dolls" width=
"500px">
<p>
Excel File → Workbook → Row → Cell
</p>
</section> -->
<section>
<h3>
What's SQL?
</h3>
<div class="c13">
<ul class="c12">
<li>
<span class="c11">SELECT</span> user_name, SUM(total_purchases)
</li>
<li>
<span class="c11">FROM</span> billing_database.user_table
</li>
<li>
<span class="c11">WHERE</span> user_name LIKE 'Henderson%'
</li>
<li>
<span class="c11">AND</span> billing_month = 'January'
</li>
<li>
<span class="c11">GROUP BY</span> user_name;
</li>
</ul>
</div><br>
<ul>
<li>SQL is the language that the database speaks to get data, modify data, and modify the database.
</li><br>
<li>SQL has a lot of words similar to English, but they have their own meaning as keywords.
</li><br>
<li>Like Siri or Alexa
</li><br>
</ul>
</section>
<section>
<h3>
Different Flavors of SQL
</h3>
<ul>
<li>How do you say that you want a can of Coca-Cola?
</li>
<ul>
<li class="c3">Coke, soda or pop?
</li><br>
</ul>
<li>How do you write SQL?
</li>
</ul><br>
<table class="c14">
<tr>
<td>
Oracle
</td>
<td>
MySQL
</td>
</tr>
<tr>
<td>
SQL Server
</td>
<td>
Postgres
</td>
</tr>
<tr>
<td>
Access
</td>
<td>
Sybase
</td>
</tr>
<tr>
<td>
DB2
</td>
</tr>
</table><br>
<p>
All <span class="c15">different flavors</span> of the same language
</p>
</section>
<section>
<h3>
A multi-functional language
</h3>
<ul>
<li>
<span class="c16">Reading</span> tables you or other people wrote
</li><br>
<li>
<span class="c16">Modifying</span> those tables to add your own information and/or creating your own tables and data
</li><br>
</ul>
</section>
<section>
<h3>
Databases and CRUD
</h3>
<div class="c17">
<ul class="c12">
<li>
<span class="c1">Create</span> INSERT, CREATE
</li>
<ul class="c12">
<li>Adding rows to your table or tables to your database
</li>
</ul>
<li>
<span class="c1">Read</span> SELECT
</li>
<ul class="c12">
<li>Picking specific information from your table
</li>
</ul>
<li>
<span class="c1">Update</span> UPDATE, ALTER
</li>
<ul class="c12">
<li>Changing specific information in your table or your database
</li>
</ul>
<li>
<span class="c1">Delete</span> DELETE, DROP, TRUNCATE
</li>
<ul class="c12">
<li>Erase your data or your tables
</li>
</ul>
</ul>
</div>
</section>
<section>
<h3>
Plan for the day: CRUD
</h3>
<div id="column1" class="c5">
<h3 class="c2">
<small>Pre-break: c<span class="c1">R</span>ud</small>
</h3>
<ul>
<li>
<p>
<small>What's a database? What's SQL?</small>
</p>
</li>
<li>
<small>How to query a database:</small>
</li>
<ul>
<li class="c3">
<small>Select statement (from statement)</small>
</li>
<ul class="c4">
<li>
<small>Select, Distinct</small>
</li>
<li>
<small>Count</small>
</li>
</ul>
<li class="c3">
<small>Group By, Order By</small>
</li>
<li class="c3">
<small>Clauses</small>
</li>
<ul class="c4">
<li>
<small>Where</small>
</li>
<li>
<small>Like</small>
</li>
</ul>
<li class="c3">
<small>Joins (Inner/Outer Left/Right)</small>
</li>
<li class="c3">
<small>Practice</small>
</li>
</ul>
</ul>
</div>
<div id="column2" class="c5">
<h3 class="c2">
<small>Post-break: <span class="c1">C</span>r<span class="c1">UD</span></small>
</h3>
<ul>
<ul>
<li>
<small>Subqueries</small>
</li>
<li>
<small>Practice</small>
</li>
<li>
<small>How to modify a database</small>
</li>
<ul class="c6">
<li>
<small>Creating a table</small>
</li>
<li>
<small>Updating a table with rows</small>
</li>
<li>
<small>Deleting rows vs. deleting tables</small>
</ul>
<li class="c7">
<small>Optimizing queries</small>
</li>
<li>
<small>Resources and links</small>
</li>
<li>
<small>Practice</small>
</li>
</ul>
</ul>
</div>
<div class="left c8"></div>
</section>
<section>
<h3>
What can you do with SQL - Reading!
</h3>
<p class="c18">
The power is in the relationship between data sets.
</p><br>
<ul>
<li>How many customers do we have in Los Angeles?
</li><br>
<li>What's a certain person's Facebook and Twitter handle?
</li><br>
<li>Did profits go up or down this quarter?
</li><br>
<li>How many users are clicking on our new site?
</li><br>
</ul>
</section>
<section>
<h3>
SQL clauses: required order
</h3>
<div class="c21">
<table class="c20">
<tr>
<td></td>
</tr>
<tr>
<td>
1 <span class="c19">SELECT</span>
</td>
<td>
selects variables
</td>
</tr>
<tr>
<td>
2 <span class="c19">FROM</span>
</td>
<td>
opens datasets
</td>
</tr>
<tr>
<td>
3 WHERE
</td>
<td>
restricts observations
</td>
</tr>
<tr>
<td>
4 GROUP BY
</td>
<td>
groups observations
</td>
</tr>
<tr>
<td>
5 HAVING
</td>
<td>
restricts groups
</td>
</tr>
<tr>
<td>
6 ORDER BY
</td>
<td>
sorts results
</td>
</tr>
</table>
</div><br>
</section>
<section>
<h2>
Let's develop it!
</h2>
</section>
<section>
<h3>
Let's get started reading tables
</h3>
<p>
We'll use MySQL, a popular version of SQL.
</p>
<p>
If you've done anything online, chances are the transation went through MySQL (Walmart, Kayak, Facebook, Etsy, Verizon).
</p><br>
<p>
We'll log on to the front-end of MySQL, PHPMyAdmin.
</p><br>
<div class="c23">
<p class="c22">
Logon site: https://demo.phpmyadmin.net/STABLE/
</p>
<p class="c22">
Login username is "root". Leave the password field blank.
</p>
</div>
</section>
<section>
<h3>
Once you're in, make sure you're in the MySQL demo:
</h3><img src="img/MySqlDemo.JPG" alt="Correct demo selected">
</section>
<section>
<h3>
How PHPMyAdmin Works
</h3><img src="img/php-abstraction.png" alt="PHP abstraction">
</section>
<!-- <section>
<h3>
Two ways to execute in MySQL: PHPMyAdmin and command line
</h3><img src="img/ways-to-execute.png" alt="PHP abstraction" width="900px">
</section> -->
<section>
<h3>
We have test data we can learn with!
</h3>
<p>
Already loaded are a bunch of employee records we'll be using together.
</p><img src="img/options.png" alt="Options" width="850px">
</section>
<section>
<h3 class="c25">
Employee data:<br>
6 tables in the employees schema
</h3><img src="img/employee-schema.png" alt="Schema"><br>
<a href="https://github.com/datacharmer/test_db">Data Source</a>
</section>
<section>
<h3>
Tables and Schemas
</h3>
<p>
<code>employees</code> is the database and it has 6 tables.
</p>
<p class="c26">
Database → Schema → Table
</p>
<p class="c26">
Book → Chapter → Page
</p>
<p class="c26">
MySQL Database → Employees Schema → employees Table
</p><img src="img/employee-table.png" alt="PHP abstraction" width="250px">
</section>
<section>
<h3>
How to access specific tables
</h3>
<ul class="c12">
<li>Database: <span class="c27">employees</span>
</li><br>
<li>Tables:
</li>
<ul class="c12">
<li class="c28">employees.employees,
</li>
<li class="c28">employees.departments,
</li>
<li class="c28">employees.salaries
</li>
</ul>
<li>And so on.
</li>
</ul><br>
<br>
<p class="c29">
Usually it's bad practice to name tables the same thing as the database (confusing), but we'll be ok with the small test data set.
</p>
</section>
<section>
<h2>
Let's develop it!
</h2>
</section>
<section>
<h3>
Let's run our first query!
</h3>
<h6>
Go to the SQL tab and type in:
</h6>
<pre><code contenteditable="" class="sql">SELECT * FROM employees.employees; </code></pre><img src="img/select-star.png" alt="select-star">
<p class="c29">
Type out all the SQL you write. It'll be easier to learn.
</p>
</section>
<section>
<h3>
SELECT * from different tables
</h3>
<p>
Try a couple of these:
</p>
<pre><code contenteditable="" class="sql">SELECT * FROM employees.titles;
SELECT * FROM employees.dept_emp;
SELECT * FROM employees.salaries; </code></pre>
<p>
and check out the tables and info.
</p>
</section>
<section>
<h2>
Stretch break!
</h2><img src="img/cat_stretch.jpg" alt="kitty!">
</section>
<section>
<h3>
Selecting specific columns
</h3>
<p>
Now let's try selecting only certain columns
</p>
<pre><code contenteditable="" class="sql">SELECT title, emp_no FROM employees.titles; </code></pre>
<p>
A note about the semi-colon: depending on which flavor of SQL you're using you may not need it, but it's good
practice in general to include it.
</p>
</section>
<section>
<h3>
More specific columns. Let's get to know our data.
</h3>
<p class="c30">
Selecting one column:
</p>
<pre><code contenteditable="" class="sql">SELECT dept_name FROM employees.departments;</code></pre>
<p class="c30">
Selecting multiple columns:
</p>
<pre><code contenteditable="" class="sql">SELECT emp_no, dept_no FROM employees.dept_emp;</code></pre>
</section>
<section>
<h3>
SQL has built-in functions!
</h3>
<ul>
<li>The MIN() function returns the smallest value of the selected column.</li>
<li>The MAX() function returns the largest value of the selected column.</li>
<li>The COUNT() function returns the number of rows that matches a specified criteria.</li>
<li>The AVG() function returns the average value of a numeric column.</li>
<li>The SUM() function returns the total sum of a numeric column.</li>
</ul>
</section>
<section>
<h3>
Trying out some built-in functions
</h3>
<pre><code contenteditable="" class="sql">SELECT AVG(salary) FROM employees.salaries;</code></pre>
<pre><code contenteditable="" class="sql">SELECT MIN(from_date) FROM employees.dept_manager;</code></pre>
</section>
<section>
<h3>
Distinct: selecting only unique instances of that column.
</h3>
<pre><code contenteditable="" class="sql">SELECT DISTINCT title FROM employees.titles;</code></pre>
</h2><img src="img/distinct-titles.JPG" alt="list of the distinct titles in the database">
</section>
<section>
<h3>
Difference between distinct and *
</h3>
<pre><code contenteditable="" class="sql">SELECT COUNT(DISTINCT title) FROM employees.titles;
6
</code></pre>
<pre><code contenteditable="" class="sql">SELECT COUNT(title) FROM employees.titles;
14,782
</code></pre>
</section>
<section>
<h3>
Difference between distinct and *
</h3>
<p>
Distinct: Selecting only unique instances
</p>
<p>
Which result set is bigger?
</p>
<p>
What is each one telling us?
</p>
<pre><code contenteditable="" class="sql">SELECT COUNT(title) FROM employees.titles;
14,782
</code></pre>
<pre><code contenteditable="" class="sql">SELECT COUNT(*) FROM employees.salaries;
94,917
</code></pre>
</section>
<section>
<h3>
Which table is the largest?<br>
</h3>
<pre><code contenteditable="" class="sql">SELECT COUNT(*) from employees.departments;
9</code></pre>
<pre><code contenteditable="" class="sql">SELECT COUNT(*) FROM employees.dept_emp;
11,050</code></pre>
<pre><code contenteditable="" class="sql">SELECT COUNT(*) FROM employees.dept_manager;
24</code></pre>
<pre><code contenteditable="" class="sql">SELECT COUNT(*) FROM employees.employees;
10,000</code></pre>
<pre><code contenteditable="" class="sql">SELECT COUNT(*) FROM employees.salaries;
94,917</code></pre>
<pre><code contenteditable="" class="sql">SELECT COUNT(*) FROM employees.titles;
14,782</code></pre>
</section>
<section>
<h3>
SQL clauses: required order
</h3>
<div class="c21">
<table class="c20">
<tr>
<td>
1 SELECT
</td>
<td>
selects variables
</td>
</tr>
<tr>
<td>
2 FROM
</td>
<td>
opens datasets
</td>
</tr>
<tr>
<td>
3 WHERE
</td>
<td>
restricts observations
</td>
</tr>
<tr>
<td>
<span class="c19">4 GROUP BY</span>
</td>
<td>
groups observations
</td>
</tr>
<tr>
<td>
5 HAVING
</td>
<td>
restricts groups
</td>
</tr>
<tr>
<td>
6 ORDER BY
</td>
<td>
sorts results
</td>
</tr>
</table>
</div>
</section>
<section>
<h3>
Now let's group stuff!
</h3>
<pre><code contenteditable="" class="sql">
SELECT type_of_dog,COUNT(*)
FROM animals.total_dogs
GROUP BY type_of_dog;
</code></pre>
<p>
Group statements corral data together into categories when we want to count things by groups of things
</p>
<p>
i.e. How many total dogs vs puppies vs parents.
</p><img src="img/dog-moms.png" alt="Pups"><br>
</section>
<section>
<h3>
Counting titles and salaries
</h3>What is this telling us? How many instances of that title are there in the table?
<pre><code contenteditable="" class="sql">SELECT title, COUNT(*) FROM employees.titles
GROUP by title;
</code></pre><br>
</section>
<section>
<h3>
More group by examples
</h3>