-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmode_tutorial_basic.sql
More file actions
357 lines (278 loc) · 8.86 KB
/
mode_tutorial_basic.sql
File metadata and controls
357 lines (278 loc) · 8.86 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
-- 1: Using SQL in Mode
-- 2: SQL SELECT
# SELECT: indicates which columns you would like to view
# FROM: identifies the table that they live in
SELECT *
FROM tutorial.us_housing_units;
SELECT year,
month,
west
FROM tutorial.us_housing_units;
SELECT west AS "West_Region",
south AS "South_Region"
FROM tutorial.us_housing_units;
SELECT year AS "Year",
month AS "Month",
month_name AS "Month Name",
west AS "West",
midwest AS "Midwest",
south AS "South",
northeast AS "Northeast"
FROM tutorial.us_housing_units;
-- 3: SQL LIMIT
# LIMIT: use limits as a simple way to keep their queries from taking too long to return
SELECT *
FROM tutorial.us_housing_units
LIMIT 100;
SELECT *
FROM tutorial.us_housing_units
LIMIT 15;
-- 4: SQL WHERE
# WHERE: filtering the data using the WHERE
-- 5: SQL Comparison Operators
# Not equal to: <> or !=
# You can use >, <, and the rest of the comparison operators on non-numeric columns as well—they filter based on alphabetical order
SELECT *
FROM tutorial.us_housing_units
WHERE month = 1;
SELECT *
FROM tutorial.us_housing_units
WHERE west > 30;
SELECT *
FROM tutorial.us_housing_units
WHERE west > 50;
SELECT *
FROM tutorial.us_housing_units
WHERE south <= 20;
SELECT *
FROM tutorial.us_housing_units
WHERE month_name != 'January';
SELECT *
FROM tutorial.us_housing_units
WHERE month_name > 'January';
SELECT *
FROM tutorial.us_housing_units
WHERE month_name > 'J';
SELECT *
FROM tutorial.us_housing_units
WHERE month_name = 'February';
SELECT *
FROM tutorial.us_housing_units
WHERE month_name < 'O';
SELECT *
FROM tutorial.us_housing_units
WHERE month_name <= 'N';
SELECT year,
month,
west,
south,
west + south AS south_plus_west
FROM tutorial.us_housing_units;
SELECT year,
month,
west,
south,
west + south - 4 * year AS nonsense_column
FROM tutorial.us_housing_units;
SELECT
*,
west + south + midwest + northeast as total
FROM tutorial.us_housing_units;
SELECT year,
month,
west,
south,
(west + south)/2 AS south_west_avg
FROM tutorial.us_housing_units;
SELECT *
FROM tutorial.us_housing_units
WHERE west > ( midwest + northeast );
SELECT
year,
month,
south / ( south + west + midwest + northeast ) * 100 as south_pct,
west / ( south + west + midwest + northeast ) * 100 as west_pct,
midwest / ( south + west + midwest + northeast ) * 100 as midwest_pct,
northeast / ( south + west + midwest + northeast ) * 100 as northeast_pct
FROM tutorial.us_housing_units
WHERE year >= 2000;
-- 6: SQL Logical Operators
# LIKE: allows you to match similar values, instead of exact values.
# IN: allows you to specify a list of values youd like to include.
# BETWEEN: allows you to select only rows within a certain range.
# IS NULL: allows you to select rows that contain no data in a given column.
# AND: allows you to select only rows that satisfy two conditions.
# OR: allows you to select rows that satisfy either of two conditions.
# NOT: allows you to select rows that do not match a certain condition.
SELECT *
FROM tutorial.billboard_top_100_year_end
ORDER BY year DESC, year_rank;
-- 7: SQL LIKE
# %: used above represents any character or set of characters
# LIKE: case-sensitive, meaning that the above query will only capture matches (대소문자 구분!)
# ILIKE: LIKE랑 동일한 기능인데 대소문자 구분 안 함
# _ (a single underscore): substitute for an individual character
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE "group_name" LIKE 'Snoop%';
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE "group_name" ILIKE 'snoop%';
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE artist ILIKE 'dr_ke';
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE artist LIKE 'Ludacris';
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE artist LIKE 'DJ%';
-- 8: SQL IN
# IN: a logical operator in SQL that allows you to specify a list of values that youd like to include in the results
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank IN (1, 2, 3);
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE artist IN ('Taylor Swift', 'Usher', 'Ludacris');
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE artist ILIKE 'm.c.%';
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE artist ILIKE 'Elvis%';
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE artist IN ('Elvis Presley', 'M.C. Hammer');
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE artist ILIKE 'hammer';
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE "group_name" IN ('M.C. Hammer', 'Hammer', 'Elvis Presley');
-- 9: SQL BETWEEN
# BETWEEN: a logical operator in SQL that allows you to select only rows that are within a specific range
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank BETWEEN 5 AND 10;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank >= 5 AND year_rank <= 10;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year BETWEEN 1985 AND 1990; -- 주의! WHERE은 ,로 나열 못 하고 AND로만 나열 가능함
-- 10: SQL IS NULL
# IS NULL: a logical operator in SQL that allows you to exclude rows with missing data from your results
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE artist IS NULL;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE song_name IS NULL;
-- 11: SQL AND
# AND: a logical operator in SQL that allows you to select only rows that satisfy two conditions
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2012 AND year_rank <= 10;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2012
AND year_rank <= 10
AND "group_name" ILIKE '%feat%';
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank <= 10
AND artist ILIKE '%Ludacris%';
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank = 1
AND year IN (1990, 2000, 2010); -- AND 말고도 IN을 활용해서 작성하기!!
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year BETWEEN 1960 AND 1969
AND song_name ilike '%love%';
-- 12: SQL OR
# OR: a logical operator in SQL that allows you to select rows that satisfy either of two conditions
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank = 5 OR artist = 'Gotye';
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2013
AND ("group_name" ILIKE '%macklemore%' OR "group_name" ILIKE '%timberlake%');
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank <= 10
AND ("group_name" ILIKE '%Katy Perry%' OR "group_name" ILIKE '%Bon Jovi%');
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE song_name LIKE '%California%'
AND (year BETWEEN 1970 AND 1979 OR year BETWEEN 1990 AND 1999);
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE group_name ILIKE '%Dr. Dre%'
AND (year < 2001 OR year > 2009);
-- 13: SQL NOT
# NOT: a logical operator in SQL that you can put before any conditional statement to select rows for which that statement is false
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2013
AND year_rank NOT BETWEEN 2 AND 3;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2013
AND year_rank <= 3;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2013
AND "group_name" NOT ILIKE '%macklemore%';
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2013
AND artist IS NOT NULL;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2013
AND song_name NOT ilike '%a%';
-- 14: SQL ORDER BY
# ORDER BY: allows you to reorder your results based on the data in one or more columns
SELECT *
FROM tutorial.billboard_top_100_year_end
ORDER BY artist;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2013
ORDER BY year_rank;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2013
ORDER BY year_rank DESC;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2012
ORDER BY song_name DESC;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank <= 3
ORDER BY year DESC, year_rank;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank <= 3
ORDER BY year_rank, year DESC;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank <= 3
ORDER BY 2, 1 DESC; -- 2번째 컬럼은 오름차순 정렬 > 1번째 컬럼은 내림차순 정렬
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2010
ORDER BY year_rank, artist;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE group_name ILIKE '%T-Pain%'
ORDER BY year_rank DESC;
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank BETWEEN 10 AND 20
AND YEAR IN (1993, 2003, 2013)
ORDER BY year, year_rank;