-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis_queries.sql
More file actions
274 lines (190 loc) · 7.45 KB
/
Copy pathanalysis_queries.sql
File metadata and controls
274 lines (190 loc) · 7.45 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
265
266
267
268
269
270
271
272
273
274
1. **Write a query to calculate the total revenue generated from pizza sales?**:
SELECT SUM(total_price) AS total_revenue
FROM pizza_sales;;
2. **Write a SQL query to calculate the average order value (AOV) by dividing the total revenue by the number of unique orders. Format the result as a decimal with two places?**:
SELECT
CAST(SUM(total_price) / COUNT(DISTINCT order_id) AS DECIMAL(10, 2)) AS avg_order_value
FROM pizza_sales;
3. **Write a SQL query to calculate the total number of pizzas sold?**:
SELECT
SUM(quantity) AS total_pizzas_sold
FROM pizza_sales;
4. **Write a SQL query to calculate the total number of orders?**:
SELECT
COUNT(DISTINCT order_id) AS total_orders
FROM dbo.pizza_sales;
5. **Write a SQL query to calculate the average number of pizzas sold per order?**:
SELECT
CAST(CAST(SUM(quantity) AS DECIMAL(10, 2)) /
CAST(COUNT(DISTINCT order_id) AS DECIMAL(10, 2)) AS DECIMAL(10, 2)) AS avg_pizza_per_order
FROM pizza_sales;
6. **Write a SQL query to find the top 5 selling pizzas?**:
SELECT
pizza_name, SUM(total_price) AS total_sales
FROM pizza_sales
GROUP BY pizza_name
ORDER BY total_sales DESC
LIMIT 5;
7. **Write a SQL query to find the top 5 Worst selling pizzas?**:
SELECT
pizza_name, SUM(total_price) AS total_sales
FROM pizza_sales
GROUP BY pizza_name
ORDER BY total_sales
LIMIT 5;
8. **Write a SQL query to find the total number of orders for each day of the week, sorted by the total orders in descending order? **:
SELECT
DAYNAME(order_date) AS order_day,
COUNT(DISTINCT order_id) AS total_orders
FROM pizza_sales
GROUP BY DAYNAME(order_date)
ORDER BY total_orders DESC;
9. **Write an SQL query to retrieve the total number of orders placed for each hour of the day?**:
SELECT
HOUR(order_time) AS order_hour,
COUNT(DISTINCT order_id) AS total_orders
FROM pizza_sales
GROUP BY HOUR(order_time)
ORDER BY order_hour;
10. **Write a query to identify the best-selling pizza by total quantity?**:
SELECT
pizza_name,
SUM(quantity) AS total_sold
FROM pizza_sales
GROUP BY pizza_name
ORDER BY total_sold DESC;
11. **Write a query to find the total number of pizzas sold for each pizza category?**:
SELECT
pizza_category,
SUM(quantity) AS total_pizzas_sold
FROM pizza_sales
GROUP BY pizza_category
ORDER BY total_pizzas_sold DESC;
12. **Write a query to manipulation of pizza size?**:
SELECT
CASE
WHEN pizza_size = 'M' THEN 'Medium'
WHEN pizza_size = 'S' THEN 'Regular'
WHEN pizza_size = 'L' THEN 'Large'
WHEN pizza_size = 'XL' THEN 'X-Large'
WHEN pizza_size = 'XXL' THEN 'XX-Large'
END AS pizza_size, SUM(total_price) AS total_quantity
FROM pizza_sales
GROUP BY pizza_size;
13. **Write an SQL query to find the number of returning customers by counting distinct order_id values for customers who have placed more than one order?**:
SELECT
COUNT(DISTINCT order_id) AS returning_customer
FROM pizza_sales
GROUP BY order_id
HAVING COUNT(1) > 1;
14. **Write an SQL query to find the daily sale trends?**:
SELECT
order_date,
SUM(total_price) AS total_sales
FROM dbo.pizza_sales
GROUP BY order_date;
15. **Write an SQL query to calculate the total sales for each month?**:
SELECT
MONTH(order_date) AS sales_month,
SUM(total_price) AS total_sales
FROM pizza_sales
GROUP BY MONTH(order_date)
ORDER BY sales_month;
16. **Write an SQL query to calculate the total sales for each year?**:
SELECT
YEAR(order_date) AS sales_year,
SUM(total_price) AS total_sales
FROM pizza_sales
GROUP BY YEAR(order_date) AS sales_year
ORDER BY sales_year;
17. **Write an SQL query to calculate the total sales month over month?**:
WITH monthly_sales AS (
SELECT
YEAR(order_date) AS sales_year,
MONTH(order_date) AS sales_month,
SUM(total_price) AS total_sales
FROM pizza_sales
GROUP BY YEAR(order_date), MONTH(order_date)
)
SELECT sales_year,
sales_month,
total_sales,
LAG(total_sales, 1) OVER (ORDER BY sales_year, sales_month) AS previous_months_sales,
(total_sales - LAG(total_sales, 1) OVER (ORDER BY sales_year, sales_month)) /
LAG(total_sales, 1) OVER (ORDER BY sales_year, sales_month) * 100 AS MoM_growth
FROM monthly_sales;
18. **Write an SQL query to calculate the total sales year over year?**:
WITH yearly_sales AS (
SELECT
YEAR(order_date) AS sales_year,
SUM(total_price) AS total_sales
FROM pizza_sales
GROUP BY YEAR(order_date)
)
SELECT sales_year,
total_sales,
LAG(total_sales, 1) OVER (ORDER BY sales_year) AS previous_year_sales,
(total_sales - LAG(total_sales, 1) OVER (ORDER BY sales_year)) /
LAG(total_sales, 1) OVER (ORDER BY sales_year) * 100 AS YoY_growth
FROM yearly_sales;
19. **Write an SQL query to calculate the total sales for the current year up to the present date?**:
SELECT
YEAR(order_date) AS sales_year,
SUM(total_price) AS total_sales
FROM pizza_sales
WHERE order_date BETWEEN '2024-01-01' AND CURDATE()
GROUP BY YEAR(order_date)
ORDER BY sales_year;
20. **Write an SQL query to calculate the total sales for each quarter of each year?**:
SELECT
YEAR(order_date) AS sales_year,
QUARTER(order_date) AS quarter_sales,
SUM(total_price) AS total_sales
FROM pizza_sales
GROUP BY YEAR(order_date), QUARTER(order_date)
ORDER BY sales_year, quarter_sales;
21. **Write an SQL query to calculate the average sales for each month of each year?**:
SELECT
YEAR(order_date) AS sales_year,
MONTH(order_date) AS sales_month,
AVG(total_price) AS total_sales
FROM pizza_sales
GROUP BY YEAR(order_date), MONTH(order_date)
ORDER BY sales_year, sales_month;
22. **Write an SQL query to calculate the total sales for each day of the week?**:
SELECT
DAYNAME(order_date) AS Day_of_Week,
SUM(total_price) AS Sales
FROM pizza_sales
GROUP BY DAYNAME(order_date)
ORDER BY CASE DAYNAME(order_date)
WHEN 'Sunday' THEN 1
WHEN 'Monday' THEN 2
WHEN 'Tuesday' THEN 3
WHEN 'Wednesday' THEN 4
WHEN 'Thursday' THEN 5
WHEN 'Friday' THEN 6
WHEN 'Saturday' THEN 7
END;
23. **Write an SQL query to find the peak order hours by counting the number of orders placed for each hour of the day?**:
SELECT
HOUR(order_time) AS Order_Hour,
COUNT(order_id) AS Order_Count
FROM pizza_sales
GROUP BY HOUR(order_time)
ORDER BY Order_Count DESC;
24. **Write an SQL query to find the order acquistion rate?**:
SELECT
YEAR(order_date) AS Order_year,
COUNT(DISTINCT order_id) AS new_order
FROM pizza_sales
WHERE order_date BETWEEN '2015-01-01' AND CURDATE()
GROUP BY YEAR(order_date);
25. **Write an SQL query to calculate the sales for each day, the sales from the previous day, and the day-over-day changes in sales?**:
SELECT
order_date,
total_price,
LAG(total_price, 1) OVER (ORDER BY order_date) AS previous_day_sales,
total_price - COALESCE(LAG(total_price, 1) OVER (ORDER BY order_date), 0) AS daily_changes
FROM pizza_sales
ORDER BY order_date;