-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09-set-operations.sql
More file actions
272 lines (219 loc) · 7.93 KB
/
Copy path09-set-operations.sql
File metadata and controls
272 lines (219 loc) · 7.93 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
-- ============================================================
-- SQL Masterclass — Chapter 09: Set Operations
-- ============================================================
-- 🟡 INTERMEDIATE
--
-- In this chapter you will learn:
-- • UNION — combine results, remove duplicates
-- • UNION ALL — combine results, keep duplicates
-- • INTERSECT — rows in both queries
-- • EXCEPT — rows in first but not second
-- • Rules for set operations
-- • Practical use cases
-- ============================================================
-- ============================================================
-- 9.1 UNION — Combine and deduplicate
-- ============================================================
-- Combines results of two queries and removes duplicates.
-- Both queries must have the same number of columns and
-- compatible data types.
-- All unique cities from both customers and sellers
SELECT customer_city AS city, customer_state AS state
FROM customers
UNION
SELECT seller_city AS city, seller_state AS state
FROM sellers
ORDER BY state, city
LIMIT 20;
-- ============================================================
-- 9.2 UNION ALL — Combine and keep duplicates
-- ============================================================
-- Faster than UNION because no deduplication step.
-- Use when you know there are no duplicates or want to keep them.
-- Count of records in each table
SELECT 'customers' AS table_name, COUNT(*) AS row_count FROM customers
UNION ALL
SELECT 'orders', COUNT(*) FROM orders
UNION ALL
SELECT 'order_items', COUNT(*) FROM order_items
UNION ALL
SELECT 'order_payments', COUNT(*) FROM order_payments
UNION ALL
SELECT 'order_reviews', COUNT(*) FROM order_reviews
UNION ALL
SELECT 'products', COUNT(*) FROM products
UNION ALL
SELECT 'sellers', COUNT(*) FROM sellers
UNION ALL
SELECT 'geolocation', COUNT(*) FROM geolocation;
-- ============================================================
-- 9.3 UNION ALL FOR TIMELINE EVENTS
-- ============================================================
-- A common pattern: creating a unified event timeline.
-- Order lifecycle events for a specific order
SELECT
order_id,
'purchased' AS event,
order_purchase_timestamp AS event_timestamp
FROM orders
WHERE order_id = (SELECT order_id FROM orders WHERE order_status = 'delivered' LIMIT 1)
UNION ALL
SELECT
order_id,
'approved' AS event,
order_approved_at
FROM orders
WHERE order_id = (SELECT order_id FROM orders WHERE order_status = 'delivered' LIMIT 1)
AND order_approved_at IS NOT NULL
UNION ALL
SELECT
order_id,
'shipped' AS event,
order_delivered_carrier_date
FROM orders
WHERE order_id = (SELECT order_id FROM orders WHERE order_status = 'delivered' LIMIT 1)
AND order_delivered_carrier_date IS NOT NULL
UNION ALL
SELECT
order_id,
'delivered' AS event,
order_delivered_customer_date
FROM orders
WHERE order_id = (SELECT order_id FROM orders WHERE order_status = 'delivered' LIMIT 1)
AND order_delivered_customer_date IS NOT NULL
ORDER BY event_timestamp;
-- ============================================================
-- 9.4 INTERSECT — Rows in BOTH queries
-- ============================================================
-- Returns only rows that appear in BOTH result sets.
-- States that have both customers AND sellers
SELECT customer_state AS state FROM customers
INTERSECT
SELECT seller_state FROM sellers
ORDER BY state;
-- Cities that have both customers and sellers
SELECT customer_city AS city FROM customers
INTERSECT
SELECT seller_city FROM sellers
ORDER BY city
LIMIT 20;
-- ============================================================
-- 9.5 EXCEPT — Rows in first but NOT second
-- ============================================================
-- Returns rows from the first query that don't exist in the second.
-- States with customers but NO sellers
SELECT DISTINCT customer_state AS state FROM customers
EXCEPT
SELECT DISTINCT seller_state FROM sellers
ORDER BY state;
-- States with sellers but NO customers
SELECT DISTINCT seller_state AS state FROM sellers
EXCEPT
SELECT DISTINCT customer_state FROM customers
ORDER BY state;
-- Product categories that exist but have never been sold
SELECT DISTINCT product_category_name FROM products
WHERE product_category_name IS NOT NULL
EXCEPT
SELECT DISTINCT p.product_category_name
FROM order_items oi
JOIN products p ON oi.product_id = p.product_id
WHERE p.product_category_name IS NOT NULL
ORDER BY product_category_name;
-- ============================================================
-- 9.6 COMBINING SET OPERATIONS WITH CTEs
-- ============================================================
-- Compare customer and seller geographic coverage
WITH customer_states AS (
SELECT DISTINCT customer_state AS state FROM customers
),
seller_states AS (
SELECT DISTINCT seller_state AS state FROM sellers
),
both_states AS (
SELECT state FROM customer_states
INTERSECT
SELECT state FROM seller_states
),
customer_only AS (
SELECT state FROM customer_states
EXCEPT
SELECT state FROM seller_states
),
seller_only AS (
SELECT state FROM seller_states
EXCEPT
SELECT state FROM customer_states
)
SELECT state, 'Both Customers & Sellers' AS coverage FROM both_states
UNION ALL
SELECT state, 'Customers Only' FROM customer_only
UNION ALL
SELECT state, 'Sellers Only' FROM seller_only
ORDER BY state;
-- ============================================================
-- 9.7 SET OPERATIONS RULES
-- ============================================================
-- 1. Same number of columns in both queries
-- 2. Matching data types (or implicitly castable)
-- 3. Column names come from the FIRST query
-- 4. ORDER BY applies to the FINAL result
-- This WORKS — matching columns
SELECT customer_city AS city, customer_state AS state FROM customers
UNION
SELECT seller_city, seller_state FROM sellers
LIMIT 5;
-- This would FAIL — different number of columns:
-- SELECT customer_city, customer_state, customer_id FROM customers
-- UNION
-- SELECT seller_city, seller_state FROM sellers;
-- ============================================================
-- EXERCISES
-- ============================================================
-- Exercise 1: Use UNION ALL to create a summary showing:
-- 'Buyers' + count of distinct customers,
-- 'Sellers' + count of distinct sellers
-- Exercise 2: Find cities that are in the customers table but
-- NOT in the sellers table. How many are there?
-- Exercise 3: Use INTERSECT to find product categories that
-- have been both sold (in order_items) AND reviewed
-- (via orders → order_reviews → order_items → products).
-- Exercise 4: Create a unified "entity" list showing all unique
-- cities from customers, sellers, and geolocation
-- tables (using UNION).
-- ============================================================
-- SOLUTIONS
-- ============================================================
-- Exercise 1
SELECT 'Buyers' AS entity, COUNT(DISTINCT customer_id) AS count FROM customers
UNION ALL
SELECT 'Sellers', COUNT(DISTINCT seller_id) FROM sellers;
-- Exercise 2
SELECT COUNT(*) AS customer_only_cities
FROM (
SELECT DISTINCT customer_city FROM customers
EXCEPT
SELECT DISTINCT seller_city FROM sellers
);
-- Exercise 3
SELECT DISTINCT p.product_category_name
FROM order_items oi
JOIN products p ON oi.product_id = p.product_id
WHERE p.product_category_name IS NOT NULL
INTERSECT
SELECT DISTINCT p.product_category_name
FROM order_reviews r
JOIN orders o ON r.order_id = o.order_id
JOIN order_items oi ON o.order_id = oi.order_id
JOIN products p ON oi.product_id = p.product_id
WHERE p.product_category_name IS NOT NULL
ORDER BY product_category_name
LIMIT 20;
-- Exercise 4
SELECT DISTINCT customer_city AS city FROM customers
UNION
SELECT DISTINCT seller_city FROM sellers
UNION
SELECT DISTINCT geolocation_city FROM geolocation
ORDER BY city
LIMIT 30;