-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpizza.sql
More file actions
178 lines (161 loc) · 4.49 KB
/
pizza.sql
File metadata and controls
178 lines (161 loc) · 4.49 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
create database pizza_db;
show databases;
use pizza_db;
CREATE TABLE orders (
order_id INT NOT NULL,
order_date DATE NOT NULL,
order_time TIME NOT NULL,
PRIMARY KEY (order_id)
);
CREATE TABLE order_details (
order_details_id INT NOT NULL,
order_id INT NOT NULL,
pizza_id TEXT NOT NULL,
quantity INT NOT NULL,
PRIMARY KEY (order_details_id)
);
select * from order_details;
/*Basic:*/
/*Retrieve the total number of orders placed.*/
SELECT
COUNT(order_id) AS Total_Orders
FROM
orders;
/*Calculate the total revenue generated from pizza sales.*/
SELECT
ROUND(SUM(od.quantity * p.price), 2) AS Total_Revenue
FROM
order_details od
LEFT JOIN
pizzas p ON od.pizza_id = p.pizza_id;
/*Identify the highest-priced pizza.*/
SELECT
*
FROM
pizzas
WHERE
price = (SELECT
MAX(price)
FROM
pizzas);
/*Identify the most common pizza size ordered.*/
SELECT
size, COUNT(size) AS Max_Ordered_Pizza_Size
FROM
order_details od
LEFT JOIN
pizzas p ON od.pizza_id = p.pizza_id
GROUP BY p.size
ORDER BY COUNT(size) DESC
LIMIT 1;
/*List the top 5 most ordered pizza types along with their quantities.*/
SELECT
pt.name, COUNT(od.pizza_id) AS frequent_Pizza
FROM
order_details od
LEFT JOIN
pizzas p ON od.pizza_id = p.pizza_id
LEFT JOIN
pizza_types pt ON p.pizza_type_id = pt.pizza_type_id
GROUP BY pt.name
ORDER BY COUNT(od.pizza_id) DESC
LIMIT 5;
/*Intermediate:*/
/*Join the necessary tables to find the total quantity of each pizza category ordered.*/
SELECT
pt.category, SUM(od.quantity) AS Total_Qunatity
FROM
order_details od
LEFT JOIN
pizzas p ON od.pizza_id = p.pizza_id
LEFT JOIN
pizza_types pt ON pt.pizza_type_id = p.pizza_type_id
GROUP BY pt.category
ORDER BY SUM(od.quantity) DESC;
/*Determine the distribution of orders by hour of the day.*/
SELECT
EXTRACT(HOUR FROM order_time) AS hour,
COUNT(order_id) AS No_Of_Order
FROM
orders
GROUP BY hour
ORDER BY No_Of_Order ASC;
/*Join relevant tables to find the category-wise distribution of pizzas.*/
SELECT
pt.category, COUNT(pt.name) as No_Of_Pizza_Type
FROM
pizza_types pt
GROUP BY pt.category;
/*Group the orders by date and calculate the average number of pizzas ordered per day.*/
SELECT
AVG(Total_Quantity) AS Avrage_Quantity_Per_Day
FROM
(SELECT
o.order_date, SUM(od.quantity) AS Total_Quantity
FROM
orders o
LEFT JOIN order_details od ON o.order_id = od.order_id
GROUP BY o.order_date) AS Order_Quantity;
/*Determine the top 3 most ordered pizza types based on revenue.*/
SELECT
pt.name AS Pizza_Name,
ROUND(SUM(od.quantity * p.price), 2) AS Revenue
FROM
order_details od
JOIN
pizzas p ON od.pizza_id = p.pizza_id
JOIN
pizza_types pt ON pt.pizza_type_id = p.pizza_type_id
GROUP BY pt.name
ORDER BY Revenue DESC
LIMIT 3;
/*Advanced:
/*Calculate the percentage contribution of each pizza type to total revenue.*/
SELECT
pt.category AS Pizza_Category,
ROUND(ROUND(SUM(od.quantity * p.price), 2) / (SELECT
ROUND(SUM(od.quantity * p.price), 2)
FROM
order_details od
JOIN
pizzas p ON od.pizza_id = p.pizza_id
JOIN
pizza_types pt ON pt.pizza_type_id = p.pizza_type_id) * 100,
2) AS Revenue
FROM
order_details od
JOIN
pizzas p ON od.pizza_id = p.pizza_id
JOIN
pizza_types pt ON pt.pizza_type_id = p.pizza_type_id
GROUP BY pt.category
ORDER BY Revenue;
/*Analyze the cumulative revenue generated over time.*/
SELECT order_date,SUM(Day_Revenue) OVER(ORDER BY order_date) AS Cum_Revenue
FROM
(SELECT
o.order_date, ROUND(SUM(od.quantity * p.price),2) AS Day_Revenue
FROM
orders o
JOIN
order_details od ON o.order_id = od.order_id
JOIN
pizzas p ON p.pizza_id = od.pizza_id
GROUP BY o.order_date)
AS Sales ;
/*Determine the top 3 most ordered pizza types based on revenue for each pizza category.*/
SELECT Category, Name, Revenue FROM
(SELECT
Category, Name, Revenue,
rank() over (partition by Category order by Revenue desc ) as RN FROM
(SELECT
pt.category AS Category,
pt.name AS Name,
ROUND(SUM(od.quantity * p.price), 2) AS Revenue
FROM
order_details od
JOIN
pizzas p ON od.pizza_id = p.pizza_id
JOIN
pizza_types pt ON p.pizza_type_id = pt.pizza_type_id
GROUP BY pt.category , pt.name) AS A) AS B WHERE RN < 4 ;