-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicQueries.sql
475 lines (415 loc) · 20.3 KB
/
BasicQueries.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* Clean up any previous homework answers */
DROP TABLE IF EXISTS ANSWER01;
DROP TABLE IF EXISTS ANSWER02;
DROP TABLE IF EXISTS ANSWER03;
DROP TABLE IF EXISTS ANSWER04;
DROP TABLE IF EXISTS ANSWER05;
DROP TABLE IF EXISTS ANSWER06;
DROP TABLE IF EXISTS ANSWER07;
DROP TABLE IF EXISTS ANSWER08;
DROP TABLE IF EXISTS ANSWER09;
DROP TABLE IF EXISTS ANSWER10;
DROP TABLE IF EXISTS TEMP0;
DROP TABLE IF EXISTS TEMP1;
DROP TABLE IF EXISTS TEMP2;
DROP TABLE IF EXISTS TEMP3;
DROP TABLE IF EXISTS TEMP4;
DROP TABLE IF EXISTS TEMP5;
DROP TABLE IF EXISTS TEMP6;
DROP TABLE IF EXISTS TEMP7;
DROP TABLE IF EXISTS TEMP8;
DROP TABLE IF EXISTS TEMP9;
DROP TABLE IF EXISTS TEMP10;
/* Clean up any previous version */
DROP TABLE IF EXISTS PRODUCTUSAGE;
DROP TABLE IF EXISTS CONTAINS;
DROP TABLE IF EXISTS ITEM;
DROP TABLE IF EXISTS ORDERS;
DROP TABLE IF EXISTS STORED;
DROP TABLE IF EXISTS WAREHOUSE;
DROP TABLE IF EXISTS PRODUCT;
DROP TABLE IF EXISTS CUSTOMER;
DROP TABLE IF EXISTS VENDOR;
DROP TABLE IF EXISTS SHIPPER;
DROP TABLE IF EXISTS COMPANY;
/* Define tables */
CREATE TABLE COMPANY(
EIN CHAR(16) PRIMARY KEY,
NAME CHAR(24) NOT NULL
);
CREATE TABLE SHIPPER(
EIN CHAR(16) PRIMARY KEY,
PRICEPERLB FLOAT,
FOREIGN KEY(EIN) REFERENCES COMPANY(EIN) ON DELETE CASCADE
);
CREATE TABLE CUSTOMER(
EIN CHAR(16) PRIMARY KEY,
CREDIT FLOAT,
FOREIGN KEY(EIN) REFERENCES COMPANY(EIN) ON DELETE CASCADE
);
CREATE TABLE VENDOR(
EIN CHAR(16) PRIMARY KEY,
FOREIGN KEY(EIN) REFERENCES COMPANY(EIN) ON DELETE CASCADE
);
CREATE TABLE WAREHOUSE(
SHIPPEREIN CHAR(16),
WAREHOUSENAME CHAR(24),
STATE CHAR(16),
CITY CHAR(16),
PRIMARY KEY(SHIPPEREIN, WAREHOUSENAME),
FOREIGN KEY(SHIPPEREIN) REFERENCES SHIPPER(EIN) ON DELETE CASCADE
);
CREATE TABLE PRODUCT(
PRODUCTNAME CHAR(24),
PRODUCTID CHAR(16),
PRICE FLOAT NOT NULL,
WEIGHTINLB FLOAT NOT NULL,
EIN CHAR(16),
PRIMARY KEY(PRODUCTNAME, PRODUCTID),
FOREIGN KEY(EIN) REFERENCES VENDOR(EIN) ON DELETE CASCADE
);
CREATE TABLE STORED(
SHIPPEREIN CHAR(16),
WAREHOUSENAME CHAR(24),
PRODUCTNAME CHAR(24),
PRODUCTID CHAR(16),
QUANTITY INT,
PRIMARY KEY(SHIPPEREIN, WAREHOUSENAME, PRODUCTNAME, PRODUCTID),
FOREIGN KEY(SHIPPEREIN, WAREHOUSENAME) REFERENCES WAREHOUSE(SHIPPEREIN, WAREHOUSENAME) ON DELETE CASCADE,
FOREIGN KEY(PRODUCTNAME, PRODUCTID) REFERENCES PRODUCT(PRODUCTNAME, PRODUCTID) ON DELETE CASCADE
);
CREATE TABLE ORDERS(
CUSTOMEREIN CHAR(16),
SHIPPEREIN CHAR(16),
WAREHOUSENAME CHAR(24),
PRODUCTNAME CHAR(24),
PRODUCTID CHAR(16),
QUANTITY INT,
PRIMARY KEY(CUSTOMEREIN, SHIPPEREIN, WAREHOUSENAME, PRODUCTNAME, PRODUCTID),
FOREIGN KEY(CUSTOMEREIN) REFERENCES CUSTOMER(EIN) ON DELETE CASCADE,
FOREIGN KEY(SHIPPEREIN, WAREHOUSENAME, PRODUCTNAME, PRODUCTID) REFERENCES STORED(SHIPPEREIN, WAREHOUSENAME, PRODUCTNAME, PRODUCTID) ON DELETE CASCADE
);
CREATE TABLE PRODUCTUSAGE(
PRODUCTUSAGEVALUE FLOAT,
PRODUCTNAME CHAR(24),
PRODUCTID CHAR(16),
PRIMARY KEY(PRODUCTUSAGEVALUE, PRODUCTNAME, PRODUCTID),
FOREIGN KEY(PRODUCTNAME, PRODUCTID) REFERENCES PRODUCT(PRODUCTNAME, PRODUCTID) ON DELETE CASCADE
);
CREATE TABLE ITEM(
ITEMID CHAR(16) PRIMARY KEY,
ITEMSIZE INT,
SECONDARY CHAR(16),
FOREIGN KEY(SECONDARY) REFERENCES ITEM(ITEMID) ON DELETE CASCADE
);
CREATE TABLE CONTAINS(
PRODUCTNAME CHAR(24),
PRODUCTID CHAR(16),
ITEMID CHAR(16),
PRIMARY KEY(PRODUCTNAME, PRODUCTID, ITEMID),
FOREIGN KEY(PRODUCTNAME, PRODUCTID) REFERENCES PRODUCT(PRODUCTNAME, PRODUCTID) ON DELETE CASCADE,
FOREIGN KEY(ITEMID) REFERENCES ITEM(ITEMID) ON DELETE CASCADE
);
/* Generate data */
INSERT INTO COMPANY VALUES('078-05-1120', 'IBM');
INSERT INTO COMPANY VALUES('917-34-6302', 'DELL');
INSERT INTO COMPANY VALUES('078-05-1123', 'HP');
INSERT INTO COMPANY VALUES('078-05-1130', 'APPLE');
INSERT INTO COMPANY VALUES('077-02-1330', 'GOOGLE');
INSERT INTO COMPANY VALUES('079-04-1120', 'ASUS');
INSERT INTO COMPANY VALUES('378-35-1108', 'LENOVO');
INSERT INTO COMPANY VALUES('278-05-1120', 'SAMSUNG');
INSERT INTO COMPANY VALUES('348-75-1450', 'ACER');
INSERT INTO COMPANY VALUES('256-90-4576', 'SONY');
INSERT INTO COMPANY VALUES('025-45-1111', 'TOSHIBA');
INSERT INTO COMPANY VALUES('025-59-1919', 'INTEL');
INSERT INTO COMPANY VALUES('567-45-2351', 'AMD');
INSERT INTO COMPANY VALUES('100-40-0011', 'QUAlCOMM');
INSERT INTO COMPANY VALUES('100-40-0012', 'AMAZON');
INSERT INTO COMPANY VALUES('111-21-3379', 'DHL');
INSERT INTO COMPANY VALUES('113-21-3856', 'USPS');
INSERT INTO COMPANY VALUES('108-34-9999', 'NEX EXPRESS');
INSERT INTO COMPANY VALUES('108-64-2222', 'AMEX');
INSERT INTO COMPANY VALUES('256-42-7755', 'DHL');
INSERT INTO COMPANY VALUES('798-99-0537', 'FLATRATE');
INSERT INTO COMPANY VALUES('776-87-0587', 'MTS LOGISTICS');
INSERT INTO COMPANY VALUES('234-90-9037', 'K INTERATIONAL TRANSPORT');
INSERT INTO COMPANY VALUES('874-87-9367', 'NY SHIPPING');
INSERT INTO COMPANY VALUES('546-23-0888', 'HIGHMARK INC.');
INSERT INTO COMPANY VALUES('098-29-9898', 'BT GROUP PLC');
INSERT INTO COMPANY VALUES('129-03-7643', 'APLICOR INC.');
INSERT INTO COMPANY VALUES('194-74-7823', 'MSFT');
INSERT INTO COMPANY VALUES('983-44-7897', 'VERIZON');
INSERT INTO COMPANY VALUES('397-89-7633', 'SUN');
INSERT INTO COMPANY VALUES('993-44-7897', 'NASA');
INSERT INTO COMPANY VALUES('997-89-7633', 'SALESFORCE');
INSERT INTO COMPANY VALUES('999-88-3312', 'CITI');
INSERT INTO COMPANY VALUES('999-98-5698', 'BOA');
INSERT INTO SHIPPER VALUES('111-21-3379', 1.89);
INSERT INTO SHIPPER VALUES('113-21-3856', 1.12);
INSERT INTO SHIPPER VALUES('108-34-9999', 0.98);
INSERT INTO SHIPPER VALUES('256-42-7755', 1.79);
INSERT INTO SHIPPER VALUES('798-99-0537', 2.13);
INSERT INTO SHIPPER VALUES('776-87-0587', 1.66);
INSERT INTO SHIPPER VALUES('234-90-9037', 1.78);
INSERT INTO SHIPPER VALUES('874-87-9367', 1.93);
INSERT INTO CUSTOMER VALUES('078-05-1120', 6.72);
INSERT INTO CUSTOMER VALUES('917-34-6302', 4.33);
INSERT INTO CUSTOMER VALUES('078-05-1123', 5.12);
INSERT INTO CUSTOMER VALUES('078-05-1130', null);
INSERT INTO CUSTOMER VALUES('079-04-1120', 6.23);
INSERT INTO CUSTOMER VALUES('378-35-1108', 7.24);
INSERT INTO CUSTOMER VALUES('278-05-1120', 3.12);
INSERT INTO CUSTOMER VALUES('348-75-1450', 8.11);
INSERT INTO CUSTOMER VALUES('256-90-4576', null);
INSERT INTO CUSTOMER VALUES('025-45-1111', 6.16);
INSERT INTO CUSTOMER VALUES('025-59-1919', 5.73);
INSERT INTO CUSTOMER VALUES('567-45-2351', null);
INSERT INTO CUSTOMER VALUES('100-40-0011', 7.13);
INSERT INTO VENDOR VALUES('100-40-0012');
INSERT INTO VENDOR VALUES('546-23-0888');
INSERT INTO VENDOR VALUES('098-29-9898');
INSERT INTO VENDOR VALUES('129-03-7643');
INSERT INTO VENDOR VALUES('194-74-7823');
INSERT INTO VENDOR VALUES('983-44-7897');
INSERT INTO VENDOR VALUES('397-89-7633');
INSERT INTO VENDOR VALUES('993-44-7897');
INSERT INTO VENDOR VALUES('997-89-7633');
INSERT INTO WAREHOUSE VALUES('111-21-3379', 'Americold Logistics', 'GE', 'Atlanta');
INSERT INTO WAREHOUSE VALUES('113-21-3856', 'Lineage Logistics', 'CA', 'Colton');
INSERT INTO WAREHOUSE VALUES('108-34-9999', 'Millard', 'Nebraska', 'Omaba');
INSERT INTO WAREHOUSE VALUES('798-99-0537', 'Freezer', 'NJ', 'Chatham');
INSERT INTO WAREHOUSE VALUES('776-87-0587', 'VersaCold', 'Indiana', 'Fort Wayne');
INSERT INTO WAREHOUSE VALUES('234-90-9037', 'Interstate Warehousing', null, 'Vancouver');
INSERT INTO WAREHOUSE VALUES('776-87-0587', 'Burnis Logistics', 'Deleware', 'Milford');
INSERT INTO WAREHOUSE VALUES('234-90-9037', 'US Cold Storage, Inc.', 'NJ', 'Voorhees');
INSERT INTO WAREHOUSE VALUES('234-90-9037', 'Cloverleaf, Inc.', 'IOWA', 'Sioux City');
INSERT INTO WAREHOUSE VALUES('798-99-0537', 'Nordic Logistic, LLC', 'GE', 'ATL');
INSERT INTO WAREHOUSE VALUES('798-99-0537', 'Columbia Colstor, Inc', 'WA', 'Moses Lake');
INSERT INTO WAREHOUSE VALUES('113-21-3856', 'Congebec Logistics, Inc', null, 'Quebec City');
INSERT INTO WAREHOUSE VALUES('113-21-3856', 'Frialsa Frigorificos De', 'Mexico', 'Tialnepantla Edo');
INSERT INTO WAREHOUSE VALUES('234-90-9037', 'Hanson Logistics.', 'NJ', 'Jersey City');
INSERT INTO PRODUCT VALUES('Books', '9527-24-8096', 9.9, 1.8, '100-40-0012');
INSERT INTO PRODUCT VALUES('Kindle', '1321-11-8899', 49, 0.8, '100-40-0012');
INSERT INTO PRODUCT VALUES('PC machine', '2527-24-8016', 199, 19.2, '100-40-0012');
INSERT INTO PRODUCT VALUES('Fire phone', '2333-44-9989', 99, 1.2, '100-40-0012');
INSERT INTO PRODUCT VALUES('Nut', '1537-24-8221', 18.9, 1.8, null);
INSERT INTO PRODUCT VALUES('Laptop', '2618-99-0505', 399, 6.6, '098-29-9898');
INSERT INTO PRODUCT VALUES('mobile phone', '2348-09-0445', 99, 0.8, '098-29-9898');
INSERT INTO PRODUCT VALUES('Car', '7756-01-3465', 10999, 2999, null);
INSERT INTO PRODUCT VALUES('Glasses', '1731-01-2425', 1418, 0.3, '129-03-7643');
INSERT INTO PRODUCT VALUES('XBOX', '2651-01-2126', 199, 2.01, '194-74-7823');
INSERT INTO PRODUCT VALUES('Routers', '3631-13-2351', 86, 1.00, '983-44-7897');
INSERT INTO PRODUCT VALUES('SERVERS', '9135-54-9238', 982, 30, '397-89-7633');
INSERT INTO PRODUCT VALUES('Storage Device', '9156-12-9238', 1480, 30, '397-89-7633');
INSERT INTO PRODUCT VALUES('GPS', '3235-65-7764', 500, 0.3, '993-44-7897');
INSERT INTO PRODUCT VALUES('CRM', '2398-89-1234', 1999, 0, '997-89-7633');
INSERT INTO PRODUCT VALUES('ERP', '2377-19-6534', 999, 0, '997-89-7633');
INSERT INTO PRODUCT VALUES('Bicycle', '0015-21-3921', 101, 98, null);
INSERT INTO PRODUCT VALUES('Music CDs', '1016-51-3919', 29, 0.5, null);
INSERT INTO STORED VALUES('111-21-3379', 'Americold Logistics', 'Kindle', '1321-11-8899', 200);
INSERT INTO STORED VALUES('111-21-3379', 'Americold Logistics', 'Books', '9527-24-8096', 1000);
INSERT INTO STORED VALUES('798-99-0537', 'Columbia Colstor, Inc', 'Fire phone', '2333-44-9989', 600);
INSERT INTO STORED VALUES('798-99-0537', 'Freezer', 'Laptop', '2618-99-0505', 1200);
INSERT INTO STORED VALUES('776-87-0587', 'VersaCold', 'mobile phone', '2348-09-0445', 1600);
INSERT INTO STORED VALUES('776-87-0587', 'VersaCold', 'Glasses', '1731-01-2425', 200);
INSERT INTO STORED VALUES('776-87-0587', 'VersaCold', 'Nut', '1537-24-8221', 6000);
INSERT INTO STORED VALUES('776-87-0587', 'Burnis Logistics', 'mobile phone', '2348-09-0445', 600);
INSERT INTO STORED VALUES('234-90-9037', 'US Cold Storage, Inc.', 'Nut', '1537-24-8221', 110000);
INSERT INTO STORED VALUES('234-90-9037', 'Cloverleaf, Inc.', 'Routers', '3631-13-2351', 2400);
INSERT INTO STORED VALUES('798-99-0537', 'Nordic Logistic, LLC', 'Storage Device', '9156-12-9238', 80);
INSERT INTO STORED VALUES('798-99-0537', 'Columbia Colstor, Inc', 'Nut', '1537-24-8221', 6000);
INSERT INTO STORED VALUES('234-90-9037', 'US Cold Storage, Inc.', 'mobile phone', '2348-09-0445', 1000);
INSERT INTO STORED VALUES('113-21-3856', 'Frialsa Frigorificos De', 'GPS', '3235-65-7764', 220);
INSERT INTO STORED VALUES('113-21-3856', 'Congebec Logistics, Inc', 'Bicycle', '0015-21-3921', 460);
INSERT INTO STORED VALUES('113-21-3856', 'Frialsa Frigorificos De', 'Music CDs', '1016-51-3919', 8700);
INSERT INTO STORED VALUES('234-90-9037', 'Cloverleaf, Inc.', 'Glasses', '1731-01-2425', 400);
INSERT INTO STORED VALUES('798-99-0537', 'Freezer', 'Books', '9527-24-8096', 2100);
INSERT INTO STORED VALUES('111-21-3379', 'Americold Logistics', 'Nut', '1537-24-8221', 20400);
INSERT INTO ORDERS VALUES('078-05-1120', '111-21-3379', 'Americold Logistics', 'Books', '9527-24-8096', 80);
INSERT INTO ORDERS VALUES('078-05-1120', '798-99-0537', 'Freezer', 'Laptop', '2618-99-0505', 130);
INSERT INTO ORDERS VALUES('078-05-1123', '798-99-0537', 'Freezer', 'Laptop', '2618-99-0505', 400);
INSERT INTO ORDERS VALUES('378-35-1108', '776-87-0587', 'VersaCold', 'mobile phone', '2348-09-0445', 720);
INSERT INTO ORDERS VALUES('256-90-4576', '234-90-9037', 'Cloverleaf, Inc.', 'Routers', '3631-13-2351', 140);
INSERT INTO ORDERS VALUES('378-35-1108', '798-99-0537', 'Freezer', 'Books', '9527-24-8096', 180);
INSERT INTO ORDERS VALUES('378-35-1108', '798-99-0537', 'Freezer', 'Laptop', '2618-99-0505', 40);
INSERT INTO ORDERS VALUES('025-59-1919', '776-87-0587', 'VersaCold', 'mobile phone', '2348-09-0445', 270);
INSERT INTO ORDERS VALUES('025-59-1919', '113-21-3856', 'Frialsa Frigorificos De', 'GPS', '3235-65-7764', 210);
INSERT INTO ORDERS VALUES('567-45-2351', '111-21-3379', 'Americold Logistics', 'Kindle', '1321-11-8899', 198);
INSERT INTO ORDERS VALUES('567-45-2351', '113-21-3856', 'Frialsa Frigorificos De', 'Music CDs', '1016-51-3919', 2800);
INSERT INTO ORDERS VALUES('348-75-1450', '113-21-3856', 'Congebec Logistics, Inc', 'Bicycle', '0015-21-3921', 360);
INSERT INTO PRODUCTUSAGE VALUES(77.6, 'PC machine', '2527-24-8016');
INSERT INTO PRODUCTUSAGE VALUES(37.6, 'PC machine', '2527-24-8016');
INSERT INTO PRODUCTUSAGE VALUES(97.5, 'Fire phone', '2333-44-9989');
INSERT INTO PRODUCTUSAGE VALUES(32, 'Books', '9527-24-8096');
INSERT INTO PRODUCTUSAGE VALUES(21, 'Books', '9527-24-8096');
INSERT INTO PRODUCTUSAGE VALUES(52, 'Books', '9527-24-8096');
INSERT INTO PRODUCTUSAGE VALUES(92, 'mobile phone', '2348-09-0445');
INSERT INTO PRODUCTUSAGE VALUES(97, 'mobile phone', '2348-09-0445');
INSERT INTO PRODUCTUSAGE VALUES(66, 'Storage Device', '9156-12-9238');
INSERT INTO PRODUCTUSAGE VALUES(89, 'GPS', '3235-65-7764');
INSERT INTO PRODUCTUSAGE VALUES(81.1, 'Bicycle', '0015-21-3921');
INSERT INTO PRODUCTUSAGE VALUES(88.4, 'Music CDs', '1016-51-3919');
INSERT INTO PRODUCTUSAGE VALUES(74.5, 'Music CDs', '1016-51-3919');
INSERT INTO ITEM VALUES('0003', 2, null);
INSERT INTO ITEM VALUES('0004', 7, null);
INSERT INTO ITEM VALUES('0009', 11, null);
INSERT INTO ITEM VALUES('0019', 31, null);
INSERT INTO ITEM VALUES('0021', 23, null);
INSERT INTO ITEM VALUES('0016', 9, null);
INSERT INTO ITEM VALUES('0048', 87, null);
INSERT INTO ITEM VALUES('0024', 34, null);
INSERT INTO ITEM VALUES('0026', 37, null);
INSERT INTO ITEM VALUES('0001', 6, '0003');
INSERT INTO ITEM VALUES('0002', 8, '0004');
INSERT INTO ITEM VALUES('0007', 12, '0009');
INSERT INTO ITEM VALUES('0017', 22, '0021');
INSERT INTO ITEM VALUES('0018', 32, '0019');
INSERT INTO ITEM VALUES('0058', 30, '0019');
INSERT INTO ITEM VALUES('0038', 12, '0009');
INSERT INTO CONTAINS VALUES('Books', '9527-24-8096','0001');
INSERT INTO CONTAINS VALUES('Books', '9527-24-8096','0002');
INSERT INTO CONTAINS VALUES('Books', '9527-24-8096','0009');
INSERT INTO CONTAINS VALUES('Laptop', '2618-99-0505','0017');
INSERT INTO CONTAINS VALUES('Laptop', '2618-99-0505', '0021');
INSERT INTO CONTAINS VALUES('Fire phone', '2333-44-9989','0018');
INSERT INTO CONTAINS VALUES('Fire phone', '2333-44-9989','0019');
INSERT INTO CONTAINS VALUES('mobile phone', '2348-09-0445','0019');
INSERT INTO CONTAINS VALUES('mobile phone', '2348-09-0445','0018');
INSERT INTO CONTAINS VALUES('XBOX', '2651-01-2126','0016');
INSERT INTO CONTAINS VALUES('Storage Device', '9156-12-9238','0058');
INSERT INTO CONTAINS VALUES('Car', '7756-01-3465','0048');
INSERT INTO CONTAINS VALUES('Bicycle', '0015-21-3921','0009');
INSERT INTO CONTAINS VALUES('Books', '9527-24-8096','0024');
INSERT INTO CONTAINS VALUES('Bicycle', '0015-21-3921','0026');
/*********************************
INSERT YOUR SOLUTIONS IN PART 03
*********************************/
/* 1.Produce table Answer01 (ProductName, Product_ID) which contains
all products that can not be made by vendor. */
/* (a) CREATE TABLE ANSWER01 as */
CREATE TABLE ANSWER01
AS
SELECT DISTINCT PRODUCTNAME, PRODUCTID FROM PRODUCT
WHERE (EIN IS NULL)
ORDER BY PRODUCTNAME ASC, PRODUCTID ASC;
/* 2.Produce table Answer02 (EIN, NAME) which contains all companies
that are not in SHIPPER, CUSTOMER and VENDOR. */
/* (b) CREATE TABLE ANSWER02 as */
CREATE TABLE TEMP1 AS
SELECT DISTINCT EIN FROM CUSTOMER
UNION
SELECT DISTINCT EIN FROM SHIPPER
UNION
SELECT DISTINCT EIN FROM VENDOR;
CREATE TABLE ANSWER02
AS
SELECT DISTINCT * FROM COMPANY
WHERE COMPANY.EIN NOT IN (SELECT EIN FROM TEMP1)
ORDER BY COMPANY.EIN ASC;
/* 3.Produce table Answer03 (AVERAGE_PRICE) which contains
the average price of the products that are heavier than 10 LB. */
/* (c) CREATE TABLE ANSWER03 as */
CREATE TABLE TEMP3 AS
SELECT DISTINCT EIN, PRICE FROM PRODUCT
WHERE (WEIGHTINLB > 10);
CREATE TABLE ANSWER03 AS
SELECT AVG(PRICE) AS AVERAGE_PRICE FROM TEMP3;
/* 4.Produce table Answer04 (CUSTOMER_NAME, CREDIT_RATING, PRODUCT_NAME)
which contains all the customers and the products they ordered.*/
/* (d) CREATE TABLE ANSWER04 as */
CREATE TABLE TEMP4 AS
SELECT ORDERS.CUSTOMEREIN, ORDERS.PRODUCTNAME, COMPANY.NAME FROM ORDERS, COMPANY
WHERE (COMPANY.EIN = ORDERS.CUSTOMEREIN);
CREATE TABLE ANSWER04
AS
SELECT TEMP4.NAME AS CUSTOMER_NAME, CUSTOMER.CREDIT AS CREDIT_RATING, PRODUCTNAME AS PRODUCT_NAME FROM TEMP4, CUSTOMER
WHERE(CUSTOMER.EIN = TEMP4.CUSTOMEREIN)
ORDER BY CUSTOMER_NAME ASC, CREDIT_RATING ASC, PRODUCTNAME ASC;
/* 5.Produce table Answer05 (SHIPPER_NAME, SHIPPER_EIN, PRICE_LB)
that contains all SHIPPERs that have more than 2 warehouses,
in name decreasing order.
*/
/* (e) CREATE TABLE ANSWER05 as */
CREATE TABLE TEMP5 AS
SELECT SHIPPEREIN AS SHIPPER_EIN FROM WAREHOUSE
GROUP BY SHIPPEREIN HAVING COUNT(*) > 2;
CREATE TABLE TEMP6 AS
SELECT SHIPPER.PRICEPERLB AS PRICE_LB, TEMP5.SHIPPER_EIN FROM SHIPPER, TEMP5
WHERE(TEMP5.SHIPPER_EIN = SHIPPER.EIN);
CREATE TABLE ANSWER05 AS
SELECT COMPANY.NAME AS SHIPPER_NAME, TEMP6.SHIPPER_EIN, TEMP6.PRICE_LB FROM COMPANY, TEMP6
WHERE (COMPANY.EIN = SHIPPER_EIN)
ORDER BY COMPANY.NAME DESC;
/* 6.Produce the table Answer06 (PRODUCT_1, PRODUCT_2) that contains all
pairs of products where
i. at least one usagevalue of the first product is larger than at least
one usagevalue of the second product;
ii. the price of the first product is smaller than the second product's
*/
/* (f) CREATE TABLE ANSWER06 as */
CREATE TABLE TEMP2 AS
SELECT PRODUCTUSAGE.PRODUCTNAME, PRODUCTUSAGE.PRODUCTUSAGEVALUE, PRODUCT.PRODUCTID, PRICE FROM PRODUCTUSAGE, PRODUCT
WHERE PRODUCT.PRODUCTID = PRODUCTUSAGE.PRODUCTID;
CREATE TABLE TEMP2B AS
SELECT PRODUCTUSAGE.PRODUCTNAME, PRODUCTUSAGE.PRODUCTUSAGEVALUE, PRODUCT.PRODUCTID, PRODUCT.PRICE FROM PRODUCTUSAGE, PRODUCT
WHERE PRODUCT.PRODUCTID = PRODUCTUSAGE.PRODUCTID;
CREATE TABLE ANSWER06
AS
SELECT DISTINCT TEMP2.PRODUCTNAME AS PRODUCT_1, TEMP2B.PRODUCTNAME AS PRODUCT_2 FROM TEMP2, TEMP2B
WHERE TEMP2.PRODUCTUSAGEVALUE > TEMP2B.PRODUCTUSAGEVALUE
AND TEMP2.PRICE < TEMP2B.PRICE
GROUP BY PRODUCT_1, PRODUCT_2
ORDER BY PRODUCT_1 ASC;
/* 7.Produce the table Answer07 (CUSTOMER_EIN, CUSTOMER_NAME) that contains all CUSTOMERs
the products they ordered cover all the products that IBM ordered, other
than IBM itself */
/* (g) CREATE TABLE ANSWER07 as */
CREATE TABLE TEMP7 AS
SELECT DISTINCT CUSTOMEREIN, PRODUCTID FROM ORDERS
WHERE PRODUCTID = '9527-24-8096'
AND CUSTOMEREIN <> '078-05-1120';
CREATE TABLE TEMP8 AS
SELECT DISTINCT CUSTOMEREIN, PRODUCTID FROM ORDERS
WHERE PRODUCTID = '2618-99-0505'
AND CUSTOMEREIN <> '078-05-1120';
CREATE TABLE TEMP9 AS
SELECT TEMP7.CUSTOMEREIN FROM TEMP7
WHERE EXISTS (SELECT CUSTOMEREIN FROM TEMP8);
CREATE TABLE ANSWER07
SELECT DISTINCT TEMP9.CUSTOMEREIN AS CUSTOMER_EIN, COMPANY.NAME AS CUSTOMER_NAME FROM TEMP9, COMPANY
WHERE (TEMP9.CUSTOMEREIN = COMPANY.EIN);
/* 8.Produce the table Answer08 (PRODUCT_NAME, PRICE, ITEMSIZE) which contains
the product whose corresponding item size is maximal among all items*/
/* (h) CREATE TABLE ANSWER08 as */
CREATE TABLE TEMP10 AS
SELECT CONTAINS.PRODUCTID, CONTAINS.ITEMID, ITEM.ITEMSIZE, PRODUCT.PRODUCTNAME, PRODUCT.PRICE FROM CONTAINS, ITEM, PRODUCT
WHERE CONTAINS.ITEMID = ITEM.ITEMID
AND CONTAINS.PRODUCTID = PRODUCT.PRODUCTID
AND CONTAINS.PRODUCTNAME = PRODUCT.PRODUCTNAME;
CREATE TABLE ANSWER08
AS
SELECT TEMP10.PRODUCTNAME AS PRODUCT_NAME, TEMP10.PRICE, MAX(TEMP10.ITEMSIZE) AS ITEMSIZE FROM TEMP10
WHERE ITEMSIZE = (SELECT MAX(TEMP10.ITEMSIZE) FROM TEMP10);
/* Produce the table Answer09 (CUSTOMER_NAME, SHIPMENT_PRICE) which contains
all customer companies' name and the total shipment price they need to pay
SHIPPERS, in descending order by SHIPMENT_PRICE (please read the definition
of SHIPMENT payment in Relation file) */
/* (i) CREATE TABLE ANSWER09 as */
CREATE TABLE ANSWER09
AS
SELECT COMPANY.NAME AS CUSTOMER_NAME, SUM(ORDERS.QUANTITY * SHIPPER.PRICEPERLB * PRODUCT.WEIGHTINLB) AS SHIPMENT_PRICE
FROM COMPANY, CUSTOMER, ORDERS, SHIPPER, PRODUCT
WHERE ORDERS.SHIPPEREIN = SHIPPER.EIN AND COMPANY.EIN = CUSTOMER.EIN AND CUSTOMER.EIN = ORDERS.CUSTOMEREIN
AND ORDERS.PRODUCTNAME = PRODUCT.PRODUCTNAME AND ORDERS.PRODUCTID = PRODUCT.PRODUCTID
GROUP BY COMPANY.NAME
ORDER BY SHIPMENT_PRICE DESC;
SELECT * FROM ANSWER01;
SELECT * FROM ANSWER02;
SELECT * FROM ANSWER03;
SELECT * FROM ANSWER04;
SELECT * FROM ANSWER05;
SELECT * FROM ANSWER06;
SELECT * FROM ANSWER07;
SELECT * FROM ANSWER08;
SELECT * FROM ANSWER09;