-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path-- Intermediate_questons.sql
More file actions
65 lines (44 loc) · 1.23 KB
/
-- Intermediate_questons.sql
File metadata and controls
65 lines (44 loc) · 1.23 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
-- INTERMEDIATE QUESTIONS
-- 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
o.order_date, SUM(od.quantity) AS quantity
FROM
orders o
JOIN order_details od ON o.order_id = od.order_id
GROUP BY o.order_date) AS order_quantity;
-- Join the necessary tables to find the total
-- quantity of each pizza category ordered.
SELECT
pt.category, FORMAT(SUM(od.quantity),0) AS total_quantity
FROM
pizza_types pt
JOIN
pizzas p ON pt.pizza_type_id = p.pizza_type_id
JOIN
order_details od ON p.pizza_id = od.pizza_id
GROUP BY pt.category
ORDER BY total_quantity DESC;
-- 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);
-- Determine the top 3 most ordered pizza types based on revenue.
select
pt.name,
CONCAT('$ ', Format(sum(od.quantity * p.price), 1)) 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 pt.name
order by revenue desc
limit 3;