-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay1.sql
More file actions
212 lines (157 loc) · 5.4 KB
/
Day1.sql
File metadata and controls
212 lines (157 loc) · 5.4 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
**Schema (MySQL v8.0)**
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(50),
product_category VARCHAR(50),
product_price DECIMAL(6, 2)
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
product_id INT,
customer_id INT,
quantity INT,
order_date DATE,
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
INSERT INTO products (product_id, product_name, product_category, product_price)
VALUES
(1, 'Product A', 'Category 1', 50.00),
(2, 'Product B', 'Category 1', 25.00),
(3, 'Product C', 'Category 2', 75.00),
(4, 'Product D', 'Category 2', 100.00),
(5, 'Product E', 'Category 3', 150.00);
INSERT INTO orders (order_id, product_id, customer_id, quantity, order_date)
VALUES
(1, 1, 101, 2, '2023-01-01'),
(2, 2, 102, 1, '2023-01-02'),
(3, 3, 103, 3, '2023-01-03'),
(4, 1, 104, 1, '2023-01-04'),
(5, 5, 105, 2, '2023-01-05'),
(6, 4, 106, 4, '2023-01-06'),
(7, 2, 107, 2, '2023-01-07'),
(8, 3, 101, 1, '2023-01-08'),
(9, 1, 108, 2, '2023-01-09'),
(10, 5, 109, 3, '2023-01-10');
---
-- How many orders were placed for each product?
-- What is the total quantity sold for each product category?
-- Which product has generated the most sales revenue?
-- Which customers have purchased 'Product A'?
-- How many unique customers have made purchases?
-- checking tables
select * from products;
select * from orders;
-- 1. How many orders were placed for each product?
select product_id,
count(order_id) as order_count
from orders
group by product_id;
-- 2. What is the total quantity sold for each product category?
SELECT p.product_category, t.total_quantity
FROM products p
JOIN (
SELECT product_id, SUM(quantity) AS total_quantity
FROM orders
GROUP BY product_id
) AS t
ON p.product_id = t.product_id;
-- 3. Which product has generated the most sales revenue?
select p.product_id, p.product_name, product_price*quantity as sales_revenue
from products p
join orders o
on p.product_id = o.product_id
order by sales_revenue desc
limit 5;
-- 4. Which customers have purchased 'Product A'?
SELECT customer_id
FROM orders
WHERE product_id = 4;
-- 5. How many unique customers have made purchases?
select count(distinct customer_id)
from orders;
**Query #1**
select * from products;
| product_id | product_name | product_category | product_price |
| ---------- | ------------ | ---------------- | ------------- |
| 1 | Product A | Category 1 | 50.00 |
| 2 | Product B | Category 1 | 25.00 |
| 3 | Product C | Category 2 | 75.00 |
| 4 | Product D | Category 2 | 100.00 |
| 5 | Product E | Category 3 | 150.00 |
---
**Query #2**
select * from orders;
| order_id | product_id | customer_id | quantity | order_date |
| -------- | ---------- | ----------- | -------- | ---------- |
| 1 | 1 | 101 | 2 | 2023-01-01 |
| 2 | 2 | 102 | 1 | 2023-01-02 |
| 3 | 3 | 103 | 3 | 2023-01-03 |
| 4 | 1 | 104 | 1 | 2023-01-04 |
| 5 | 5 | 105 | 2 | 2023-01-05 |
| 6 | 4 | 106 | 4 | 2023-01-06 |
| 7 | 2 | 107 | 2 | 2023-01-07 |
| 8 | 3 | 101 | 1 | 2023-01-08 |
| 9 | 1 | 108 | 2 | 2023-01-09 |
| 10 | 5 | 109 | 3 | 2023-01-10 |
---
**Query #3**
select product_id,
count(order_id) as order_count
from orders
group by product_id;
| product_id | order_count |
| ---------- | ----------- |
| 1 | 3 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
| 5 | 2 |
---
**Query #4**
SELECT p.product_category, t.total_quantity
FROM products p
JOIN (
SELECT product_id, SUM(quantity) AS total_quantity
FROM orders
GROUP BY product_id
) AS t
ON p.product_id = t.product_id;
| product_category | total_quantity |
| ---------------- | -------------- |
| Category 1 | 5 |
| Category 1 | 3 |
| Category 2 | 4 |
| Category 2 | 4 |
| Category 3 | 5 |
---
**Query #5**
select p.product_id, p.product_name, product_price*quantity as sales_revenue
from products p
join orders o
on p.product_id = o.product_id
order by sales_revenue desc
limit 5;
| product_id | product_name | sales_revenue |
| ---------- | ------------ | ------------- |
| 5 | Product E | 450.00 |
| 4 | Product D | 400.00 |
| 5 | Product E | 300.00 |
| 3 | Product C | 225.00 |
| 1 | Product A | 100.00 |
---
**Query #6**
SELECT customer_id
FROM orders
WHERE product_id = 4;
| customer_id |
| ----------- |
| 106 |
---
**Query #7**
select count(distinct customer_id)
from orders;
| count(distinct customer_id) |
| --------------------------- |
| 9 |
---
[View on DB Fiddle](https://www.db-fiddle.com/f/jeQknCYFGstK7Qxk6Tj6Uj/10)