-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWalmart_Sales_Data_Analysis(SQL).sql
More file actions
180 lines (148 loc) · 4.85 KB
/
Walmart_Sales_Data_Analysis(SQL).sql
File metadata and controls
180 lines (148 loc) · 4.85 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
use walmartsd
SELECT *FROM walmartsalesdata;
#Generic Questions:
#1. How many unique cities does the data have?
SELECT City, COUNT(*) FROM walmartsalesdata GROUP BY City;
#In which city is each branch?
SELECT branch, city, COUNT(*) AS count FROM walmartsalesdata GROUP BY branch, city;
#Product Analysis:
#1. How many unique product lines does the data have?
SELECT `Product line`, COUNT(*) AS count FROM walmartsalesdata GROUP BY `Product line`;
#2. What is the most common payment method?
SELECT Payment, COUNT(*) AS count
FROM walmartsalesdata
GROUP BY Payment
ORDER BY count DESC
LIMIT 1;
#3. What is the most selling product line?
SELECT `Product line`, SUM(quantity) AS total_sold
FROM walmartsalesdata
GROUP BY `Product line`
ORDER BY total_sold DESC
LIMIT 1;
#4. What is the total revenue by month?
SELECT MONTH(date) AS month, SUM(total) AS Total_Revenue
FROM walmartsalesdata
GROUP BY MONTH(date)
ORDER BY month;
#5. What month had the largest COGS?
SELECT MONTH(date) AS month, SUM(cogs) AS total_cogs
FROM walmartsalesdata
GROUP BY MONTH(date)
ORDER BY total_cogs DESC
LIMIT 1;
#6. What product line had the largest revenue?
SELECT `Product line`, SUM(total) AS total_revenue
FROM walmartsalesdata
GROUP BY `Product line`
ORDER BY total_revenue DESC
LIMIT 1;
#7. What is the city with the largest revenue?
SELECT city, SUM(total) AS total_revenue
FROM walmartsalesdata
GROUP BY city
ORDER BY total_revenue DESC
LIMIT 1;
#8. What product line had the largest VAT?
SELECT `Product line`, SUM(`Tax 5%`) AS total_vat
FROM walmartsalesdata
GROUP BY `Product line`
ORDER BY total_vat DESC
LIMIT 1;
#9. Fetch each product line and add a column to classify them as "Good" or "Bad" based on average sales.
SELECT `Product line`,
CASE
WHEN AVG(quantity) > (SELECT AVG(quantity) FROM walmartsalesdata) THEN 'Good'
ELSE 'Bad'
END AS category
FROM walmartsalesdata
GROUP BY `Product line`;
#10. Which branch sold more products than the average product sold?
SELECT branch, SUM(quantity) AS total_sold
FROM walmartsalesdata
GROUP BY branch
HAVING total_sold > (SELECT AVG(quantity) FROM walmartsalesdata);
#11. What is the most common product line by gender?
SELECT gender, `Product line`, COUNT(*) AS total_sold
FROM walmartsalesdata
GROUP BY gender, `Product line`
ORDER BY gender, total_sold DESC;
#12. What is the average rating of each product line?
SELECT `Product line`, AVG(rating) AS avg_rating
FROM walmartsalesdata
GROUP BY `Product line`
ORDER BY avg_rating DESC;
#Sales Analysis
#1. Number of sales made in each time of the day per weekday
SELECT DAYNAME(date) AS weekday, Time, COUNT(*) AS sales_count
FROM walmartsalesdata
GROUP BY weekday, Time
ORDER BY weekday;
#2. Which customer type brings the most revenue?
SELECT `Customer type` , SUM(total) AS total_revenue
FROM walmartsalesdata
GROUP BY `Customer type`
ORDER BY total_revenue DESC
LIMIT 1;
#3. Which city has the largest tax percentage (VAT)?
SELECT city, AVG(`Tax 5%`) AS avg_tax
FROM walmartsalesdata
GROUP BY city
ORDER BY avg_tax DESC
LIMIT 1;
#4. Which customer type pays the most in VAT?
SELECT `Customer type`, SUM(`Tax 5%`) AS total_vat
FROM walmartsalesdata
GROUP BY `Customer type`
ORDER BY total_vat DESC
LIMIT 1;
#Customer Analysis
#1. How many unique customer types does the data have?
SELECT customer_type, COUNT(*) AS count FROM sales GROUP BY customer_type;
#2. How many unique payment methods does the data have?
SELECT payment, COUNT(*) AS count FROM sales GROUP BY payment;
#3. What is the most common customer type?
SELECT customer_type, COUNT(*) AS count
FROM sales
GROUP BY customer_type
ORDER BY count DESC
LIMIT 1;
#4. Which customer type buys the most?
SELECT customer_type, SUM(quantity) AS total_bought
FROM sales
GROUP BY customer_type
ORDER BY total_bought DESC
LIMIT 1;
#5. What is the gender of most of the customers?
SELECT gender, COUNT(*) AS count
FROM sales
GROUP BY gender
ORDER BY count DESC
LIMIT 1;
#6. What is the gender distribution per branch?
SELECT branch, gender, COUNT(*) AS count
FROM sales
GROUP BY branch, gender
ORDER BY branch, count DESC;
#7. Which time of the day do customers give most ratings?
SELECT time_of_day, AVG(rating) AS avg_rating
FROM sales
GROUP BY time_of_day
ORDER BY avg_rating DESC
LIMIT 1;
#8. Which time of the day do customers give most ratings per branch?
SELECT branch, time_of_day, AVG(rating) AS avg_rating
FROM sales
GROUP BY branch, time_of_day
ORDER BY branch, avg_rating DESC;
#9. Which day of the week has the best average ratings?
SELECT DAYNAME(date) AS weekday, AVG(rating) AS avg_rating
FROM sales
GROUP BY weekday
ORDER BY avg_rating DESC
LIMIT 1;
#10. Which day of the week has the best average ratings per branch?
SELECT branch, DAYNAME(date) AS weekday, AVG(rating) AS avg_rating
FROM sales
GROUP BY branch, weekday
ORDER BY branch, avg_rating DESC;