-
Notifications
You must be signed in to change notification settings - Fork 0
/
Walmart Sales Analysis SQL Analysis By Hemil Shah.sql
221 lines (154 loc) · 5.91 KB
/
Walmart Sales Analysis SQL Analysis By Hemil Shah.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
select * from Walmart_Sales_Data.dbo.Walmart_Sales
-- Step 1: Add the new column basis of time
ALTER TABLE Walmart_Sales_Data.dbo.Walmart_Sales
ADD time_of_day Varchar(15);
-- Step 2: Update the new column with values based on 'Time' Column and Segregate time based on Morning,Afternoon,Evening,
UPDATE Walmart_Sales_Data.dbo.Walmart_Sales
SET time_of_day =
CASE
WHEN Time >= '06:00:00' AND Time < '12:00:00' THEN 'Morning'
WHEN Time >= '12:00:00' AND Time < '18:00:00' THEN 'Afternoon'
WHEN Time >= '18:00:00' AND Time < '00:00:00' THEN 'Evening'
ELSE 'Night'
END;
-- Step 1: Add the new column based on Day
ALTER TABLE Walmart_Sales_Data.dbo.Walmart_Sales
ADD day_name Varchar(15);
select day_name from Walmart_Sales_Data.dbo.Walmart_Sales
-- Step 2: Update the new column with values from Date and Segregate it on basis of Weekdays Name
UPDATE Walmart_Sales_Data.dbo.Walmart_Sales
SET day_name = DATENAME(weekday, Date );
-- Step 1: Add the new column based on Month
ALTER TABLE Walmart_Sales_Data.dbo.Walmart_Sales
ADD month_name Varchar(15);
select month_name from Walmart_Sales_Data.dbo.Walmart_Sales
-- Step 2: Update the new column with values from the Date Column and Segregate based on Month from January to December
UPDATE Walmart_Sales_Data.dbo.Walmart_Sales
SET month_name = DATENAME(month, Date );
--Generic Question
--1. How many unique cities does the data have?
select distinct(city) as 'Unique Cities' from Walmart_Sales
--2. In which city is each branch?
select city,branch from Walmart_Sales
group by city,branch
--Product
--1. How many unique product lines does the data have?
select distinct product_line from Walmart_Sales
--2. What is the most common payment method?
select payment_method,count(payment_method) as 'No of Payments'
from Walmart_Sales
group by payment_method
--3. What is the most selling product line?
select top 1 product_line,count(product_line) as 'No of Product_line'
from Walmart_Sales
group by product_line
order by count(product_line) desc
--4. What is the total revenue by month?
select month_name,sum(total) as 'Total Income in that Month'
from Walmart_Sales
group by month_name
--5. What month had the largest COGS?
select month_name,sum(cogs) as 'Largest COGS'
from Walmart_Sales
group by month_name
order by sum(cogs) desc
--6. What product line had the largest revenue?
select product_line,sum(total) as 'Largest Revenue'
from Walmart_Sales
group by product_line
order by sum(total) desc
--5. What is the city with the largest revenue?
select city,sum(total) as 'Largest Revenue'
from Walmart_Sales
group by city
order by sum(total) desc
--6. Fetch each product line and add a column to those product line showing "Good", "Bad". Good if its greater than average sales
WITH Sales AS (
SELECT product_line, AVG(total) AS Average_Sales
FROM Walmart_Sales
GROUP BY product_line
)
SELECT product_line,
CASE
WHEN Average_Sales > (SELECT AVG(Average_Sales) FROM Sales) THEN 'Good'
ELSE 'Bad'
END AS Classification
FROM Sales
--7. Which branch sold more products than average product sold?
with sales as
(select sum(quantity) as Total_bills,branch from Walmart_sales
group by branch)
SELECT branch,Total_bills,
CASE
WHEN Total_bills > (SELECT sum(Total_bills)/3 FROM sales) THEN 'Higher Than Average'
ELSE 'Lower Than Average'
END AS Classification
FROM sales
--8. What is the most common product line by gender?
select product_line,gender,
case
when gender = 'Male' then count(gender)
when gender ='Female'then count(gender)
end as 'gender_based_classification'
from Walmart_Sales
group by product_line,gender
order by count(gender) desc
--9. What is the average rating of each product line?
select (round(avg(rating),2)) as 'Average_Rating',product_line
from Walmart_Sales
group by product_line
--Sales
--1. Which of the customer types brings the most revenue?
select customer_type ,sum(total) as 'Total'
from Walmart_Sales
group by customer_type
--2. Which city has the largest tax percent/ VAT (**Value Added Tax**)?
with Sum_of_Vat as
(select city,sum(VAT) as Vat_Collected
from Walmart_Sales
group by City),
Sum_of_total_Vat as
(select sum(VAT) as Total_Vat_Collected
from Walmart_Sales
)
select Sum_of_Vat.city,(Vat_Collected*100/Total_Vat_Collected) as vat_percentage
from Sum_of_Vat,Sum_of_total_Vat
order by city
--Customer
--1. How many unique customer types does the data have?
select count(distinct(customer_type)) as 'Unique_customer_type' from Walmart_Sales
--2. How many unique payment methods does the data have?
select count(distinct(payment_method)) as 'Unique_Payment_Method' from Walmart_Sales
--3. What is the most common customer type?
select customer_type,count(invoice_id) as 'Total_Customer' from Walmart_Sales
group by customer_type
--4. What is the gender of most of the customers?
select gender,count(invoice_id) as 'Total_Customer'
from Walmart_Sales
group by gender;
--5. What is the gender distribution per branch?
with gender_count as
(select branch,gender,count(gender) as count_of_gender
from Walmart_Sales
group by branch,gender),
gender_total as
(select branch,COUNT(gender) as Gender
from Walmart_Sales
group by branch)
select gender_count.branch,gender_count.gender, (count_of_gender*100/gender_total.Gender) as 'Gender_percentage'
from gender_count join gender_total on gender_count.branch = gender_total.branch
order by branch,gender
--7. Which time of the day do customers give most ratings?
select time_of_day,avg(rating) as 'Rating'
from Walmart_Sales
group by time_of_day
--8. Which time of the day do customers give most ratings per branch?
select time_of_day,Branch,avg(rating) as 'Rating'
from Walmart_Sales
group by time_of_day,Branch
order by Branch,time_of_day
--9. Which day of the week has the best average ratings per branch?
select day_name,Branch,avg(rating) as 'Rating'
from Walmart_Sales
group by day_name,Branch
order by avg(rating) desc