-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueries_DataAnalysis.sql
More file actions
87 lines (70 loc) · 2.61 KB
/
Queries_DataAnalysis.sql
File metadata and controls
87 lines (70 loc) · 2.61 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
--1. List all the items ordered with their total quantities and total price.
SELECT
i.item_name,
SUM(oi.quantity) AS total_quantity,
SUM(oi.item_price) as total_price
FROM Items i
JOIN OrderItems oi ON i.item_id = oi.item_id
GROUP BY i.item_name;
--2. Calculate the total amount spent on each order and list the orders in descending order, with the highest amounts at the top.
SELECT o.order_id,
SUM(oi.quantity * oi.item_price) AS total_amount
FROM Orders o
JOIN OrderItems oi ON o.order_id = oi.order_id
GROUP BY o.order_id
ORDER BY total_amount DESC;
--3. List the most expensive item in each category.
SELECT item_category,
MAX(item_price) AS max_price
FROM Items
GROUP BY item_category;
--4. Calculate the total cost of ingredients for each item, factoring in the size of the item.
SELECT i.item_id,
i.item_name, item_size,
SUM(ing.ingredient_price * ii.quantity_required) AS total_ingredient_cost
FROM Items i
JOIN ItemIngredients ii ON i.item_id = ii.item_id
JOIN Ingredients ing ON ii.ingredient_id = ing.ingredient_id
GROUP BY i.item_id, i.item_name,item_size;
--5. List the top 5 items with the highest total sales amount.
SELECT i.item_name,
SUM(oi.quantity * oi.item_price) AS total_sales
FROM OrderItems oi
JOIN Items i ON oi.item_id = i.item_id
GROUP BY i.item_name
ORDER BY total_sales DESC
LIMIT 5;
--6. Show orders placed in the last 30 days along with the total quantity of items ordered
SELECT o.order_id,
COUNT(oi.order_item_id) AS total_items_ordered
FROM Orders o
JOIN OrderItems oi ON o.order_id = oi.order_id
WHERE o.placement_date >= NOW() - INTERVAL 30 DAY
GROUP BY o.order_id;
--7. Show items that have never been ordered.
SELECT i.item_id,
i.item_name
FROM Items i
LEFT JOIN OrderItems oi ON i.item_id = oi.item_id
WHERE oi.item_id IS NULL;
--8. List all ingredients that are used in more than 3 items.
SELECT ing.ingredient_id,
ing.ingredient_name,
COUNT(ii.item_id) AS num_items
FROM Ingredients ing
JOIN ItemIngredients ii ON ing.ingredient_id = ii.ingredient_id
GROUP BY ing.ingredient_id, ing.ingredient_name
HAVING COUNT(ii.item_id) > 3;
--9. Calculate the average price of items in each category, rounded to two decimal points.
SELECT item_category,
ROUND(AVG(item_price), 2) AS average_price
FROM Items
GROUP BY item_category;
--10. List items with inventory below a certain threshold (100)
SELECT i.item_id,
i.item_name,
SUM(inv.quantity) AS total_inventory_quantity
FROM Items i
LEFT JOIN Inventory inv ON i.item_id = inv.item_id
GROUP BY i.item_id, i.item_name
HAVING total_inventory_quantity < 100;