-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetail_sql_queries.sql
More file actions
156 lines (136 loc) · 4.05 KB
/
Retail_sql_queries.sql
File metadata and controls
156 lines (136 loc) · 4.05 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
select * from walmart limit 10;
-- Q1. Find different payment methods, number of transactions, and quantity sold by payment method
SELECT
payment_method,
COUNT(*) AS no_payments,
SUM(quantity) AS no_qty_sold
FROM walmart
GROUP BY payment_method;
-- Q2: Identify the highest-rated category in each branch
-- Display the branch, category, and avg rating
with cte as(
SELECT
branch,
category,
AVG(rating) AS avg_rating,
RANK() OVER(PARTITION BY branch ORDER BY AVG(rating) DESC) AS rank_rating
FROM walmart
GROUP BY branch, category
)
select branch, category, avg_rating
from cte
where rank_rating = 1;
-- Q3: Identify the busiest day for each branch based on the number of transactions
with cte as(
SELECT
branch,
DAYNAME(STR_TO_DATE(date, '%d/%m/%Y')) AS day_name,
COUNT(*) AS no_transactions,
RANK() OVER(PARTITION BY branch ORDER BY COUNT(*) DESC) AS rank_transactions
FROM walmart
GROUP BY branch, day_name
)
select branch, day_name, no_transactions
from cte
WHERE rank_transactions = 1;
-- Q4: Calculate the total quantity of items sold per payment method
SELECT
payment_method,
SUM(quantity) AS total_qty_sold
FROM walmart
GROUP BY payment_method;
-- Q5: Determine the average, minimum, and maximum rating of categories for each city
SELECT
city,
category,
MIN(rating) AS min_rating,
MAX(rating) AS max_rating,
AVG(rating) AS avg_rating
FROM walmart
GROUP BY city, category;
-- Q6: Calculate the total profit for each category
SELECT
category,
SUM(unit_price * quantity * profit_margin) AS total_profit
FROM walmart
GROUP BY category
ORDER BY total_profit DESC;
-- Q7: Determine the most common payment method for each branch
WITH cte AS (
SELECT
branch,
payment_method,
COUNT(*) AS total_trans,
RANK() OVER(PARTITION BY branch ORDER BY COUNT(*) DESC) AS rank_payment
FROM walmart
GROUP BY branch, payment_method
)
SELECT branch, payment_method AS preferred_payment_method
FROM cte
WHERE rank_payment = 1;
-- Q8: Categorize sales into Morning, Afternoon, and Evening shifts
SELECT
branch,
CASE
WHEN HOUR(TIME(time)) < 12 THEN 'Morning'
WHEN HOUR(TIME(time)) BETWEEN 12 AND 17 THEN 'Afternoon'
ELSE 'Evening'
END AS shift,
COUNT(*) AS num_invoices
FROM walmart
GROUP BY branch, shift
ORDER BY branch, num_invoices DESC;
-- Q9: Identify the 5 branches with the highest revenue decrease ratio from last year to current year (e.g., 2022 to 2023)
WITH revenue_2022 AS (
SELECT
branch,
SUM(total) AS revenue
FROM walmart
WHERE YEAR(STR_TO_DATE(date, '%d/%m/%Y')) = 2022
GROUP BY branch
),
revenue_2023 AS (
SELECT
branch,
SUM(total) AS revenue
FROM walmart
WHERE YEAR(STR_TO_DATE(date, '%d/%m/%Y')) = 2023
GROUP BY branch
)
SELECT
r2022.branch,
r2022.revenue AS last_year_revenue,
r2023.revenue AS current_year_revenue,
ROUND(((r2022.revenue - r2023.revenue) / r2022.revenue) * 100, 2) AS revenue_decrease_ratio
FROM revenue_2022 AS r2022
JOIN revenue_2023 AS r2023 ON r2022.branch = r2023.branch
WHERE r2022.revenue > r2023.revenue
ORDER BY revenue_decrease_ratio DESC
LIMIT 5;
-- Q10. Identify the 5 branches with the highest revenue increase ratio from last year to current year (e.g., 2022 to 2023)
WITH revenue_2022 AS (
SELECT
branch,
SUM(total) AS revenue
FROM walmart
WHERE YEAR(STR_TO_DATE(date, '%d/%m/%Y')) = 2022
GROUP BY branch
),
revenue_2023 AS (
SELECT
branch,
SUM(total) AS revenue
FROM walmart
WHERE YEAR(STR_TO_DATE(date, '%d/%m/%Y')) = 2023
GROUP BY branch
)
SELECT
r2022.branch,
r2022.revenue AS last_year_revenue,
r2023.revenue AS current_year_revenue,
ROUND(((r2023.revenue - r2022.revenue) / r2022.revenue) * 100, 2) AS revenue_increase_ratio
FROM revenue_2022 AS r2022
JOIN revenue_2023 AS r2023 ON r2022.branch = r2023.branch
WHERE r2023.revenue > r2022.revenue
ORDER BY revenue_increase_ratio DESC
LIMIT 5;