Skip to content

Commit 4441669

Browse files
authored
Update #6 Marketing Campaign Analysis.sql
1 parent 838c4f5 commit 4441669

File tree

1 file changed

+301
-0
lines changed

1 file changed

+301
-0
lines changed
Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,302 @@
1+
# Date created and Last ran : 10-20-2023
2+
# Queries to solve Steel Data Challenge
3+
# Challenge 6 - Marketing Analysis
4+
5+
USE steel_data ;
6+
-- Lets create the Tables Required
7+
-- Create the table
8+
CREATE TABLE sustainable_clothing (
9+
product_id INT PRIMARY KEY,
10+
product_name VARCHAR(100),
11+
category VARCHAR(50),
12+
size VARCHAR(10),
13+
price FLOAT
14+
);
15+
-- Insert data into the table
16+
INSERT INTO sustainable_clothing (product_id, product_name, category, size, price)
17+
VALUES
18+
(1, 'Organic Cotton T-Shirt', 'Tops', 'S', 29.99),
19+
(2, 'Recycled Denim Jeans', 'Bottoms', 'M', 79.99),
20+
(3, 'Hemp Crop Top', 'Tops', 'L', 24.99),
21+
(4, 'Bamboo Lounge Pants', 'Bottoms', 'XS', 49.99),
22+
(5, 'Eco-Friendly Hoodie', 'Outerwear', 'XL', 59.99),
23+
(6, 'Linen Button-Down Shirt', 'Tops', 'M', 39.99),
24+
(7, 'Organic Cotton Dress', 'Dresses', 'S', 69.99),
25+
(8, 'Sustainable Swim Shorts', 'Swimwear', 'L', 34.99),
26+
(9, 'Recycled Polyester Jacket', 'Outerwear', 'XL', 89.99),
27+
(10, 'Bamboo Yoga Leggings', 'Activewear', 'XS', 54.99),
28+
(11, 'Hemp Overalls', 'Bottoms', 'M', 74.99),
29+
(12, 'Organic Cotton Sweater', 'Tops', 'L', 49.99),
30+
(13, 'Cork Sandals', 'Footwear', 'S', 39.99),
31+
(14, 'Recycled Nylon Backpack', 'Accessories', 'One Size', 59.99),
32+
(15, 'Organic Cotton Skirt', 'Bottoms', 'XS', 34.99),
33+
(16, 'Hemp Baseball Cap', 'Accessories', 'One Size', 24.99),
34+
(17, 'Upcycled Denim Jacket', 'Outerwear', 'M', 79.99),
35+
(18, 'Linen Jumpsuit', 'Dresses', 'L', 69.99),
36+
(19, 'Organic Cotton Socks', 'Accessories', 'M', 9.99),
37+
(20, 'Bamboo Bathrobe', 'Loungewear', 'XL', 69.99);
38+
-- Create the table
39+
CREATE TABLE marketing_campaigns (
40+
campaign_id INT PRIMARY KEY,
41+
campaign_name VARCHAR(100),
42+
product_id INT,
43+
start_date DATE,
44+
end_date DATE,
45+
FOREIGN KEY (product_id) REFERENCES sustainable_clothing (product_id)
46+
);
47+
-- Insert data into the table
48+
INSERT INTO marketing_campaigns (campaign_id, campaign_name, product_id, start_date, end_date)
49+
VALUES
50+
(1, 'Summer Sale', 2, '2023-06-01', '2023-06-30'),
51+
(2, 'New Collection Launch', 10, '2023-07-15', '2023-08-15'),
52+
(3, 'Super Save', 7, '2023-08-20', '2023-09-15');
53+
-- Create the table
54+
CREATE TABLE transactions2 (
55+
transaction_id INT PRIMARY KEY,
56+
product_id INT,
57+
quantity INT,
58+
purchase_date DATE,
59+
FOREIGN KEY (product_id) REFERENCES sustainable_clothing (product_id)
60+
);
61+
-- Insert data into the table
62+
INSERT INTO transactions2 (transaction_id, product_id, quantity, purchase_date)
63+
VALUES
64+
(1, 2, 2, '2023-06-02'),
65+
(2, 14, 1, '2023-06-02'),
66+
(3, 5, 2, '2023-06-05'),
67+
(4, 2, 1, '2023-06-07'),
68+
(5, 19, 2, '2023-06-10'),
69+
(6, 2, 1, '2023-06-13'),
70+
(7, 16, 1, '2023-06-13'),
71+
(8, 10, 2, '2023-06-15'),
72+
(9, 2, 1, '2023-06-18'),
73+
(10, 4, 1, '2023-06-22'),
74+
(11, 18, 2, '2023-06-26'),
75+
(12, 2, 1, '2023-06-30'),
76+
(13, 13, 1, '2023-06-30'),
77+
(14, 4, 1, '2023-07-04'),
78+
(15, 6, 2, '2023-07-08'),
79+
(16, 15, 1, '2023-07-08'),
80+
(17, 9, 2, '2023-07-12'),
81+
(18, 20, 1, '2023-07-12'),
82+
(19, 11, 1, '2023-07-16'),
83+
(20, 10, 1, '2023-07-20'),
84+
(21, 12, 2, '2023-07-24'),
85+
(22, 5, 1, '2023-07-29'),
86+
(23, 10, 1, '2023-07-29'),
87+
(24, 10, 1, '2023-08-03'),
88+
(25, 19, 2, '2023-08-08'),
89+
(26, 3, 1, '2023-08-14'),
90+
(27, 10, 1, '2023-08-14'),
91+
(28, 16, 2, '2023-08-20'),
92+
(29, 18, 1, '2023-08-27'),
93+
(30, 12, 2, '2023-09-01'),
94+
(31, 13, 1, '2023-09-05'),
95+
(32, 7, 1, '2023-09-05'),
96+
(33, 6, 1, '2023-09-10'),
97+
(34, 15, 2, '2023-09-14'),
98+
(35, 9, 1, '2023-09-14'),
99+
(36, 11, 2, '2023-09-19'),
100+
(37, 17, 1, '2023-09-23'),
101+
(38, 2, 1, '2023-09-28'),
102+
(39, 14, 1, '2023-09-28'),
103+
(40, 5, 2, '2023-09-30'),
104+
(41, 16, 1, '2023-10-01'),
105+
(42, 12, 2, '2023-10-01'),
106+
(43, 1, 1, '2023-10-01'),
107+
(44, 7, 1, '2023-10-02'),
108+
(45, 18, 2, '2023-10-03'),
109+
(46, 12, 1, '2023-10-03'),
110+
(47, 13, 1, '2023-10-04'),
111+
(48, 4, 1, '2023-10-05'),
112+
(49, 12, 2, '2023-10-05'),
113+
(50, 7, 1, '2023-10-06'),
114+
(51, 4, 2, '2023-10-08'),
115+
(52, 8, 2, '2023-10-08'),
116+
(53, 16, 1, '2023-10-09'),
117+
(54, 19, 1, '2023-10-09'),
118+
(55, 1, 1, '2023-10-10'),
119+
(56, 18, 2, '2023-10-10'),
120+
(57, 2, 1, '2023-10-10'),
121+
(58, 15, 2, '2023-10-11'),
122+
(59, 17, 2, '2023-10-13'),
123+
(60, 13, 1, '2023-10-13'),
124+
(61, 10, 2, '2023-10-13'),
125+
(62, 9, 1, '2023-10-13'),
126+
(63, 19, 2, '2023-10-13'),
127+
(64, 20, 1, '2023-10-14');
128+
129+
-- Business Questions to Answer
130+
131+
-- 1. How many transactions were completed during each marketing campaign?
132+
133+
SELECT mc.campaign_name,
134+
COUNT(t.transaction_id) as transaction_count
135+
FROM
136+
marketing_campaigns mc
137+
JOIN
138+
transactions2 t ON mc.product_id = t.product_id
139+
GROUP BY
140+
mc.campaign_name;
141+
142+
-- 2. Which product had the highest sales quantity?
143+
144+
SELECT
145+
sc.product_name,
146+
SUM(t.quantity) as total_quantity_sold
147+
FROM
148+
sustainable_clothing sc
149+
JOIN
150+
transactions2 t ON sc.product_id = t.product_id
151+
GROUP BY
152+
sc.product_name
153+
ORDER BY
154+
total_quantity_sold DESC
155+
LIMIT 1;
156+
157+
-- 3. What is the total revenue generated from each marketing campaign?
158+
159+
SELECT
160+
mc.campaign_name,
161+
ROUND(SUM(t.quantity * sc.price),2) as total_revenue
162+
FROM
163+
marketing_campaigns mc
164+
JOIN
165+
transactions2 t ON mc.product_id = t.product_id
166+
JOIN
167+
sustainable_clothing sc ON mc.product_id = sc.product_id
168+
GROUP BY
169+
mc.campaign_name;
170+
171+
-- 4. What is the top-selling product category based on the total revenue generated?
172+
SELECT
173+
sc.category,
174+
ROUND(SUM(t.quantity * sc.price),2) as total_revenue
175+
FROM
176+
sustainable_clothing sc
177+
JOIN
178+
transactions2 t ON sc.product_id = t.product_id
179+
GROUP BY
180+
sc.category
181+
ORDER BY
182+
total_revenue DESC
183+
LIMIT 1;
184+
185+
-- 5. Which products had a higher quantity sold compared to the average quantity sold?
186+
SELECT
187+
sc.product_name,
188+
SUM(t.quantity) as total_quantity_sold
189+
FROM
190+
sustainable_clothing sc
191+
JOIN
192+
transactions2 t ON sc.product_id = t.product_id
193+
GROUP BY
194+
sc.product_name
195+
HAVING
196+
total_quantity_sold > (SELECT AVG(quantity) FROM transactions2)
197+
ORDER BY total_quantity_sold DESC;
198+
199+
-- 6. What is the average revenue generated per day during the marketing campaigns?
200+
SELECT
201+
mc.campaign_name,
202+
ROUND(AVG(t.quantity * sc.price),2) as average_daily_revenue
203+
FROM
204+
marketing_campaigns mc
205+
JOIN
206+
transactions2 t ON mc.product_id = t.product_id
207+
JOIN
208+
sustainable_clothing sc ON mc.product_id = sc.product_id
209+
GROUP BY
210+
mc.campaign_name
211+
ORDER BY
212+
mc.campaign_name;
213+
214+
-- 7. What is the percentage contribution of each product to the total revenue?
215+
WITH product_revenue AS (
216+
SELECT
217+
sc.product_name,
218+
ROUND(SUM(t.quantity * sc.price),2) as total_revenue
219+
FROM
220+
sustainable_clothing sc
221+
JOIN
222+
transactions2 t ON sc.product_id = t.product_id
223+
GROUP BY
224+
sc.product_name
225+
)
226+
SELECT
227+
product_name,
228+
total_revenue,
229+
ROUND((total_revenue / (SELECT SUM(total_revenue) FROM product_revenue)) * 100,2) as percentage_contribution
230+
FROM
231+
product_revenue
232+
ORDER BY
233+
total_revenue DESC;
234+
235+
-- 8. Compare the average quantity sold during marketing campaigns to outside the marketing campaigns
236+
WITH campaign_quantity AS (
237+
SELECT mc.campaign_name,
238+
AVG(t.quantity) as average_quantity_sold
239+
FROM
240+
marketing_campaigns mc
241+
JOIN
242+
transactions2 t ON mc.product_id = t.product_id
243+
GROUP BY
244+
mc.campaign_name
245+
),
246+
outside_campaign_quantity AS (
247+
SELECT 'Outside Campaigns' as campaign_name,
248+
AVG(t.quantity) as average_quantity_sold
249+
FROM
250+
transactions2 t
251+
WHERE
252+
t.product_id NOT IN (SELECT product_id FROM marketing_campaigns)
253+
)
254+
SELECT * FROM campaign_quantity
255+
UNION ALL
256+
SELECT * FROM outside_campaign_quantity;
257+
258+
-- 9. Compare the revenue generated by products inside the marketing campaigns to outside the campaigns
259+
WITH campaign_revenue AS (
260+
SELECT
261+
mc.campaign_name,
262+
ROUND(SUM(t.quantity * sc.price),2) as total_revenue
263+
FROM
264+
marketing_campaigns mc
265+
JOIN
266+
transactions2 t ON mc.product_id = t.product_id
267+
JOIN
268+
sustainable_clothing sc ON mc.product_id = sc.product_id
269+
GROUP BY
270+
mc.campaign_name
271+
),
272+
outside_campaign_revenue AS (
273+
SELECT
274+
'Outside Campaigns' as campaign_name,
275+
ROUND(SUM(t.quantity * sc.price),2) as total_revenue
276+
FROM
277+
transactions2 t
278+
JOIN
279+
sustainable_clothing sc ON t.product_id = sc.product_id
280+
WHERE
281+
t.product_id NOT IN (SELECT product_id FROM marketing_campaigns)
282+
)
283+
SELECT * FROM campaign_revenue
284+
UNION ALL
285+
SELECT * FROM outside_campaign_revenue;
286+
287+
-- 10. Rank the products by their average daily quantity sold
288+
WITH product_avg_quantity AS (
289+
SELECT
290+
sc.product_name,
291+
ROUND(AVG(t.quantity),2) as average_daily_quantity_sold
292+
FROM
293+
transactions2 t
294+
JOIN
295+
sustainable_clothing sc ON t.product_id = sc.product_id
296+
GROUP BY
297+
sc.product_name
298+
)
299+
SELECT *
300+
FROM product_avg_quantity
301+
ORDER BY average_daily_quantity_sold DESC;
1302

0 commit comments

Comments
 (0)