|
@@ -0,0 +1,340 @@
|
|
1
|
+# SQL
|
|
2
|
+
|
|
3
|
+The following lab is to be completed using MySQL in your terminal.
|
|
4
|
+
|
|
5
|
+To begin import the schema for all of the tables from the following location:
|
|
6
|
+
|
|
7
|
+`src/main/resources/schema-h2.sql`
|
|
8
|
+
|
|
9
|
+You must create a database in which to import the schema by accessing mysql through the terminal, then exit and import the schema using:
|
|
10
|
+
|
|
11
|
+`mysql -u root -p [database] < src/main/resources/schema-h2.sql`
|
|
12
|
+
|
|
13
|
+Don't submit any changes to the schema file. And, in all reality, you shouldn't be doing anything in there outside of
|
|
14
|
+experimenting for your own curiosity.
|
|
15
|
+
|
|
16
|
+## Guided Walkthrough
|
|
17
|
+
|
|
18
|
+Proceed through the sections below, testing out the queries shown and observing the result.
|
|
19
|
+Feel free to experiment with your own variations on these queries.
|
|
20
|
+This will help to build your familiarity with the process of working with SQL queries.
|
|
21
|
+
|
|
22
|
+#### Insert people into People table
|
|
23
|
+
|
|
24
|
+```SQL
|
|
25
|
+INSERT INTO people (last_name, first_name, mobile, birthday)
|
|
26
|
+VALUES ('Smith', 'John', '230-4293', '1973-01-23');
|
|
27
|
+```
|
|
28
|
+
|
|
29
|
+After doing that insert, go ahead and run all the statements in the `data-h2.sql` file. That should build out your
|
|
30
|
+database enough to get started. Feel free to add more people, though.
|
|
31
|
+
|
|
32
|
+My suggestion for the following stuff is, in the H2 console, leave the first line as a `SELECT *` from whatever tables
|
|
33
|
+you're querying. Then, after that, run your queries. That way you can compare the results of your query with the
|
|
34
|
+data in the table.
|
|
35
|
+
|
|
36
|
+#### Selecting all rows from table
|
|
37
|
+
|
|
38
|
+```SQL
|
|
39
|
+SELECT * FROM people;
|
|
40
|
+```
|
|
41
|
+
|
|
42
|
+#### Updating rows
|
|
43
|
+
|
|
44
|
+Update firstname for person whose id is 1
|
|
45
|
+
|
|
46
|
+```SQL
|
|
47
|
+UPDATE people SET first_name = 'Tony' WHERE id = 1;
|
|
48
|
+```
|
|
49
|
+
|
|
50
|
+Update mobile where last names are Smith
|
|
51
|
+
|
|
52
|
+```SQL
|
|
53
|
+UPDATE people SET mobile = '152-9854' WHERE last_name = 'Smith';
|
|
54
|
+```
|
|
55
|
+
|
|
56
|
+Update multiple columns with multiple conditions
|
|
57
|
+
|
|
58
|
+```SQL
|
|
59
|
+UPDATE people SET birthday = '1955-01-25'
|
|
60
|
+WHERE last_name = 'Smith'
|
|
61
|
+AND id = 4;
|
|
62
|
+```
|
|
63
|
+
|
|
64
|
+```SQL
|
|
65
|
+UPDATE people SET mobile = '333-3333', last_name = 'Johnson'
|
|
66
|
+WHERE first_name = 'Noelle' OR first_name = 'Raj';
|
|
67
|
+```
|
|
68
|
+
|
|
69
|
+#### Basic Functions
|
|
70
|
+
|
|
71
|
+```SQL
|
|
72
|
+SELECT * FROM people;
|
|
73
|
+```
|
|
74
|
+
|
|
75
|
+```SQL
|
|
76
|
+SELECT COUNT(homenumber) FROM homes;
|
|
77
|
+```
|
|
78
|
+
|
|
79
|
+```SQL
|
|
80
|
+SELECT homenumber FROM homes WHERE id = 1;
|
|
81
|
+```
|
|
82
|
+
|
|
83
|
+```SQL
|
|
84
|
+SELECT COUNT(*) FROM homes;
|
|
85
|
+```
|
|
86
|
+
|
|
87
|
+```SQL
|
|
88
|
+SELECT COUNT(DISTINCT last_name) FROM people;
|
|
89
|
+```
|
|
90
|
+
|
|
91
|
+```SQL
|
|
92
|
+SELECT SUM(id), AVG(id) FROM people;
|
|
93
|
+```
|
|
94
|
+
|
|
95
|
+```SQL
|
|
96
|
+SELECT SUM(id) AS sum, AVG(id) AS avg FROM people;
|
|
97
|
+```
|
|
98
|
+Notice the difference in the returned table with the aliases?
|
|
99
|
+
|
|
100
|
+```SQL
|
|
101
|
+SELECT MIN(birthday) FROM people;
|
|
102
|
+```
|
|
103
|
+
|
|
104
|
+#### Strings
|
|
105
|
+
|
|
106
|
+```SQL
|
|
107
|
+SELECT UPPER (first_name), LOWER(last_name) FROM people;
|
|
108
|
+```
|
|
109
|
+
|
|
110
|
+```SQL
|
|
111
|
+SELECT REPLACE(last_name, 'a', '1') FROM people;
|
|
112
|
+```
|
|
113
|
+
|
|
114
|
+```SQL
|
|
115
|
+SELECT last_name FROM people;
|
|
116
|
+```
|
|
117
|
+
|
|
118
|
+```SQL
|
|
119
|
+INSERT INTO people (first_name, last_name, mobile)
|
|
120
|
+VALUES ('Otto', 'Von Count', '656-6548');
|
|
121
|
+```
|
|
122
|
+
|
|
123
|
+```SQL
|
|
124
|
+SELECT CONCAT(first_name, last_name) FROM people
|
|
125
|
+WHERE last_name = 'Smith';
|
|
126
|
+```
|
|
127
|
+
|
|
128
|
+```SQL
|
|
129
|
+SELECT CONCAT(first_name, ' ', last_name)
|
|
130
|
+FROM people
|
|
131
|
+WHERE last_name = 'Smith';
|
|
132
|
+```
|
|
133
|
+
|
|
134
|
+```SQL
|
|
135
|
+SELECT CONCAT_WS(' ',first_name, last_name, mobile)
|
|
136
|
+FROM people WHERE last_name= 'Smith';
|
|
137
|
+```
|
|
138
|
+
|
|
139
|
+```SQL
|
|
140
|
+SELECT homenumber, LEFT(homenumber, 3), RIGHT(homenumber, 2) FROM homes;
|
|
141
|
+```
|
|
142
|
+
|
|
143
|
+```SQL
|
|
144
|
+SELECT LENGTH(address), CHAR_LENGTH(address) FROM homes;
|
|
145
|
+```
|
|
146
|
+
|
|
147
|
+#### Compare
|
|
148
|
+
|
|
149
|
+```SQL
|
|
150
|
+SELECT first_name, last_name, YEAR(birthday) FROM people WHERE birthday >= '1970-07-06' AND birthday<='1987-07-06';
|
|
151
|
+```
|
|
152
|
+
|
|
153
|
+```SQL
|
|
154
|
+SELECT first_name, birthday FROM people WHERE first_name='Thomas' OR first_name='Raj' OR first_name='Sheeri';
|
|
155
|
+```
|
|
156
|
+
|
|
157
|
+```SQL
|
|
158
|
+SELECT first_name, birthday FROM people WHERE first_name IN ('Noelle', 'Thomas', 'Raj');
|
|
159
|
+```
|
|
160
|
+
|
|
161
|
+#### Wild Cards
|
|
162
|
+
|
|
163
|
+```SQL
|
|
164
|
+SELECT first_name FROM people WHERE RIGHT(first_name,1)='e';
|
|
165
|
+```
|
|
166
|
+
|
|
167
|
+```SQL
|
|
168
|
+SELECT first_name FROM people WHERE first_name LIKE '%j';
|
|
169
|
+```
|
|
170
|
+
|
|
171
|
+```SQL
|
|
172
|
+SELECT first_name FROM people WHERE first_name LIKE '%o%';
|
|
173
|
+```
|
|
174
|
+
|
|
175
|
+```SQL
|
|
176
|
+SELECT first_name FROM people WHERE first_name NOT LIKE '%o%';
|
|
177
|
+```
|
|
178
|
+
|
|
179
|
+```SQL
|
|
180
|
+SELECT COUNT(*) FROM people;
|
|
181
|
+```
|
|
182
|
+
|
|
183
|
+```SQL
|
|
184
|
+SELECT last_name, COUNT(*) FROM people GROUP BY last_name;
|
|
185
|
+```
|
|
186
|
+
|
|
187
|
+```SQL
|
|
188
|
+SELECT last_name, GROUP_CONCAT(mobile) FROM people GROUP BY last_name;
|
|
189
|
+```
|
|
190
|
+
|
|
191
|
+```SQL
|
|
192
|
+SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people GROUP BY last_name;
|
|
193
|
+```
|
|
194
|
+
|
|
195
|
+```SQL
|
|
196
|
+SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people GROUP BY last_name HAVING COUNT(*)>1;
|
|
197
|
+```
|
|
198
|
+
|
|
199
|
+```SQL
|
|
200
|
+SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people WHERE last_name != 'Cabral' GROUP BY last_name HAVING COUNT(*)>1;
|
|
201
|
+```
|
|
202
|
+
|
|
203
|
+#### Sorting
|
|
204
|
+
|
|
205
|
+```SQL
|
|
206
|
+SELECT first_name, birthday FROM people ORDER BY birthday;
|
|
207
|
+```
|
|
208
|
+
|
|
209
|
+```SQL
|
|
210
|
+SELECT first_name, birthday FROM people ORDER BY birthday DESC;
|
|
211
|
+```
|
|
212
|
+
|
|
213
|
+```SQL
|
|
214
|
+SELECT first_name, last_name FROM people ORDER BY last_name, first_name;
|
|
215
|
+```
|
|
216
|
+
|
|
217
|
+```SQL
|
|
218
|
+SELECT first_name, birthday FROM people ORDER BY birthday DESC LIMIT 3;
|
|
219
|
+```
|
|
220
|
+
|
|
221
|
+```SQL
|
|
222
|
+SELECT first_name, MONTHNAME(birthday) as mon, birthday FROM people ORDER BY MONTH(birthday);
|
|
223
|
+```
|
|
224
|
+
|
|
225
|
+```SQL
|
|
226
|
+SELECT last_name, COUNT(*) FROM people GROUP BY last_name;
|
|
227
|
+```
|
|
228
|
+
|
|
229
|
+```SQL
|
|
230
|
+SELECT last_name, COUNT(*) FROM people GROUP BY last_name ORDER BY NULL;
|
|
231
|
+```
|
|
232
|
+
|
|
233
|
+#### Inserting and Replacing Records
|
|
234
|
+
|
|
235
|
+```SQL
|
|
236
|
+INSERT INTO people (first_name, last_name, birthday, home_id)
|
|
237
|
+VALUES ('John', 'Smith', '1998-04-07', 4),
|
|
238
|
+('Maya', 'Wasserman' , NULL, 4),
|
|
239
|
+('Paul', 'Thompson', '1996-05-27', 1);
|
|
240
|
+```
|
|
241
|
+
|
|
242
|
+#### Deleting
|
|
243
|
+
|
|
244
|
+```SQL
|
|
245
|
+DELETE FROM people WHERE first_name='Maya';
|
|
246
|
+```
|
|
247
|
+
|
|
248
|
+```SQL
|
|
249
|
+SELECT * FROM people;
|
|
250
|
+```
|
|
251
|
+
|
|
252
|
+#### JOIN
|
|
253
|
+
|
|
254
|
+```SQL
|
|
255
|
+INSERT INTO people (first_name, last_name, birthday)
|
|
256
|
+VALUES ('Eli', 'Kramer', '1984-01-15');
|
|
257
|
+```
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+```SQL
|
|
261
|
+SELECT * FROM people;
|
|
262
|
+```
|
|
263
|
+
|
|
264
|
+```SQL
|
|
265
|
+SELECT * FROM homes;
|
|
266
|
+```
|
|
267
|
+
|
|
268
|
+```SQL
|
|
269
|
+SELECT p.first_name, h.address
|
|
270
|
+FROM people p
|
|
271
|
+INNER JOIN homes h on (p.home_id = h.id);
|
|
272
|
+```
|
|
273
|
+
|
|
274
|
+```SQL
|
|
275
|
+SELECT first_name, last_name
|
|
276
|
+FROM people p
|
|
277
|
+INNER JOIN homes h on (p.home_id = h.id)
|
|
278
|
+WHERE p.HOME_ID = 1;
|
|
279
|
+```
|
|
280
|
+
|
|
281
|
+```SQL
|
|
282
|
+SELECT p.*, h.address, h.homenumber
|
|
283
|
+FROM people p
|
|
284
|
+INNER JOIN homes h on (p.home_id = h.id)
|
|
285
|
+WHERE p.first_name LIKE '%e%';
|
|
286
|
+```
|
|
287
|
+
|
|
288
|
+##### Exercise:
|
|
289
|
+
|
|
290
|
+Devise a report:
|
|
291
|
+
|
|
292
|
+Show all the people in your address table, only if you know their birthday.
|
|
293
|
+
|
|
294
|
+Show their name, address and birthday ordered by birthday month, so January birthdays are first.
|
|
295
|
+
|
|
296
|
+Devise a report:
|
|
297
|
+
|
|
298
|
+Output all information for all people and their home information
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+## Mini Movie Database
|
|
302
|
+
|
|
303
|
+Once you have figured out the correct queries for each step,
|
|
304
|
+save a copy in a file called `src/main/resources/script.sql`. This will be how you submit this assignment.
|
|
305
|
+If at any time you need to reset the database, you can restart your Spring Boot server.
|
|
306
|
+
|
|
307
|
+Add the following movies to the `movies` table using an insert statement:
|
|
308
|
+
|
|
309
|
+| Title | Runtime | Genre | IMDB Score | Rating |
|
|
310
|
+| ----- | ------- | ----- | ----------- | ----- |
|
|
311
|
+| Howard the Duck | 110 | Sci-Fi | 4.6 | PG |
|
|
312
|
+| Lavalantula | 83 | Horror | 4.7 | TV-14 |
|
|
313
|
+| Starship Troopers | 129 | Sci-Fi | 7.2 | PG-13 |
|
|
314
|
+| Waltz With Bashir | 90 | Documentary | 8.0 | R |
|
|
315
|
+| Spaceballs | 96 | Comedy | 7.1 | PG |
|
|
316
|
+| Monsters Inc. | 92 | Animation | 8.1 | G |
|
|
317
|
+
|
|
318
|
+Add a few more movies of your choosing.
|
|
319
|
+
|
|
320
|
+Create a query to find all movies in the Sci-Fi genre.
|
|
321
|
+
|
|
322
|
+Create a query to find all films that scored at least a 6.5 on IMDB
|
|
323
|
+
|
|
324
|
+For parents who have young kids, but who don't want to sit through long children's movies,
|
|
325
|
+create a query to find all of the movies rated G or PG that are less than 100 minutes long.
|
|
326
|
+
|
|
327
|
+Create a query to show the average runtimes of movies scoring below a 7.5 on imdb, grouped by their respective genres.
|
|
328
|
+
|
|
329
|
+There's been a data entry mistake; Starship Troopers is actually rated R, not PG-13.
|
|
330
|
+Create a query that finds the movie by its title and changes its rating to R.
|
|
331
|
+
|
|
332
|
+Show the ID number and rating of all of the Horror and Documentary movies in the database. Do this in only one query.
|
|
333
|
+
|
|
334
|
+This time let's find the average, maximum, and minimum IMDB score for movies of each rating.
|
|
335
|
+
|
|
336
|
+That last query isn't very informative for ratings that only have 1 entry.
|
|
337
|
+Use a `HAVING COUNT(*) > 1` clause to only show ratings with multiple movies showing.
|
|
338
|
+
|
|
339
|
+Let's make our movie list more child-friendly. Delete all entries that have a rating of R.
|
|
340
|
+Remember to record your query in `script.sql`.
|