Repository navigation
Expand file tree
/
Copy pathretail_sales_analysis_solved.sql
More file actions
264 lines (211 loc) · 5.56 KB
/
Copy pathretail_sales_analysis_solved.sql
File metadata and controls
264 lines (211 loc) · 5.56 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
-- Retail Sales Analysis
-- Checking all the rows
select count(*)
from retail_sales;
-- Checking null values
-- We can write all together with OR function
select * from retail_sales
where
transactions_id is null
or
sale_date is null
or
sale_time is null
or
customer_id is null
or
gender is null
or
category is null
or
quantiy is null
or
price_per_unit is null
or
cogs is null
or
total_sale is null;
select * from retail_sales
where age is null;
-- Dropping null values
delete from retail_sales
where
transactions_id is null
or
sale_date is null
or
sale_time is null
or
customer_id is null
or
gender is null
or
category is null
or
quantiy is null
or
price_per_unit is null
or
cogs is null
or
total_sale is null;
-- Filling the age column with average
update retail_sales
set age=(select avg(age) from retail_sales)
where age is null;
-- Business Problems and solutions
-- Find the total Sales per day
select sale_date, sum(total_sale) as total_sales
from retail_sales
group by 1
order by 1;
-- Find the sales trend over the time
select date_trunc('month', sale_date) as month, sum(total_sale) as total_sales
from retail_sales
group by 1
order by 1;
-- Find the top selling category
select category, sum(total_sale)as total_sales
from retail_sales
group by 1
order by 2 desc;
-- Find the top 5 most frequent customers who are going for purchase
select customer_id, count(transactions_id)as total_transaction
from retail_sales
group by 1
order by 2 desc
limit 5;
-- Find the average spending each customer does for shopping
select customer_id, round(avg(total_sale)::numeric, 2)as avg_spending
from retail_sales
group by 1
order by 2 desc
-- Find which age group does more shopping order by descending
select
case
when age between 18 and 28 then '18-28'
when age between 29 and 40 then '29-40'
when age between 41 and 50 then '41-50'
when age between 51 and 60 then '51-60'
else '60+'
end as age_group,
sum(total_sale)as total_sales
from retail_sales
group by 1
order by 2 desc;
-- Demographic wise number of sales distribution
select gender, sum(total_sale)as total_sales
from retail_sales
group by 1
order by 2 desc;
-- Find the total number of transaction made by each gender in each category
select category,
gender,
count(*)as total_transaction
from retail_sales
group by 1, 2;
-- Find the number of transaction each category is making aslo break the sales category wise
select category,
count(transactions_id)as no_of_transaction,
sum(total_sale)as totalSales
from retail_sales
group by 1
order by 3 desc;
-- Profitability analysis of categories through overall sales
select category,
round((sum(total_sale)-sum(cogs))::numeric, 2)as profit
from retail_sales
group by 1
order by 2 desc;
-- Find the number of quantity each product/category among all the sales data
select category,
sum(quantiy)as total_quantity
from retail_sales
group by 1
order by 2 desc;
-- Find the peak sale hour
select extract(hour from sale_time)as hour,
sum(total_sale)as total_sales
from retail_sales
group by 1
order by 2 desc;
-- When is the time the maximum sales are happening according to the different time range
select
category,
case
when sale_time between '04:00:00' and '11:59:59' then 'Morning'
when sale_time between '12:00:00' and '15:59:59' then 'Afternoon'
when sale_time between '16:00:00' and '19:59:59' then 'Evening'
else 'Night'
end as time_range,
sum(total_sale)as total_sales
from retail_sales
group by 1, 2
order by 3 desc;
-- Find the maximum orders placed in each shift in retail sales
with cte_tb2 as
(select
*,
case
when sale_time between '04:00:00' and '11:59:59' then 'Morning'
when sale_time between '12:00:00' and '15:59:59' then 'Afternoon'
when sale_time between '16:00:00' and '19:59:59' then 'Evening'
else 'Night'
end as time_range
from retail_sales)
select time_range, count(*)as total_orders
from cte_tb2
group by time_range
order by total_orders desc;
-- Find the sales by day of week for the records from the table
select
to_char(sale_date, 'Day')as day_of_week,
sum(total_sale)as total_sales
from retail_sales
group by 1
order by 2 desc
-- Which day has maximum sales weekend or weekday
select
case
when to_char(sale_date, 'D') in ('1', '7') then 'Weekend' -- '1' for Sunday, '7' for Saturday
else 'Weekday'
end as day_category,
sum(total_sale) as total_sales
from retail_sales
group by day_category
order by total_sales desc;
-- Find the average quantity sold per product
select category,
avg(quantiy)as avg_quantity
from retail_sales
group by 1
order by 2 desc;
-- Calculate the average sale for each month also find the best selling month in each year
with cte_tb as
(select extract(year from sale_date)as year,
extract(month from sale_date)as month,
avg(total_sale)as avg_sales,
rank() over(partition by extract(year from sale_date) order by avg(total_sale) desc)as rank
from retail_sales
group by 1, 2)
select year, month, avg_sales from cte_tb
where rank=1;
-- Find the profit margin of all the product categories out there
select
category,
sum(total_sale)as total_sales,
sum(cogs)as total_cogs,
round(cast((sum(total_sale)-sum(cogs))/sum(total_sale)*100 as numeric), 2)as profit_margin
from retail_sales
group by category
order by profit_margin desc;
-- Find the top 5 customers with total sale
select customer_id, sum(total_sale)as total_sales
from retail_sales
group by 1
order by 2 desc
limit 5;
-- Find the unique customers who have purchased from each category
select category, count(distinct customer_id)as unique customers
from retail_sales
group by 1;