Repository navigation
Expand file tree
/
Copy pathWalmart project2.sql
More file actions
216 lines (175 loc) · 7.11 KB
/
Copy pathWalmart project2.sql
File metadata and controls
216 lines (175 loc) · 7.11 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
--TO CHECK ROW COUNT MATCHES
SELECT *
FROM walmart ;
-- TO CHECK DUPLICATES
SELECT invoice_id, count(*)
FROM walmart
GROUP BY invoice_id
HAVING COUNT (*)>1;
--For null
SELECT count(*)
FROM walmart
WHERE unit_price IS NULL OR quantity IS NULL;
--CHECK DATA TYPE
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'walmart';
SELECT *
FROM walmart ;
---BUSINESS QUESTIONS
-- 1. WHAT IS THE TOTAL REVENUE GENERATED ACROSS ALL TRANSACTIONS?
select sum(revenue) as total_revenue
from
(SELECT (unit_price * quantity) as revenue
FROM walmart) as sales
-- Another way without subquery
select sum(unit_price * quantity)
from walmart
--2. Which city has the highest number of transactions?
select "City", count(invoice_id) as number_of_transaction
from walmart
group by "City"
order by count(invoice_id) desc
-- 3. What is the average rating per category?
select category, avg(rating) as avg_rating
from walmart
group by category
--4. Which branch generated the highest total revenue?
select "Branch", sum(unit_price * quantity) as total_revenue
from walmart
GROUP BY "Branch"
ORDER BY total_revenue desc;
--5. What is the most commonly used payment method, and how does it vary by city?
select "City", payment_method, count(invoice_id)
from walmart
GROUP BY "City" , payment_method
ORDER BY count(invoice_id) desc;
select payment_method, count(invoice_id)
from walmart
GROUP BY payment_method
ORDER BY count(invoice_id) desc;
--6. Which product category has the highest average profit margin?
SELECT category, avg(profit_margin) as avg_profit_margin
FROM walmart
GROUP BY category
order by avg_profit_margin desc
--7. What is the total revenue per month across the full dataset (2019-2023)?
select extract(year from date) as year_number,
extract(month from date) as mon_number,
TO_CHAR(date,'MON-YYYY') as month_year_name,
sum(unit_price * quantity) as total_revenue
from walmart
group by year_number, mon_number, month_year_name
order by year_number, mon_number ;
--7B. What's driving the Nov/Dec revenue spike identified in Q7 — more transactions, larger basket sizes, or higher prices?
select extract(year from date) as year_number,
extract(month from date) as mon_number,
TO_CHAR(date,'MON-YYYY') as month_year_name,
sum(unit_price * quantity) as total_revenue,
count(invoice_id) as total_transaction,
avg(quantity) as avg_quantity,
avg(unit_price) as avg_price
from walmart
group by year_number, mon_number, month_year_name
order by year_number, mon_number ;
-- 8. Which day of the week has the highest average transaction value?
select extract(ISODOW from date) as day_number_mon_to_sun,
to_char(date,'FMDay') as day_name,
count(invoice_id) as total_transaction,
avg(unit_price * quantity) as avg_transaction_value
from walmart
group by day_number_mon_to_sun, day_name
order by avg_transaction_value desc;
-- 9. Is there a noticeable revenue trend year-over-year?
select extract (year from date) as year_number,
sum(unit_price* quantity) as total_revenue
from walmart
group by year_number
order by year_number
-- 10. Rank branches by revenue within each city (window function).
select "City","Branch", total_revenue,
RANK()OVER(PARTITION BY "City" order by total_revenue desc) as branch_rank
from (select "City","Branch",sum(unit_price * quantity) total_revenue
from walmart
group by "City","Branch"
) as Branch_total_revenue
ORDER BY branch_rank
select "City", count(distinct "Branch") as branch_count
from walmart
group by "City"
order by branch_count desc
-- 10B. Rank branches by revenue within each city (Filtering for cities with 2 branch i.e Waxahachie and Weslaco).
select "City","Branch", total_revenue,
RANK()OVER(PARTITION BY "City" order by total_revenue desc) as branch_rank
from (select "City","Branch",sum(unit_price * quantity) total_revenue
from walmart
where "City" = 'Waxahachie' or "City" = 'Weslaco'
group by "City","Branch"
) as Branch_total_revenue
ORDER BY branch_rank
--11. Find the top 3 best-selling categories per branch (window function + partition).
select "City","Branch",category,total_revenue,category_rank
from (select "City","Branch",category,total_revenue,
RANK()OVER(PARTITION BY "Branch" ORDER BY total_revenue DESC) as category_rank
from (SELECT "City","Branch",category,sum(unit_price * quantity) as total_revenue
FROM walmart
group by category,"Branch","City"
) as category_total_revenue
) as ranked_categories
where category_rank <= 3
order by category_rank
--12. Compare each branch's monthly revenue to the overall monthly average.
select year_number,
mon_number,
mon_year_name,
"City",
"Branch",
total_revenue,
avg_monthly_revenue,
total_revenue - avg_monthly_revenue as difference_from_average
from (select "City",
"Branch",
mon_number,
year_number,
mon_year_name,
total_revenue,
avg(total_revenue)over(partition by mon_number, year_number) as avg_monthly_revenue
from (SELECT "City","Branch",
extract(month from date) as mon_number,
extract(YEAR from date) as year_number,
to_char(date, 'MON-YYYY') as mon_year_name,
sum(unit_price*quantity) as total_revenue
from walmart
group by "City","Branch", mon_number,year_number,mon_year_name
) as monthly_revenue
) as average_mon_revenue
--12B. Which branches most consistently outperform or underperform their monthly cohort average across the full dataset (not just in a single month)?
select "Branch",
avg(difference_from_average) as avg_diff
from (select year_number,
mon_number,
mon_year_name,
"City",
"Branch",
total_revenue,
avg_monthly_revenue,
total_revenue - avg_monthly_revenue as difference_from_average
from (select "City",
"Branch",
mon_number,
year_number,
mon_year_name,
total_revenue,
avg(total_revenue)over(partition by mon_number, year_number) as avg_monthly_revenue
from (SELECT "City","Branch",
extract(month from date) as mon_number,
extract(YEAR from date) as year_number,
to_char(date, 'MON-YYYY') as mon_year_name,
sum(unit_price*quantity) as total_revenue
from walmart
group by "City","Branch", mon_number,year_number,mon_year_name
) as monthly_revenue
) as average_mon_revenue
) Branch_monthly_performance
group by "Branch"
order by avg_diff DESC