-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment question 1 and 2 MySQL queries.sql
325 lines (161 loc) · 7.89 KB
/
Assignment question 1 and 2 MySQL queries.sql
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*Use the products table from SQL_STORE and write a query to display the name,
unit price, and a new price with a 10% increase rate.*/
use sql_store;
select p.name, p.unit_price, round((p.unit_price * 0.1 + p.unit_price), 2) as 'new_unit_price'
from products as p;
select p.name, p.unit_price, truncate((p.unit_price * 0.1 + p.unit_price), 2) as 'new_unit_price'
from products as p;
/*Write a query to get the items for order #6 with a total price of over 30. Use the
ORDER_ITEMS table from SQL_STORE database*/
select o.order_id, p.product_id, p.name, o.quantity, o.unit_price,
(o.quantity * o.unit_price) as 'total_price'
from order_items as o inner join products as p on o.product_id = p.product_id
where o.order_id = 6 and (o.quantity * o.unit_price) > 30;
select *, (quantity * unit_price) as 'total_price' from order_items
where order_id = 6 and (quantity * unit_price) > 30;
select product_id from order_items
where order_id = 6 and (quantity * unit_price) > 30;
select p.name as 'Product Name'
from order_items as o inner join products as p on o.product_id = p.product_id
where o.order_id = 6 and (o.quantity * o.unit_price) > 30;
/*Use SQL_STORE database & join the ORDER_ITEMS table with the products table
on the PRODUCT_ID column and select the specific columns of your choice. */
select p.name as 'Product', o.order_id as 'Order Id', o.quantity as 'Order Qty',
o.unit_price as 'Price', p.unit_price as 'Cost', p.quantity_in_stock as 'Stock Qty'
from order_items as o inner join products as p on o.product_id = p.product_id;
/*Join products table of SQL_INVENTORY with ORDER_ITEM_NOTES table of
SQL_STORE on PRODUCT_ID and select specific columns*/
select p.name as 'product_name', p.quantity_in_stock, p.unit_price, o.note
from sql_inventory.products as p inner join sql_store.order_item_notes as o
on p.product_id = o.product_id;
/*Use the ‘SQL_INVOICING’ database. Join the ‘payments’ table with the ‘clients’
table using the ‘CLINETS_ID’ column and then join the ‘payments’ table with the
‘PAYMENT_METHODS’ table using the ‘PAYMENT_METHOD’ column.*/
use sql_invoicing;
select * from payments as p inner join clients as c on
p.client_id = c.client_id inner join payment_methods as m on
m.payment_method_id = p.payment_method;
select p.date, p.amount, c.name, c.address, c.city, c.state, c.phone,
m.name as 'payment_method'
from payments as p inner join clients as c on
p.client_id = c.client_id inner join payment_methods as m on
m.payment_method_id = p.payment_method;
/*Write a query to perform left join on orders table and shippers table
of SQL_STORE on SHIPPER_ID and get the details of ORDER_ID, CUSTOMER_ID,
ORDER_DATE, SHIPPED_DATE & SHIPPERS_NAME*/
select o.order_id, o.customer_id, o.order_date, o.shipped_date
from sql_store.orders as o left outer join sql_store.shippers as s
on o.shipper_id = s.shipper_id;
/*Create a database with the name MYDBMS and create a ‘PERSONS’ table under
MYDBMS database with the following columns.
PERSON_ID,
FIRSTNAME,
LASTNAME,
AGE.
Make ‘PERSON_ID’ as primary key and give the appropriate data type to each column.*/
select * from sql_invoicing.payments as p inner join sql_invoicing.clients as c on
p.client_id = c.client_id inner join sql_invoicing.payment_methods as m on
m.payment_method_id = p.payment_method;
/*-----------------------------------------------------------------------------------*/
create database `mydbms`;
use `mydbms`;
create table `persons` (
`person_id` int(6),
`firstname` varchar(15),
`lastname` varchar(25),
`age` int(3),
primary key (`person_id`));
select * from persons;
create table `persons` (
`person_id` int(6) not null auto_increment,
`firstname` varchar(15),
`lastname` varchar(25),
`age` int(3),
primary key (`person_id`));
select * from persons;
create table `persons` (
`person_id` int not null auto_increment,
`firstname` varchar(15) default null,
`lastname` varchar(25) default null,
`age` int default null,
primary key (`person_id`)
);
select * from persons;
INSERT INTO `persons` VALUES (1,'Sachin','Tendulkar',50);
INSERT INTO `persons` VALUES (2,'Taimur','Khan',10);
INSERT INTO `persons` VALUES (3,'Brad','Pitt',55);
INSERT INTO `persons` VALUES (4,'John','Wick',53);
INSERT INTO `persons` VALUES (5,'James','Bond',47);
INSERT INTO `persons` VALUES (6,'Yuraj','Singh',43);
INSERT INTO persons (person_id,firstname, lastname, age) VALUES (1,'Sachin','Tendulkar',50);
INSERT INTO persons (person_id,firstname, lastname, age) VALUES (2,'Taimur','Khan',10);
INSERT INTO persons (person_id,firstname, lastname, age) VALUES (3,'Brad','Pitt',55);
INSERT INTO persons (person_id,firstname, lastname, age) VALUES (4,'John','Wick',53);
INSERT INTO persons (person_id,firstname, lastname, age) VALUES (5,'James','Bond',47);
INSERT INTO persons (person_id,firstname, lastname, age) VALUES (6,'Yuraj','Singh',43);
select * from persons;
/*Use the ‘SQL_INVOICING’ database.
Join the ‘payments’ table with the ‘clients’ table using the ‘CLINETS_ID’
column and then join the ‘payments’ table with the ‘PAYMENT_METHODS’ table using
the ‘PAYMENT_METHOD’ column. Select the following columns.
PAYMENT_ID from the payments table,
Amount from the payments table,
CLIENT_ID from the clients table,
Name as CLINET_NAME from the clients table,
Phone as CLIENT_PHONE from the clients table,
Name as PAYMENT_METHOD from PAYMENT_METHODS table
*/
use sql_invoicing;
select p.payment_id, p.amount, c.client_id, c.name as 'CLINET_NAME', c.phone as 'CLIENT_PHONE',
pm.name as 'PAYMENT_METHOD'
from payments as p inner join clients as c on p.client_id = c.client_id
inner join payment_methods as pm on pm.payment_method_id = p.payment_method;
select p.payment_id, p.amount, c.client_id, c.name as 'CLINET_NAME', c.phone as 'CLIENT_PHONE',
pm.name as 'PAYMENT_METHOD'
from
(payment_methods as pm inner join
(payments as p inner join clients as c on p.client_id = c.client_id)
on pm.payment_method_id = p.payment_method);
/*Create a ‘payment_details’ table*/
create table `payment_details` (
`tbl_id` int(11) not null auto_increment,
`payment_id` int(11) not null,
`amount` decimal(9,2) not null,
`client_id` int(11) not null,
`client_name` varchar(50) not null,
`client_phone` varchar(50) default null,
`payment_method` varchar(50) NOT NULL,
primary key (`tbl_id`));
select * from payment_details;
insert into payment_details (payment_id, amount, client_id, client_name, client_phone,
payment_method)
select p.payment_id, p.amount, c.client_id, c.name,
c.phone, pm.name
from
(payment_methods as pm inner join
(payments as p inner join clients as c on p.client_id = c.client_id)
on pm.payment_method_id = p.payment_method);
select * from payment_details;
/*Refer ‘PERSONS’ table, modify the column constraint of AGE and LASTNAME and
change it to not null*/
alter table persons change `age` `age` int not null;
alter table persons change `lastname` `lastname` varchar(25) not null;
/*after change the column constraints*/
/*below is the system generated sql script*/
CREATE TABLE `persons` (
`person_id` int NOT NULL AUTO_INCREMENT,
`firstname` varchar(15) DEFAULT NULL,
`lastname` varchar(25) NOT NULL,
`age` int NOT NULL,
PRIMARY KEY (`person_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*In the ‘PERSONS’ table, update the income of PERSON_ID 2, as 100 times of his age*/
alter table persons add column `income` decimal(9,2) default null;
update persons set income = (age * 100) where person_id = 2;
select * from persons;
/*Write a query to give 50 extra points to customers born before 1990. Use the customers table of SQL_STORE*/
use sql_store;
select * from customers;
select * from customers where birth_date < '1990-01-01';
update customers set points = (points + 50) where birth_date < '1990-01-01';
select * from customers;