-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData Digger.txt
More file actions
221 lines (152 loc) · 5.23 KB
/
Copy pathData Digger.txt
File metadata and controls
221 lines (152 loc) · 5.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
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
213
214
215
216
217
218
219
220
221
--------PR:-1 DATA DIGGER--------
:-used foreign key and primary key
:-used SUM() MAX() MIN() AVG() COUNT
:-used GROUP BY and AND and BETWEEN
------------------------------------------
1:- CUSTOMER TABLE
------------------------------------------
CREATE TABLE customer_table (
customer_id INT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(100),
address VARCHAR(100)
);
QUERIES TO PERFORM :-
1 :- Insert at least 5 customers into the customer table ?
INSERT INTO customer_table (customer_id, name, email, address)
VALUES
(101, 'Alice', 'alice@gmail.com', 'Adarsh Colony'),
(102, 'Samarth', 'samarth@gmail.com', 'Vidya Society'),
(103, 'Siddharth', 'siddharth@gmail.com', 'Sadguru Apartments'),
(104, 'Vedarth', 'vedarth@gmail.com', 'Skyline Heights'),
(105, 'Hetarth', 'hetarth@gmail.com', 'Shabrivatika Apartments');
2:- Retrieve all customers details ?
SELECT * FROM customer_table;
3:- Update a customer address ?
UPDATE customer_table
SET address = 'Agman Residency'
WHERE customer_id = 102;
4:- Delete a customer using their customerid ?
DELETE FROM customer_table
WHERE customer_id = 103;
5:- Display all customers whose name is 'Alice' ?
SELECT * FROM customer_table
WHERE name = 'Alice';
------------------------------------------
2:- ORDERS TABLE
------------------------------------------
CREATE TABLE orders_table (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
total_amount DECIMAL(10,2),
FOREIGN KEY (customer_id) REFERENCES customer_table(customer_id)
);
QUERIES TO PERFORM :-
1:- Insert at least 5 sample orders into the orders table ?
INSERT INTO orders_table (order_id, customer_id, order_date, total_amount)
VALUES
(111, 101, '2024-12-12', 35550),
(222, 102, '2024-10-09', 44810),
(333, 101, '2025-01-05', 12500),
(444, 104, '2024-11-09', 75110),
(555, 105, '2025-12-22', 24540);
2:- Retrieve all orders made by a specific customer ?
SELECT * FROM orders_table
WHERE customer_id = 102;
3:- Update an order's total amount ?
UPDATE orders_table
SET total_amount = 90000
WHERE order_id = 111;
4:- Delete an order using its orderid ?
DELETE FROM orders_table
WHERE order_id = 444;
5:- Retrieve Orders placed in the last 30 days ?
SELECT * FROM orders_table
WHERE order_date BETWEEN '2025-12-01' AND '2025-12-31';
6:- Retrieve the heighest, lowest, and average order amount using Aggregate functions ?
(1):- HIGHEST
SELECT MAX(total_amount)
FROM orders_table;
(2):- LOWEST
SELECT MIN(total_amount)
FROM orders_table;
(3):- AVERAGE
SELECT AVG(total_amount)
FROM orders_table;
------------------------------------------
3:- PRODUCTS TABLE
------------------------------------------
CREATE TABLE products_table (
product_id INT PRIMARY KEY,
product_name VARCHAR(50),
price DECIMAL(10,2),
stock INT
);
QUERIES TO PERFORM :-
1:- Insert at least 5 sample products into the products table ?
INSERT INTO products_table (product_id, product_name, price, stock)
VALUES
(110, 'Earbuds', 5450, 19),
(120, 'Mobile', 56600, 87),
(130, 'TV', 35900, 4),
(140, 'Washing Machine', 13234, 0),
(150, 'Grinder', 1500, 25);
2:- Retrieve all products sorted by price in descending order ?
SELECT * FROM products_table
ORDER BY price DESC;
3:- Update the price of a specific product ?
UPDATE products_table
SET price = 67000
WHERE product_id = 130;
4:- Delete a product if it is out of stock ?
DELETE FROM products_table
WHERE stock = 0;
5:- Retrieve Products whose price is between ₹500 and ₹2000 ?
SELECT * FROM products_table
WHERE price BETWEEN 500 AND 2000;
6:- Retrieve the Most expensive and cheapest products using MAX() and MIN() ?
MOST EXPENSIVE:-
SELECT MAX(price) AS most_expensive
FROM products_table;
MOST CHEAPEST:-
SELECT MIN(price) AS most_cheapest
FROM products_table;
------------------------------------------
4:- ORDER DETAILS TABLE
------------------------------------------
CREATE TABLE orderdetails_table (
orderdetail_id INT PRIMARY KEY,
order_id INT,
product_id INT,
quantity INT,
subtotal DECIMAL(10,2),
FOREIGN KEY (order_id) REFERENCES orders_table(order_id),
FOREIGN KEY (product_id) REFERENCES products_table(product_id)
);
QUERIES TO PERFORM :-
1:- Insert at least 5 sample records into the orderdetails table ?
INSERT INTO orderdetails_table (orderdetail_id, order_id, product_id, quantity, subtotal)
VALUES
(1, 111, 110, 2, 10900),
(2, 111, 150, 1, 1500),
(3, 222, 120, 1, 56600),
(4, 333, 130, 1, 67000),
(5, 555, 150, 3, 4500);
2:- Retrieve all order details for a specific order ?
SELECT * FROM orderdetails_table
WHERE order_id = 111;
3:- calculate the Total revenue generated from all orders using SUM() ?
SELECT SUM(subtotal)
FROM orderdetails_table;
4:- Retrieve the Top 3 most ordered products ?
SELECT product_id, SUM(quantity)
FROM orderdetails_table
GROUP BY product_id
HAVING SUM(quantity)> 1
LIMIT 3;
5:- Count how many times a specific product has been sold using COUNT() ?
SELECT product_id, COUNT(*)
FROM orderdetails_table
WHERE product_id = 150
GROUP BY product_id;