-
Notifications
You must be signed in to change notification settings - Fork 0
/
SUSTAINABLE_CLOTHING.sql
313 lines (258 loc) · 8.66 KB
/
SUSTAINABLE_CLOTHING.sql
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
CREATE TABLE sustainable_clothing (
product_id INT PRIMARY KEY,
product_name VARCHAR(100),
category VARCHAR(50),
size VARCHAR(10),
price FLOAT
);
INSERT INTO sustainable_clothing (product_id, product_name, category, size, price)
VALUES
(1, 'Organic Cotton T-Shirt', 'Tops', 'S', 29.99),
(2, 'Recycled Denim Jeans', 'Bottoms', 'M', 79.99),
(3, 'Hemp Crop Top', 'Tops', 'L', 24.99),
(4, 'Bamboo Lounge Pants', 'Bottoms', 'XS', 49.99),
(5, 'Eco-Friendly Hoodie', 'Outerwear', 'XL', 59.99),
(6, 'Linen Button-Down Shirt', 'Tops', 'M', 39.99),
(7, 'Organic Cotton Dress', 'Dresses', 'S', 69.99),
(8, 'Sustainable Swim Shorts', 'Swimwear', 'L', 34.99),
(9, 'Recycled Polyester Jacket', 'Outerwear', 'XL', 89.99),
(10, 'Bamboo Yoga Leggings', 'Activewear', 'XS', 54.99),
(11, 'Hemp Overalls', 'Bottoms', 'M', 74.99),
(12, 'Organic Cotton Sweater', 'Tops', 'L', 49.99),
(13, 'Cork Sandals', 'Footwear', 'S', 39.99),
(14, 'Recycled Nylon Backpack', 'Accessories', 'One Size', 59.99),
(15, 'Organic Cotton Skirt', 'Bottoms', 'XS', 34.99),
(16, 'Hemp Baseball Cap', 'Accessories', 'One Size', 24.99),
(17, 'Upcycled Denim Jacket', 'Outerwear', 'M', 79.99),
(18, 'Linen Jumpsuit', 'Dresses', 'L', 69.99),
(19, 'Organic Cotton Socks', 'Accessories', 'M', 9.99),
(20, 'Bamboo Bathrobe', 'Loungewear', 'XL', 69.99);
CREATE TABLE marketing_campaigns (
campaign_id INT PRIMARY KEY,
campaign_name VARCHAR(100),
product_id INT,
start_date DATE,
end_date DATE,
FOREIGN KEY (product_id) REFERENCES sustainable_clothing (product_id)
);
INSERT INTO marketing_campaigns (campaign_id, campaign_name, product_id, start_date, end_date)
VALUES
(1, 'Summer Sale', 2, '2023-06-01', '2023-06-30'),
(2, 'New Collection Launch', 10, '2023-07-15', '2023-08-15'),
(3, 'Super Save', 7, '2023-08-20', '2023-09-15');
CREATE TABLE transactions (
transaction_id INT PRIMARY KEY,
product_id INT,
quantity INT,
purchase_date DATE,
FOREIGN KEY (product_id) REFERENCES sustainable_clothing (product_id)
);
INSERT INTO transactions (transaction_id, product_id, quantity, purchase_date)
VALUES
(1, 2, 2, '2023-06-02'),
(2, 14, 1, '2023-06-02'),
(3, 5, 2, '2023-06-05'),
(4, 2, 1, '2023-06-07'),
(5, 19, 2, '2023-06-10'),
(6, 2, 1, '2023-06-13'),
(7, 16, 1, '2023-06-13'),
(8, 10, 2, '2023-06-15'),
(9, 2, 1, '2023-06-18'),
(10, 4, 1, '2023-06-22'),
(11, 18, 2, '2023-06-26'),
(12, 2, 1, '2023-06-30'),
(13, 13, 1, '2023-06-30'),
(14, 4, 1, '2023-07-04'),
(15, 6, 2, '2023-07-08'),
(16, 15, 1, '2023-07-08'),
(17, 9, 2, '2023-07-12'),
(18, 20, 1, '2023-07-12'),
(19, 11, 1, '2023-07-16'),
(20, 10, 1, '2023-07-20'),
(21, 12, 2, '2023-07-24'),
(22, 5, 1, '2023-07-29'),
(23, 10, 1, '2023-07-29'),
(24, 10, 1, '2023-08-03'),
(25, 19, 2, '2023-08-08'),
(26, 3, 1, '2023-08-14'),
(27, 10, 1, '2023-08-14'),
(28, 16, 2, '2023-08-20'),
(29, 18, 1, '2023-08-27'),
(30, 12, 2, '2023-09-01'),
(31, 13, 1, '2023-09-05'),
(32, 7, 1, '2023-09-05'),
(33, 6, 1, '2023-09-10'),
(34, 15, 2, '2023-09-14'),
(35, 9, 1, '2023-09-14'),
(36, 11, 2, '2023-09-19'),
(37, 17, 1, '2023-09-23'),
(38, 2, 1, '2023-09-28'),
(39, 14, 1, '2023-09-28'),
(40, 5, 2, '2023-09-30'),
(41, 16, 1, '2023-10-01'),
(42, 12, 2, '2023-10-01'),
(43, 1, 1, '2023-10-01'),
(44, 7, 1, '2023-10-02'),
(45, 18, 2, '2023-10-03'),
(46, 12, 1, '2023-10-03'),
(47, 13, 1, '2023-10-04'),
(48, 4, 1, '2023-10-05'),
(49, 12, 2, '2023-10-05'),
(50, 7, 1, '2023-10-06'),
(51, 4, 2, '2023-10-08'),
(52, 8, 2, '2023-10-08'),
(53, 16, 1, '2023-10-09'),
(54, 19, 1, '2023-10-09'),
(55, 1, 1, '2023-10-10'),
(56, 18, 2, '2023-10-10'),
(57, 2, 1, '2023-10-10'),
(58, 15, 2, '2023-10-11'),
(59, 17, 2, '2023-10-13'),
(60, 13, 1, '2023-10-13'),
(61, 10, 2, '2023-10-13'),
(62, 9, 1, '2023-10-13'),
(63, 19, 2, '2023-10-13'),
(64, 20, 1, '2023-10-14');
select * from marketing_campaigns
select * from transactions
select * from sustainable_clothing;
--1. How many transactions were completed during each marketing campaign?
select * from marketing_campaigns
SELECT
ma.campaign_name,
count(t.transaction_id)
FROM
sustainable_clothing su
join marketing_campaigns ma on su.product_id = ma.product_id
join transactions t on su.product_id = t.product_id
GROUP BY 1
ORDER BY 2 desc;
--2. Which product had the highest sales quantity?
SELECT * FROM transactions
SELECT * FROM sustainable_clothing
SELECT
s.product_name,
SUM(t.quantity) AS sales_quatity
FROM sustainable_clothing AS s JOIN transactions AS t
ON s.product_id = t.product_id
GROUP BY 1
ORDER BY 2 DESC
--3. What is the total revenue generated from each marketing campaign?
SELECT * FROM marketing_campaigns
SELECT * FROM transactions
SELECT * FROM sustainable_clothing
revenue = t.quantity * s.price
SELECT
m.campaign_name,
SUM(t.quantity*s.price) AS revenue
FROM marketing_campaigns AS m JOIN transactions AS t
ON m.product_id = t.product_id
JOIN sustainable_clothing AS s
ON t.product_id = s.product_id
GROUP BY 1
ORDER BY 2 DESC
--4. What is the top-selling product category based on the total revenue generated?
SELECT
s.category,
SUM(t.quantity*s.price) AS revenue
FROM transactions AS t
JOIN sustainable_clothing AS s
ON s.product_id = t.product_id
GROUP BY 1
ORDER BY revenue DESC
LIMIT 3
--5. Which products had a higher quantity sold compared to the average quantity sold?
SELECT
s.product_name,
sum(t.quantity) AS total_quantity
FROM transactions AS t JOIN sustainable_clothing AS s
ON s.product_id = t.product_id
WHERE t.quantity > (SELECT avg(quantity) FROM transactions)
GROUP BY 1
ORDER BY 2 DESC
/*6.What is the average revenue generated per
day during the marketing campaigns?*/
SELECT * FROM transactions;
SELECT * FROM sustainable_clothing;
SELECT
campaign_name,
sum(avg_daily_revenue) as avg_sales_per_day
FROM (
SELECT ma.campaign_name,ma.product_id, su.price,t.quantity,
(su.price * t.quantity)as Revenue,
(ma.end_date - ma.start_date) as sales_duration,
((su.price * t.quantity) / (ma.end_date - ma.start_date)) as avg_daily_revenue
FROM
sustainable_clothing su
join marketing_campaigns ma on su.product_id = ma.product_id
join transactions t on su.product_id = t.product_id
where ma.campaign_name IN ('Summer Sale','New Collection Launch','Super Save')
AND purchase_date BETWEEN ma.start_date and ma.end_date)
Group by 1;
-- -7. What is the percentage contribution of each product to the total revenue?
SELECT * FROM TRANSACTIONS;
SELECT * FROM MARKETING_CAMPAIGNS;
SELECT * FROM SUSTAINABLE_CLOTHING;
SELECT
SUM(s.price * t.quantity)AS total_revenue
FROM sustainable_clothing as s JOIN transactions AS t
ON s.product_id = t.product_id
SELECT
s.product_name,
((sum(s.price * t.quantity) / (SELECT
SUM(s.price * t.quantity) AS total_revenue
FROM sustainable_clothing as s JOIN transactions AS t
ON s.product_id = t.product_id )) * 100) AS percent_contribution
FROM sustainable_clothing as s JOIN transactions AS t
ON s.product_id = t.product_id
GROUP BY 1
ORDER BY percent_contribution DESC
--------------------------
--8. Compare the average quantity sold during marketing campaigns to outside the
--marketing campaigns
-- AVG QTY SOLD DURING MARKETING CAMPAIGNS
SELECT
avg(t.quantity) AS avg_quantity
FROM transactions AS t JOIN sustainable_clothing AS s
ON t.product_id = s.product_id
JOIN marketing_campaigns AS m
ON s.product_id = m.product_id
WHERE t.purchase_date BETWEEN m.start_date AND m.end_date
---AVG QTY SOLD OUTSIDE MARKETING CAMPAIGNS
SELECT
avg(t.quantity) AS avg_quantity
FROM transactions AS t JOIN sustainable_clothing AS s
ON t.product_id = s.product_id
JOIN marketing_campaigns AS m
ON s.product_id = m.product_id
WHERE t.purchase_date NOT BETWEEN m.start_date AND m.end_date
-------------------------------------------
--9. Compare the revenue generated by products inside the
--marketing campaigns to outside the campaigns
-- REVENUE GENERATED DURING MARKETING CAMPAIGN
SELECT
sum(s.price * t.quantity) AS total_revenue
FROM transactions AS t JOIN sustainable_clothing AS s
ON t.product_id = s.product_id
JOIN marketing_campaigns AS m
ON s.product_id = m.product_id
WHERE t.purchase_date BETWEEN m.start_date AND m.end_date
--REVENUE GENERATED OUTSIDE MARKETING CAMPAIGN
SELECT
sum(s.price * t.quantity) AS total_revenue
FROM transactions AS t JOIN sustainable_clothing AS s
ON t.product_id = s.product_id
JOIN marketing_campaigns AS m
ON s.product_id = m.product_id
WHERE t.purchase_date NOT BETWEEN m.start_date AND m.end_date
---------------------------------------------
--10. Rank the products by their average daily quantity sold
SELECT
t.purchase_date,
s.product_name,
avg(t.quantity) AS avg_quantity
FROM transactions AS t
JOIN sustainable_clothing AS s
ON t.product_id = s.product_id
GROUP BY 1,2
ORDER BY avg_quantity DESC