-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL project_Pizza Sales.sql
145 lines (132 loc) · 3.63 KB
/
SQL project_Pizza Sales.sql
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
-- 1} Retrieve the total number of orders placed.
SELECT
COUNT(order_id) AS total_orders
FROM
orders;
-- 2} Calculate the total revenue generated from pizza sales.
SELECT
SUM(od.quantity * p.price) AS total_revenue
FROM
order_details od
JOIN
pizzas p ON od.pizza_id = p.pizza_id;
-- 3} Identify the highest-priced pizza.
SELECT
pt.name, p.price
FROM
pizza_types pt
JOIN
pizzas p ON p.pizza_type_id = pt.pizza_type_id
ORDER BY price desc
LIMIT 1;
-- 4} Identify the most common pizza size ordered.
SELECT
size, COUNT(order_id) AS ordercount
FROM
pizzas p
JOIN
order_details od ON p.pizza_id = od.pizza_id
GROUP BY size
ORDER BY ordercount DESC
LIMIT 1;
-- 5} List the top 5 most ordered pizza types along with their quantities.
SELECT
pt.name, sum(quantity) as quantity
FROM
pizzas p
JOIN
pizza_types pt ON p.pizza_type_id = pt.pizza_type_id
JOIN
order_details od ON p.pizza_id = od.pizza_id
GROUP BY pt.name
ORDER BY quantity DESC
LIMIT 5;
-- 6} Find the total quantity of each pizza category ordered.
SELECT
category, SUM(quantity) as quantity
FROM
pizza_types pt
JOIN
pizzas p ON p.pizza_type_id = pt.pizza_type_id
JOIN
order_details od ON od.pizza_id = p.pizza_id
GROUP BY category
ORDER BY quantity desc;
-- 7} Determine the distribution of orders by hour of the day.
SELECT
HOUR(order_time) AS hour, COUNT(order_id) AS Order_Count
FROM
orders
GROUP BY HOUR(order_time);
-- 8} Find the category-wise distribution of pizzas.
SELECT
category, COUNT(name)
FROM
pizza_types
GROUP BY category;
-- 9} Group the orders by date and calculate the average number of pizzas ordered per day.
SELECT
ROUND(AVG(quantity), 0) AS avg_quantity
FROM
(SELECT
order_date, SUM(quantity) AS quantity
FROM
orders o
JOIN order_details od ON o.order_id = od.order_id
GROUP BY order_date) AS order_quantity;
-- 10} Determine the top 3 most ordered pizza types based on revenue.
SELECT
name, ROUND(SUM(quantity * price), 0) AS revenue
FROM
pizza_types pt
JOIN
pizzas p ON pt.pizza_type_id = p.pizza_type_id
JOIN
order_details od ON od.pizza_id = p.pizza_id
GROUP BY name
ORDER BY revenue DESC
LIMIT 3;
-- 11} Calculate the percentage contribution of each pizza type to total revenue.
SELECT
category,
ROUND(SUM(quantity * price) / (SELECT
SUM(quantity * price)
FROM
pizzas p
JOIN
order_details od ON od.pizza_id = p.pizza_id) * 100,
2) AS revenue
FROM
pizza_types pt
JOIN
pizzas p ON p.pizza_type_id = pt.pizza_type_id
JOIN
order_details od ON od.pizza_id = p.pizza_id
GROUP BY category
ORDER BY revenue DESC;
-- 12} Analyze the cumulative revenue generated over time.
select
order_date,sum(revenue) over (order by order_date) as cum_revenue
from
(select
order_date,
sum(quantity*price) as revenue
FROM order_details od
join pizzas p on od.pizza_id = p.pizza_id
JOIN orders o ON o.order_id=od.order_id
group by order_date) as sales;
-- 13} Determine the top 3 most ordered pizza types based on revenue for each pizza category.
select name, revenue from (SELECT
category,name,revenue,
rank () over (partition by category order by revenue desc) as rn
from
(SELECT
category,name,ROUND(SUM(quantity * price), 0) AS revenue
FROM
pizza_types pt
JOIN
pizzas p ON pt.pizza_type_id = p.pizza_type_id
JOIN
order_details od ON od.pizza_id = p.pizza_id
GROUP BY category,name) as a) as b
where rn<=3;