Repository navigation
Expand file tree
/
Copy pathWalmartProjectSQL.sql
More file actions
404 lines (313 loc) · 8.72 KB
/
Copy pathWalmartProjectSQL.sql
File metadata and controls
404 lines (313 loc) · 8.72 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
CREATE database walmart_dataset;
USE walmart_dataset;
CREATE TABLE wsales(
Invoice_ID VARCHAR(50) NOT NULL PRIMARY KEY,
Branch VARCHAR(50) NOT NULL,
City VARCHAR(50) NOT NULL,
Customer_type VARCHAR(30) NOT NULL,
Gender VARCHAR(10) NOT NULL,
Product_line VARCHAR(50) NOT NULL,
Unit_price DECIMAL(10, 2) NOT NULL,
Quantity INT(20) NOT NULL,
Tax DECIMAL (10, 2) NOT NULL,
Total DECIMAL(10) NOT NULL,
Date DATE NOT NULL,
Time TIME NOT NULL,
Payment VARCHAR(20) NOT NULL,
cogs DECIMAL(10) NOT NULL,
gross_margin_pct DECIMAL(10),
gross_income DECIMAL(10) NOT NULL,
Rating DECIMAL(4) NOT NULL,
Customer_ID (50) NOT NULL
);
SELECT * FROM walmart_dataset.wsales;
USE walmart_dataset;
SELECT * FROM walmart_dataset.wsales;
## Task 1: Identifying the Top Branch by Sales Growth Rate
-- Walmart wants to identify which branch has exhibited the highest sales growth over time.
-- Analyze the total sales for each branch and compare the growth rate across months to find the top performer.
-------- Total Sales by Each Branch per Month
SELECT Branch,
DATE_FORMAT(Date, '%Y-%m') AS Month,
SUM(Total) AS Total_Sales
FROM wsales
GROUP BY Branch, Month
ORDER BY Branch, Month;
WITH MonthlySales AS(
SELECT Branch,
DATE_FORMAT(Date, '%Y-%m') AS Month,
SUM(Total) AS Total_Sales
FROM wsales
GROUP BY Branch, Month
)
-- Growth Rate
SELECT Branch, Month,
Total_Sales,
(Total_Sales - LAG(Total_Sales) OVER (PARTITION BY Branch ORDER BY Month)) / LAG(Total_Sales) OVER (PARTITION BY Branch ORDER BY Month)
AS Growth_Rate
FROM MonthlySales
ORDER BY Branch, Month;
-- Branch with Highest Growth Rate
SELECT * FROM walmart_dataset.wsales;
WITH MonthlySales AS (
SELECT
Branch,
DATE_FORMAT(Date, '%Y-%m') AS Month,
SUM(Total) AS Total_Sales
FROM
wsales
GROUP BY
Branch,
Month
),
GrowthRates AS (
SELECT
Branch,
Month,
(Total_Sales - LAG(Total_Sales) OVER (PARTITION BY Branch ORDER BY Month)) / LAG(Total_Sales) OVER (PARTITION BY Branch ORDER BY Month)
AS Growth_Rate
FROM
MonthlySales
)
-- Average Growth Rate by Branch A
SELECT
Branch,
AVG(Growth_Rate) AS Avg_Growth_Rate
FROM
GrowthRates
GROUP BY
Branch
ORDER BY
Avg_Growth_Rate DESC
LIMIT 1;
SELECT * FROM walmart_dataset.wsales;
## Task 2: Finding the Most Profitable Product Line for Each Branch
-- Walmart needs to determine which product line contributes the highest profit to each branch.
-- The profit margin should be calculated based on the difference between the gross income and cost of goods sold.
-- Calcultion of profit(gross_income) which is equal to the difference between total(cogs+tax5%) and revenue or cogs(unit price x quantity)
USE walmart_dataset;
SELECT
Branch,
Product_line,
SUM(gross_income) AS Total_Profit
FROM wsales
GROUP BY Branch, Product_line
ORDER BY Branch, Total_Profit DESC;
-- To Find the Maximum Profit each Branch:
WITH ProfitData AS (
SELECT
Branch,
Product_line,
SUM(gross_income) AS Total_Profit
FROM wsales
GROUP BY Branch, Product_line
)
-- Highest Profit Product Line
SELECT
Branch,
Product_line,
Total_Profit
FROM ProfitData
WHERE (Branch, Total_Profit) IN (
SELECT
Branch,
MAX(Total_Profit)
FROM ProfitData
GROUP BY Branch
);
SELECT * FROM walmart_dataset.wsales;
## Task 3: Analyzing Customer Segmentation Based on Spending
-- Walmart wants to segment customers based on their average spending behavior.
-- Classify customers into three tiers: High, Medium, and Low spenders based on their total purchase amounts.
-- average spending by customer
SELECT
Customer_ID,
AVG(Total) AS avg_spending
FROM wsales
GROUP BY Customer_ID;
-- TotalSpending by customer
SELECT
Customer_ID,
SUM(Total) AS total_spending
FROM wsales
GROUP BY Customer_ID;
-- -- Classify customers into three tiers: High, Medium, and Low spenders
WITH customer_spending AS (
SELECT Customer_ID, round(SUM(Total),2) AS TotalSpending
FROM wsales
GROUP BY Customer_ID
)
SELECT Customer_ID, TotalSpending,
CASE
WHEN TotalSpending < 20556 THEN 'Low'
WHEN TotalSpending BETWEEN 20556 AND 23456 THEN 'Medium'
ELSE 'High'
END AS Tiers
FROM customer_spending;
SELECT * FROM walmart_dataset.wsales;
## Task 4: Detecting Anomalies in Sales Transactions
-- Walmart suspects that some transactions have unusually high or low sales compared to the average for the product line.
-- Identify these anomalies
-- Calculate average sales per product line
WITH AvgSales AS (
SELECT
Product_line,
AVG(Total) AS Avg_Sales
FROM
wsales
GROUP BY
Product_line
)
-- compare average sales with deviation
SELECT
ws.Invoice_ID,
ws.Branch,
ws.City,
ws.Product_line,
ws.Total,
avg.Avg_Sales,
(ws.Total - avg.Avg_Sales) / avg.Avg_Sales AS Deviation
FROM
wsales AS ws
JOIN
AvgSales AS avg
ON ws.Product_line = avg.Product_line
WHERE
ABS((ws.Total - avg.Avg_Sales) / avg.Avg_Sales) > 0.5
ORDER BY
Deviation DESC;
-- Noted: ABS referes to Absolute non-negative number value which is equql to Total - Avg Sales/ Avg Sales or (Deviation) > 0.5
SELECT * FROM walmart_dataset.wsales;
## Task 5: Most Popular Payment Method by City
-- Walmart needs to determine the most popular payment method in each city to tailor marketing strategies
WITH RankedPayments AS (
SELECT City, Payment,
COUNT(*) AS Payment_Count,
ROW_NUMBER() OVER (PARTITION BY City ORDER BY COUNT(*) DESC) AS rn
FROM
wsales
GROUP BY
City, Payment
)
-- Popular Payment method and Top City
SELECT
City,
Payment,
Payment_Count
FROM
RankedPayments
WHERE
rn = 1;
SELECT * FROM walmart_dataset.wsales;
## Task 6: Monthly Sales Distribution by Gender
-- Walmart wants to understand the sales distribution between male and female customers on a monthly basis.
SELECT
DATE_FORMAT(Date, '%Y-%m') AS Month,
Gender,
SUM(Total) AS Total_Sales
FROM
wsales
GROUP BY
Month,
Gender
ORDER BY
Month,
Gender;
-- Bonus Highest Monthly Distribution by Gender
SELECT
DATE_FORMAT(Date, '%Y-%m') AS Month,
Gender,
SUM(Total) AS Total_Sales
FROM
wsales
GROUP BY
Month,
Gender
ORDER BY
Month,
Gender ASC
LIMIT 2;
SELECT * FROM walmart_dataset.wsales;
## Task 7: Best Product Line by Customer Type
-- Walmart wants to know which product lines are preferred by different customer types(Member vs. Normal).
WITH BestProductLine AS(
SELECT
Customer_type,
Product_line,
SUM(Total) AS Total_Sales,
ROW_NUMBER() OVER (PARTITION BY Customer_type ORDER BY SUM(Total) DESC) AS rn
FROM
wsales
GROUP BY
Customer_type,
Product_line
)
SELECT
Customer_type,
Product_line,
Total_Sales
FROM
BestProductLine
WHERE
rn=1;
SELECT * FROM walmart_dataset.wsales;
## Task 8: Identifying Repeat Customers
-- Walmart needs to identify customers who made repeat purchases within a specific time frame (e.g., within 30 days).
SELECT DISTINCT Customer_ID
FROM (
SELECT Customer_ID,
Date,
LAG(Date) OVER (PARTITION BY Customer_ID ORDER BY Date) AS prev_purchase_date
FROM wsales
)
-- within 30 days
AS subquery
WHERE DATEDIFF(Date, prev_purchase_date) <= 30 AND prev_purchase_date IS NOT NULL;
-- repeat purchase within 30 days
SELECT Customer_ID,
COUNT(*) AS repeat_purchase_count
FROM (
SELECT
Customer_ID,
Date,
LAG(Date) OVER (PARTITION BY Customer_ID ORDER BY Date) AS prev_purchase_date
FROM wsales
)
-- purchase count by each cutsomer
AS subquery
WHERE DATEDIFF(Date, prev_purchase_date) <= 30 AND prev_purchase_date IS NOT NULL
GROUP BY Customer_ID;
SELECT * FROM walmart_dataset.wsales;
## Task 9: Finding Top 5 Customers by Sales Volume
-- Walmart wants to reward its top 5 customers who have generated the most sales Revenue
SELECT
Customer_ID,
SUM(Total) AS Total_Sales
FROM
wsales
GROUP BY
Customer_ID
ORDER BY
Total_Sales DESC
LIMIT 5;
SELECT * FROM walmart_dataset.wsales;
## Task 10: Analyzing Sales Trends by Day of the Week (6 Marks)
-- Walmart wants to analyze the sales patterns to determine which day of the week brings the highest sales.
SELECT
DAYNAME(Date) AS Day_of_Week,
SUM(Total) AS Total_Sales
FROM
wsales
GROUP BY
Day_of_Week
ORDER BY
FIELD(Day_of_Week, 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
-- Highest sales in weekdays
SELECT
DAYNAME(Date) AS Day_of_Week,
SUM(Total) AS Total_Sales
FROM walmart_dataset.wsales
GROUP BY DAYNAME(Date)
ORDER BY Total_Sales DESC
LIMIT 1;
----------------------- END ----------------------------------------