瀏覽代碼

Add H2 walkthrough worksheet

David Ginzberg 7 年之前
父節點
當前提交
a9483abaac
共有 1 個文件被更改,包括 339 次插入0 次删除
  1. 339
    0
      H2-Worksheet.md

+ 339
- 0
H2-Worksheet.md 查看文件

@@ -0,0 +1,339 @@
1
+# SQL Worksheet
2
+
3
+Proceed through the sections below, testing out the queries shown and observing the result. Use the included H2 console to run your queries. Feel free to experiment with your own variations on these queries. This will help to build your familiarity with the process of working with SQL queries.
4
+
5
+## Clauses
6
+
7
+Select Clause  
8
+From Clause
9
+
10
+#### Insert people into People table
11
+
12
+```SQL
13
+INSERT INTO PEOPLE (LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY)
14
+VALUES ('Smith', 'John', '230-4293', '1973-01-23');
15
+```
16
+
17
+Following the example, insert at least three other people into the table.
18
+
19
+#### Selecting all rows from table 
20
+
21
+```SQL
22
+SELECT * FROM PEOPLE;
23
+```
24
+
25
+#### Updating rows
26
+
27
+Update firstname for person whose id is 1
28
+
29
+```SQL
30
+UPDATE PEOPLE SET FIRST_NAME = 'TONY' WHERE ID = 1;
31
+```
32
+
33
+Update mobile where last names are Smith
34
+
35
+```SQL
36
+UPDATE PEOPLE SET MOBILE = '152-9854' WHERE LAST_NAME = 'Smith';
37
+```
38
+
39
+Update multiple columns with multiple conditions
40
+
41
+```SQL
42
+UPDATE people SET birthday = '1955-01-25' 
43
+WHERE 
44
+	last_name = 'Smith' 
45
+	AND id = 4;
46
+```
47
+
48
+```SQL
49
+UPDATE people SET mobile = '333-3333', last_name = 'Johnson' 
50
+WHERE first_name = 'Noelle' OR first_name = 'Raj';
51
+```
52
+
53
+## Basic Functions
54
+
55
+```SQL
56
+SELECT * FROM PEOPLE;
57
+```
58
+
59
+```SQL
60
+SELECT COUNT(HOMEPHONE) FROM HOMES;
61
+```
62
+
63
+```SQL
64
+SELECT HOMENUMBER FROM HOME WHERE ID = 1;
65
+```
66
+
67
+```SQL
68
+SELECT COUNT(*) FROM HOME;
69
+```
70
+
71
+```SQL
72
+SELECT COUNT(DISTINCT last_name) FROM PEOPLE;
73
+```
74
+
75
+```SQL
76
+SELECT  SUM(ID), AVG(ID) FROM PEOPLE;
77
+```
78
+
79
+```SQL
80
+SELECT SUM(ID) AS sum, AVG(ID) AS avg FROM PEOPLE;
81
+```
82
+
83
+```SQL
84
+SELECT MIN(birthday) FROM PEOPLE;
85
+```
86
+
87
+## Strings
88
+
89
+```SQL
90
+SELECT UPPER (FRIST_NAME), LOWER(LAST_NAME) FROM PEOPLE;
91
+```
92
+
93
+```SQL
94
+SELECT REPLACE(LAST_NAME, 'a', '1') FROM PEOPLE;
95
+```
96
+
97
+```SQL
98
+SELECT LAST_NAME FROM PEOPLE;
99
+```
100
+
101
+```SQL
102
+INSERT INTO people (FIRST_NAME, LAST_NAME, MOBILE) 
103
+VALUES ('Otto', 'Von Count', '656-6548');
104
+```
105
+
106
+```SQL
107
+SELECT CONCAT(FIRST_NAME, LAST_NAME) FROM people
108
+WHERE LAST_NAME = 'Smith'
109
+```
110
+
111
+```SQL
112
+SELECT CONCAT(first_name, ' ', last_name) 
113
+FROM people 
114
+WHERE last_name = 'Smith'
115
+```
116
+
117
+```SQL
118
+SELECT CONCAT_WS(' ',first_name, last_name, mobile) 
119
+FROM people WHERE last_name= 'Smith'
120
+```
121
+
122
+```SQL
123
+SELECT HOMENUMBER, LEFT(HOMENUMBER, 3), RIGHT(HOMENUMBER, 2) FROM HOME
124
+```
125
+
126
+```SQL
127
+SELECT LENGTH(address), CHAR_LENGTH(address) FROM HOME;
128
+```
129
+
130
+```SQL
131
+CREATE TABLE length_test (string varchar(10) );
132
+```
133
+
134
+```SQL
135
+INSERT INTO length_test VALUES ('$'),('€');
136
+```
137
+
138
+```SQL
139
+SELECT string, LENGTH(string), CHAR_LENGTH(string) FROM length_test
140
+```
141
+
142
+## Compare
143
+
144
+```SQL
145
+SELECT first_name, last_name, YEAR(birthday) FROM people WHERE birthday >= '1970-07-06' AND birthday<='1987-07-06';
146
+```
147
+
148
+```SQL
149
+SELECT first_name, birthday FROM people WHERE first_name='Thomas' OR first_name='Raj' OR first_name='Sheeri';
150
+```
151
+
152
+```SQL
153
+SELECT first_name, birthday FROM people WHERE first_name IN ('Noelle', 'Thomas', 'Raj');
154
+```
155
+
156
+#### Wild Cards
157
+
158
+```SQL
159
+SELECT first_name FROM PEOPLE WHERE RIGHT(first_name,1)='e';
160
+```
161
+
162
+```SQL
163
+SELECT first_name FROM people WHERE first_name LIKE '%j'; 
164
+```
165
+
166
+```SQL
167
+SELECT first_name FROM people WHERE first_name LIKE '%o%';
168
+```
169
+
170
+```SQL
171
+SELECT first_name FROM people WHERE first_name NOT LIKE '%o%';
172
+```
173
+
174
+```SQL
175
+SELECT COUNT(*) FROM PEOPLE
176
+```
177
+
178
+```SQL
179
+SELECT last_name, COUNT(*) FROM people GROUP BY last_name;
180
+```
181
+
182
+```SQL
183
+SELECT last_name, GROUP_CONCAT(mobile) FROM PEOPLE GROUP BY last_name;
184
+```
185
+
186
+```SQL
187
+SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people GROUP BY last_name;
188
+```
189
+
190
+```SQL
191
+SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people GROUP BY last_name  HAVING COUNT(*)>1;
192
+```
193
+
194
+```SQL
195
+SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people WHERE last_name != 'Cabral' GROUP BY last_name  HAVING COUNT(*)>1;
196
+```
197
+
198
+#### Sorting 
199
+
200
+```SQL
201
+SELECT first_name, birthday FROM people ORDER BY birthday;
202
+```
203
+
204
+```SQL
205
+SELECT first_name, birthday FROM people ORDER BY birthday DESC;
206
+```
207
+
208
+```SQL
209
+SELECT first_name, last_name FROM people ORDER BY last_name, first_name;
210
+```
211
+
212
+```SQL
213
+SELECT first_name, birthday FROM people ORDER BY birthday DESC LIMIT 3;
214
+```
215
+
216
+```SQL
217
+SELECT first_name, MONTHNAME(birthday) as mon, birthday FROM people ORDER BY MONTH(birthday);
218
+```
219
+
220
+```SQL
221
+SELECT last_name, COUNT(*) FROM  people GROUP BY last_name;
222
+```
223
+
224
+```SQL
225
+SELECT last_name, COUNT(*) FROM  people GROUP BY last_name ORDER BY NULL;
226
+```
227
+
228
+## Inserting and Replacing Records
229
+
230
+```SQL
231
+INSERT INTO people (first_name, last_name, birthday, home_id)
232
+	VALUES
233
+	('John', 'Smith', '1998-04-07', 4),
234
+	('Maya', 'Wasserman' , NULL, 4),
235
+	('Paul', 'Thompson', '1996-05-27', 1);
236
+```
237
+
238
+#### Replace
239
+
240
+```SQL
241
+DELETE FROM people WHERE first_name='Maya';
242
+```
243
+
244
+```SQL
245
+SELECT * FROM people;
246
+```
247
+
248
+```SQL
249
+MERGE INTO people (first_name, last_name, birthday, home_id)
250
+	VALUES
251
+	('John', 'Sharma', '1998-04-07', 1),
252
+	('Paul', 'Sharma', '1996-05-27', 4),
253
+	('Maya', 'Wasserman', '1900-01-05',1);
254
+```
255
+
256
+
257
+## JOIN
258
+
259
+```SQL
260
+INSERT INTO people (first_name, last_name, birthday)
261
+	VALUES ('Eli', 'Kramer', '1984-01-15');
262
+```
263
+	
264
+	
265
+```SQL
266
+SELECT * FROM people;
267
+```
268
+
269
+```SQL
270
+SELECT * FROM home;
271
+```
272
+
273
+```SQL
274
+SELECT p.first_name, h.address 
275
+FROM PEOPLE p
276
+INNER JOIN HOME h  on (p.HOME_ID = h.ID)
277
+```
278
+
279
+```SQL
280
+SELECT first_name, last_name
281
+FROM PEOPLE p
282
+INNER JOIN HOME h  on (p.HOME_ID = h.HOME_ID)
283
+WHERE p.HOME_ID = 1
284
+```
285
+
286
+```SQL
287
+SELECT p.*, h.address, h.homenumber
288
+FROM PEOPLE p
289
+INNER JOIN HOME h  on (p.HOME_ID = h.HOME_ID)
290
+WHERE p.first_name  LIKE '%e%'
291
+```
292
+
293
+##### Exercise:
294
+
295
+Devise a report
296
+	show all the people in your address table
297
+	only if you know their birthday
298
+	show name, address and birthday
299
+	order by month, so January birthdays are first
300
+
301
+Devise a report
302
+	Output all information for all people and their home information
303
+
304
+
305
+	
306
+## UNION
307
+
308
+The UNION operator is used to combine the result-set of two or more SELECT statements.
309
+
310
+Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.
311
+
312
+SELECT _column\_name(s)_ FROM table1
313
+	UNION
314
+SELECT _column\_name(s)_ FROM table2;
315
+
316
+Note: The UNION operator selects only distinct values by default. To allow duplicate values, use the ALL keyword with UNION.
317
+
318
+SELECT _column\_name(s)_ FROM table1
319
+	UNION ALL
320
+SELECT _column\_name(s)_ FROM table2;
321
+
322
+##### Example
323
+
324
+```SQL
325
+SELECT HOME_ID FROM People
326
+UNION
327
+SELECT HOME_ID FROM Home
328
+ORDER 	BY HOME_ID; 
329
+```
330
+
331
+```SQL
332
+SELECT HOME_ID FROM People
333
+UNION ALL
334
+SELECT HOME_ID FROM Home
335
+ORDER 	BY HOME_ID; 
336
+```
337
+
338
+
339
+