-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-filtering-with-where.sql
More file actions
319 lines (252 loc) · 7.57 KB
/
Copy path02-filtering-with-where.sql
File metadata and controls
319 lines (252 loc) · 7.57 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
-- ============================================================
-- SQL Masterclass — Chapter 02: Filtering with WHERE
-- ============================================================
-- 🟢 BEGINNER
--
-- In this chapter you will learn:
-- • Filtering rows with WHERE
-- • Comparison operators: =, !=, <>, <, >, <=, >=
-- • Logical operators: AND, OR, NOT
-- • IN and NOT IN for lists of values
-- • BETWEEN for ranges
-- • LIKE and wildcards for pattern matching
-- • IS NULL and IS NOT NULL for missing data
-- ============================================================
-- ============================================================
-- 2.1 BASIC EQUALITY FILTER
-- ============================================================
-- Find all orders that have been delivered
SELECT *
FROM orders
WHERE order_status = 'delivered'
LIMIT 10;
-- Find all sellers from the state of São Paulo (SP)
SELECT *
FROM sellers
WHERE seller_state = 'SP'
LIMIT 10;
-- ============================================================
-- 2.2 COMPARISON OPERATORS
-- ============================================================
-- Find expensive items (price > 1000 BRL)
SELECT
order_id,
product_id,
price,
freight_value
FROM order_items
WHERE price > 1000
LIMIT 10;
-- Find cheap items (price <= 20 BRL)
SELECT
order_id,
product_id,
price
FROM order_items
WHERE price <= 20
LIMIT 10;
-- Find items where freight is more expensive than the product
SELECT
order_id,
price,
freight_value
FROM order_items
WHERE freight_value > price
LIMIT 10;
-- ============================================================
-- 2.3 NOT EQUAL: != or <>
-- ============================================================
-- Both work the same way. <> is the ANSI standard.
-- Find orders that are NOT delivered
SELECT
order_id,
order_status
FROM orders
WHERE order_status <> 'delivered'
LIMIT 10;
-- ============================================================
-- 2.4 AND — Multiple conditions (ALL must be true)
-- ============================================================
-- Find delivered orders from customer state RJ (Rio de Janeiro)
SELECT
o.order_id,
o.order_status,
c.customer_state
FROM orders o, customers c
WHERE o.customer_id = c.customer_id
AND o.order_status = 'delivered'
AND c.customer_state = 'RJ'
LIMIT 10;
-- ============================================================
-- 2.5 OR — At least ONE condition must be true
-- ============================================================
-- Find orders that are either shipped or delivered
SELECT
order_id,
order_status
FROM orders
WHERE order_status = 'shipped'
OR order_status = 'delivered'
LIMIT 10;
-- ============================================================
-- 2.6 IN — Shorthand for multiple OR conditions
-- ============================================================
-- Same as above but cleaner:
SELECT
order_id,
order_status
FROM orders
WHERE order_status IN ('shipped', 'delivered', 'invoiced')
LIMIT 10;
-- NOT IN — exclude specific values
SELECT
order_id,
order_status
FROM orders
WHERE order_status NOT IN ('delivered', 'canceled')
LIMIT 10;
-- Find sellers from the top 3 states
SELECT
seller_id,
seller_city,
seller_state
FROM sellers
WHERE seller_state IN ('SP', 'RJ', 'MG')
LIMIT 15;
-- ============================================================
-- 2.7 BETWEEN — Range filtering (inclusive)
-- ============================================================
-- Find items priced between 100 and 500 BRL
SELECT
order_id,
product_id,
price
FROM order_items
WHERE price BETWEEN 100 AND 500
LIMIT 10;
-- Note: BETWEEN is INCLUSIVE on both ends.
-- price BETWEEN 100 AND 500
-- is the same as: price >= 100 AND price <= 500
-- Find orders placed in January 2018
SELECT
order_id,
order_purchase_timestamp
FROM orders
WHERE order_purchase_timestamp BETWEEN '2018-01-01' AND '2018-01-31 23:59:59'
LIMIT 10;
-- ============================================================
-- 2.8 LIKE — Pattern matching
-- ============================================================
-- % matches any sequence of characters
-- _ matches exactly one character
-- Find cities starting with 'rio'
SELECT DISTINCT customer_city
FROM customers
WHERE customer_city LIKE 'rio%'
LIMIT 10;
-- Find cities containing 'paulo'
SELECT DISTINCT customer_city
FROM customers
WHERE customer_city LIKE '%paulo%'
LIMIT 10;
-- Find cities with exactly 3 characters
SELECT DISTINCT customer_city
FROM customers
WHERE customer_city LIKE '___'
LIMIT 10;
-- ============================================================
-- 2.9 IS NULL / IS NOT NULL
-- ============================================================
-- NULL means "no value" or "unknown". You CANNOT use = to check for NULL!
-- Find orders that have NOT been delivered yet
SELECT
order_id,
order_status,
order_delivered_customer_date
FROM orders
WHERE order_delivered_customer_date IS NULL
LIMIT 10;
-- Find orders that HAVE been delivered
SELECT
order_id,
order_status,
order_delivered_customer_date
FROM orders
WHERE order_delivered_customer_date IS NOT NULL
LIMIT 10;
-- Find reviews without any comment
SELECT
review_id,
review_score,
review_comment_message
FROM order_reviews
WHERE review_comment_message IS NULL
OR review_comment_message = ''
LIMIT 10;
-- ============================================================
-- 2.10 COMBINING EVERYTHING
-- ============================================================
-- Find high-value delivered orders placed in 2018 from São Paulo
SELECT
oi.order_id,
oi.price,
oi.freight_value
FROM order_items oi
JOIN orders o ON oi.order_id = o.order_id
JOIN customers c ON o.customer_id = c.customer_id
WHERE oi.price > 500
AND o.order_status = 'delivered'
AND c.customer_state = 'SP'
AND o.order_purchase_timestamp BETWEEN '2018-01-01' AND '2018-12-31 23:59:59'
LIMIT 10;
-- ============================================================
-- EXERCISES
-- ============================================================
-- Exercise 1: Find all orders with status 'canceled'.
-- How many are there? (use COUNT)
-- Exercise 2: Find all products where product_weight_g > 10000
-- (heavier than 10kg). Show product_id and weight.
-- Exercise 3: Find all payments made via 'boleto' (a Brazilian
-- payment method) with payment_value > 200.
-- Exercise 4: Find all customers from states 'SP', 'RJ', or 'MG'.
-- Count how many there are.
-- Exercise 5: Find all reviews with review_score of 1
-- that also have a review_comment_message (not NULL).
-- Exercise 6: Find all seller cities that start with 'sao'.
-- ============================================================
-- SOLUTIONS
-- ============================================================
-- Exercise 1
SELECT COUNT(*) AS canceled_orders
FROM orders
WHERE order_status = 'canceled';
-- Exercise 2
SELECT product_id, product_weight_g
FROM products
WHERE product_weight_g > 10000
LIMIT 15;
-- Exercise 3
SELECT order_id, payment_type, payment_value
FROM order_payments
WHERE payment_type = 'boleto'
AND payment_value > 200
LIMIT 15;
-- Exercise 4
SELECT COUNT(*) AS top3_state_customers
FROM customers
WHERE customer_state IN ('SP', 'RJ', 'MG');
-- Exercise 5
SELECT
review_id,
review_score,
review_comment_message
FROM order_reviews
WHERE review_score = 1
AND review_comment_message IS NOT NULL
AND review_comment_message != ''
LIMIT 15;
-- Exercise 6
SELECT DISTINCT seller_city
FROM sellers
WHERE seller_city LIKE 'sao%'
LIMIT 15;