-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretail_orders.sql
More file actions
136 lines (121 loc) · 4.53 KB
/
retail_orders.sql
File metadata and controls
136 lines (121 loc) · 4.53 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
CREATE DATABASE Retail_Orders;
use Retail_Orders;
-- create table retail_orders(
-- order_id int primary key,
-- order_date date,
-- ship_mode varchar (20),
-- segment varchar (20),
-- country varchar (20),
-- city varchar (20),
-- state varchar (20),
-- postal_code varchar (20),
-- region varchar (20),
-- category varchar (20),
-- sub_category varchar (20),
-- product_id varchar (50),
-- quantity int,
-- discount decimal (7, 2),
-- sale_price decimal (7, 2),
-- profit decimal (7, 2)
-- );
SELECT * FROM Retail_Orders;
# Task:
-- 1. Identify the top 10 products that generate the highest revenue.
SELECT product_id, sum(sale_price) as revenue from retail_orders
group by product_id
order by revenue desc limit 10;
-- 2. Determine the top 5 best-selling products in each region.
SELECT region, product_id, SUM(sale_price) as revenue from retail_orders
group by region, product_id
order by region,revenue desc;
with cte as (
SELECT region, product_id, SUM(sale_price) as revenue from retail_orders
group by region, product_id)
select * from (
select *, ROW_NUMBER() over(partition by region order by revenue desc) as rank_
from cte ) A
where rank_ <=5;
-- 3. Compare the month-over-month sales growth for 2022 and 2023.
select year(order_date) as order_year, month(order_date) as order_month, sum(sale_price) as sales from retail_orders
group by year(order_date), month(order_date)
order by order_year, order_month;
-- VS
with cte as (
select year(order_date) as order_year, month(order_date) as order_month, sum(sale_price) as sales from retail_orders
group by year(order_date), month(order_date)
-- order by order_year, order_month
)
select order_month
, sum(case when order_year = 2022 then sales else 0 end) as sales_2022
, sum(case when order_year = 2023 then sales else 0 end) as sales_2023
from cte
group by order_month
order by order_month;
-- 4. For each category, find out which month had the highest sales.
with cte as (
select category, format(order_date,'yyyyMM') as order_year_month
, sum(sale_price) as sales
from retail_orders
group by category, format(order_date, 'yyyyMM')
-- order by category, format(order_date, 'yyyyMM')
)
select * from (
select *, row_number() over(partition by category order by sales desc) as rank_
from cte
) a where rank_ = 1;
-- 5. Discover which sub-category experienced the highest profit growth in 2023 compared to 2022
with cte as (
select sub_category, year(order_date) as order_year, sum(sale_price) as sales from retail_orders
group by sub_category, year(order_date)
-- order by order_year, order_month
)
, cte2 as(
select sub_category,
sum(case when order_year = 2022 then sales else 0 end) as sales_2022,
sum(case when order_year = 2023 then sales else 0 end) as sales_2023
from cte
group by sub_category
)
SELECT *, (sales_2023 - sales_2022)* 100 / sales_2022 as highest_growth_sub_Category from cte2
order by highest_growth_sub_Category desc;
-- 6. Discount Effectiveness: Analyze the impact of discounts on sales and profit:
SELECT discount, SUM(sale_price) as total_sales, SUM(profit) as total_profit
from retail_orders
Group by discount
order by discount;
# This query shows the relationship between discount levels and their effect on sales and profits, helping optimize pricing strategies.
-- 7. Category-Wise Performance
-- This query compares sales and profit across different product categories.
SELECT category,
SUM(sale_price) AS total_sales,
SUM(profit) AS total_profit
FROM retail_orders
GROUP BY category
ORDER BY total_sales DESC;
-- 8. Profitability of each region.
SELECT region, SUM(profit) AS total_profit
FROM retail_orders
GROUP BY region
ORDER BY total_profit DESC;
#This query helps assess which regions are most profitable and where resources should be focused.
-- 9. Top Selling Cities:
SELECT city, SUM(sale_price) AS total_sales
FROM retail_orders
GROUP BY city
ORDER BY total_sales DESC
LIMIT 10;
# This helps identify cities with the highest demand, enabling targeted marketing and resource allocation.
-- 10. Yearly Revenue Growth by Region.
SELECT region,
YEAR(order_date) AS year,
SUM(sale_price) AS total_sales
FROM retail_orders
GROUP BY region, YEAR(order_date)
ORDER BY region, year;
-- 11. Shipping Mode Analysis:
SELECT ship_mode,
COUNT(order_id) AS total_orders,
SUM(profit) AS total_profit
FROM retail_orders
GROUP BY ship_mode
ORDER BY total_profit DESC;