-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnswers.sql
More file actions
153 lines (127 loc) · 4.22 KB
/
Copy pathAnswers.sql
File metadata and controls
153 lines (127 loc) · 4.22 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
-- 1. Retrieve the total number of orders placed.
select * from orders;
SELECT
COUNT(order_id) AS total_orders
FROM
orders;
-- 2. Calculate the total revenue generated from pizza sales.
select * from pizzas;
SELECT
ROUND(SUM(order_details.quantity * pizzas.price),2) AS total_rev_pizza_sales
FROM
order_details
JOIN
pizzas ON pizzas.pizza_id = order_details.pizza_id;
-- 3. Identify the highest-priced pizza.
SELECT
pizza_types.name, pizzas.price
FROM
pizza_types
JOIN
pizzas ON pizza_types.pizza_type_id = pizzas.pizza_type_id
ORDER BY pizzas.price DESC
LIMIT 01;
-- 4. Identify the most common pizza size ordered.
SELECT
pizzas.size, COUNT(order_details.order_id) AS order_count
FROM
pizzas
JOIN
order_details ON pizzas.pizza_id = order_details.pizza_id
GROUP BY pizzas.size
ORDER BY order_count DESC;
-- 5. List the top 5 most ordered pizza types along with their quantities.
SELECT
pizza_types.name, SUM(order_details.quantity) total_quantity
FROM
pizza_types
JOIN
pizzas ON pizza_types.pizza_type_id = pizzas.pizza_type_id
JOIN
order_details ON order_details.pizza_id = pizzas.pizza_id
GROUP BY pizza_types.name
ORDER BY total_quantity DESC
LIMIT 5;
-- 6. Join the necessary tables to find the total quantity of each pizza category ordered.
SELECT
pizza_types.category,
SUM(order_details.quantity) AS quantity
FROM
pizza_types
JOIN
pizzas ON pizza_types.pizza_type_id = pizzas.pizza_type_id
JOIN
order_details ON order_details.pizza_id = pizzas.pizza_id
GROUP BY pizza_types.category
ORDER BY quantity DESC;
-- 7. Determine the distribution of orders by hour of the day.
SELECT
HOUR(order_time) AS orders_hr,
COUNT(order_id) AS orders_count
FROM
orders
GROUP BY HOUR(order_time);
-- 8. Join relevant tables to 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_pizzas_orders_per_day from
(select orders.order_date,sum(order_details.quantity) as quantity
from orders
join order_details
on orders.order_id=order_details.order_id
group by orders.order_date) as order_quantity;
-- 10. Determine the top 3 most ordered pizza types based on revenue.
SELECT pizza_types.name,
ROUND(SUM(order_details.quantity * pizzas.price),2) AS total_rev_pizza_sales
FROM
pizza_types
JOIN
pizzas ON pizzas.pizza_type_id = pizza_types.pizza_type_id
join order_details
on order_details.pizza_id=pizzas.pizza_id
group by pizza_types.name
order by total_rev_pizza_sales desc
limit 3;
-- 11. Calculate the percentage contribution of each pizza type to total revenue.
SELECT pizza_types.category,
round((sum(order_details.quantity * pizzas.price) / (SELECT
ROUND(SUM(order_details.quantity * pizzas.price),2) AS total_rev_pizza_sales
FROM
order_details
JOIN
pizzas ON pizzas.pizza_id = order_details.pizza_id
))* 100,2 )AS total_rev_pizza_sales
FROM
pizza_types
JOIN
pizzas ON pizzas.pizza_type_id = pizza_types.pizza_type_id
join order_details
on order_details.pizza_id=pizzas.pizza_id
group by pizza_types.category order by total_rev_pizza_sales desc ;
-- 12. Analyze the cumulative revenue generated over time.
select order_date,sum(revenue) over( order by order_date) as cum_revenue
from
(select orders.order_date,sum(order_details.quantity*pizzas.price) as revenue
from order_details
join pizzas
on order_details.pizza_id= pizzas.pizza_id
join orders
on orders.order_id =order_details.order_id
group by orders.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 pizza_types.category,pizza_types.name,
sum(order_details.quantity*pizzas.price) as revenue
from pizza_types
join pizzas
on pizza_types.pizza_type_id =pizzas.pizza_type_id
join order_details
on order_details.pizza_id =pizzas.pizza_id
group by pizza_types.category,pizza_types.name) as a) as b
where rn <=3;