This repository has been archived by the owner on Jun 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
assignment-2a.sql
681 lines (622 loc) · 10.5 KB
/
assignment-2a.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
-- Anindya Kundu - 510817020
-- Create Table Branch
create table Branch (
B_name varchar2(10) PRIMARY KEY,
City varchar2(10),
CHECK(City IN('Delhi', 'Kolkata', 'Mumbai'))
);
-- Create Table Customer
create table Customer (
C_name varchar2(10) PRIMARY KEY,
City varchar2(10) NOT NULL,
CHECK(C_name = UPPER(C_name))
);
-- Create Table Deposit
create table Deposit (
Act_no varchar2(10) PRIMARY KEY,
C_name varchar2(10),
B_name varchar2(10),
Amount numeric(8,2) NOT NULL,
FOREIGN KEY(C_name) REFERENCES Customer(C_name),
FOREIGN KEY(B_name) REFERENCES Branch(B_name)
);
-- Create Table Borrow
create table Borrow (
Loan_no varchar2(10) PRIMARY KEY,
C_name varchar2(10),
B_name varchar2(10),
Amount numeric(8,2),
FOREIGN KEY(C_name) REFERENCES Customer(C_name),
FOREIGN KEY(B_name) REFERENCES Branch(B_name),
CHECK(Amount > 1000),
CHECK(Loan_no = UPPER(Loan_no))
);
-- Insert values into Branch
insert into Branch (
B_name,
City
) values (
'Hauz Khas',
'Delhi'
);
insert into Branch (
B_name,
City
) values (
'Chembur',
'Mumbai'
);
insert into Branch (
B_name,
City
) values (
'R K Puram',
'Delhi'
);
insert into Branch (
B_name,
City
) values (
'Jadavpur',
'Kolkata'
);
-- Insert values into Customer
insert into Customer (
C_name,
City
) values (
'ANJISHNU',
'Mumbai'
);
insert into Customer (
C_name,
City
) values (
'MAINAK',
'Delhi'
);
insert into Customer (
C_name,
City
) values (
'SHOUNAK',
'Mumbai'
);
insert into Customer (
C_name,
City
) values (
'MOYEEN',
'Kolkata'
);
-- Insert values into Deposit
insert into Deposit (
Act_no,
C_name,
B_name,
Amount
) values (
'BA1572',
'SHOUNAK',
'Jadavpur',
10000
);
insert into Deposit (
Act_no,
C_name,
B_name,
Amount
) values (
'DB2222',
'ANJISHNU',
'Chembur',
15000
);
insert into Deposit (
Act_no,
C_name,
B_name,
Amount
) values (
'AJ4675',
'MAINAK',
'Hauz Khas',
6500
);
insert into Deposit (
Act_no,
C_name,
B_name,
Amount
) values (
'RN7200',
'MOYEEN',
'Jadavpur',
2000
);
-- Insert values into Borrow
insert into Borrow (
Loan_no,
C_name,
B_name,
Amount
) values (
'SNS11',
'MOYEEN',
'R K Puram',
5000
);
insert into Borrow (
Loan_no,
C_name,
B_name,
Amount
) values (
'AFB26',
'SHOUNAK',
'R K Puram',
2000
);
insert into Borrow (
Loan_no,
C_name,
B_name,
Amount
) values (
'AFB28',
'ANJISHNU',
'Jadavpur',
4500
);
insert into Borrow (
Loan_no,
C_name,
B_name,
Amount
) values (
'TRR65',
'MAINAK',
'Hauz Khas',
6550
);
-- Queries
--1
select Customer.C_name
from Deposit, Customer, Branch
where
Deposit.C_name = Customer.C_name
and Branch.B_name = Deposit.B_name
and Customer.City = 'Mumbai'
and Branch.City = 'Kolkata'
union
select Customer.C_name
from Borrow, Customer, Branch
where
Borrow.C_name = Customer.C_name
and Customer.City = 'Mumbai'
and Branch.B_name = Borrow.B_name
and Branch.City = 'Kolkata';
--2
select Customer.C_name
from Deposit, Customer, Branch
where
Deposit.C_name = Customer.C_name
and Deposit.B_name = Branch.B_name
and Customer.City = Branch.City
union
select Customer.C_name
from Borrow, Customer, Branch
where
Borrow.C_name = Customer.C_name
and Borrow.B_name = Branch.B_name
and Customer.City = Branch.City;
--3
select distinct C_name
from Deposit, Branch
where
Deposit.B_name = Branch.B_name
and Branch.City = (
select City
from Deposit, Branch
where
C_name = 'SHOUNAK'
and Deposit.B_name = Branch.B_name)
and not C_name = 'SHOUNAK';
--4
select Act_no, C_name, Deposit.B_name, Amount
from Deposit, Branch
where
Deposit.B_name = Branch.B_name
and Branch.City = (
select City
from Customer
where C_name = 'SHOUNAK')
union
select Loan_no, C_name, Borrow.B_name, Amount
from Borrow, Branch
where
Borrow.B_name = Branch.B_name
and Branch.City = (
select City
from Customer
where C_name = 'SHOUNAK');
--5
select count(Deposit.C_name)
from Deposit, Borrow
where Deposit.C_name = Borrow.C_name;
--6
select C_name
from Borrow
where
Amount > (
select Amount
from Borrow
where C_name = 'ANJISHNU');
--7
select Deposit.C_name
from Deposit, Branch, (
select distinct Branch.City
from Branch, Deposit, Borrow
where
(Deposit.C_name = 'SHOUNAK'
and Deposit.B_name = Branch.B_name)
or
(Borrow.C_name = 'SHOUNAK'
and Borrow.B_name = Branch.B_name))
Scity
where Deposit.B_name = Branch.B_name
and Branch.City = Scity.City
and Branch.City = (
select City
from Customer
where C_name = 'MOYEEN');
--8
select Customer.C_name
from Deposit, Customer, (
select Deposit.C_name
from Deposit, Branch
where
Deposit.B_name = Branch.B_name
and Branch.City = 'Delhi'
union
select Borrow.C_name
from Borrow, Branch
where
Borrow.B_name = Branch.B_name
and Branch.City = 'Delhi'
) Nname
where
Deposit.C_name = Customer.C_name
and Customer.City = 'Kolkata'
and Customer.C_name = Nname.C_name;
--9
select Customer.C_name
from Customer, (
select City
from Customer
where C_name = 'SHOUNAK'
) Ncity, (
select C_name
from Borrow
union
select C_name
from Deposit
) Nnames
where
Customer.City = Ncity.City
and Customer.C_name = Nnames.C_name
and not Customer.C_name = 'SHOUNAK';
--10
select distinct Customer.C_name
from Customer, (
select City
from Borrow, Branch
where
(C_name = 'SHOUNAK'
and Borrow.B_name = Branch.B_name)
union
select City
from Deposit, Branch
where
(C_name = 'SHOUNAK'
and Deposit.B_name = Branch.B_name)
) Ncity, (
select C_name
from Borrow
union
select C_name
from Deposit
) Nnames, (
select C_name, City
from Borrow, Branch
where
Borrow.B_name = Branch.B_name
union
select C_name, City
from Deposit, Branch
where
Deposit.B_name = Branch.B_name
) Ccity
where
Customer.City = 'Kolkata'
and Customer.C_name = Nnames.C_name
and Ccity.City = Ncity.City
and Customer.C_name = Nnames.C_name
and not Customer.C_name = 'SHOUNAK';
--11
select distinct City
from Borrow, Branch
where
Borrow.B_name = Branch.B_name
and
C_name = 'SHOUNAK'
or C_name = 'MAINAK';
union
select distinct City
from Deposit, Branch
where
Deposit.B_name = Branch.B_name
and
C_name = 'SHOUNAK'
or C_name = 'MAINAK';
--12
select B_name
from (
select max(C_no) as C_max
from (
select B_name, count(B_name) as C_no
from Deposit
group by B_name
) Bcnt
) Cm, (
select B_name, count(B_name) as C_no
from Deposit
group by B_name
) Bc
where
Cm.C_max = Bc.C_no;
--13
select C_name
from (
select max(A_no) as A_max
from (
select C_name, max(Amount) as A_no
from Deposit
group by C_name
) Bcnt
) Cm, (
select C_name, max(Amount) as A_no
from Deposit
group by C_name
) Bc
where
Cm.A_max = Bc.A_no;
--14
select Customer.C_name, Amt
from Customer, (
select C_name, sum(Amount) as Amt
from Deposit
group by C_name
union
select C_name, sum(Amount) as Amt
from Borrow
group by C_name
) Nname
where
Nname.C_name = Customer.C_name
and City = 'Delhi';
--15
select Deposit.C_name
from Deposit, (
select B_name
from Borrow
where
C_name = 'ANJISHNU'
union
select B_name
from Deposit
where
C_name = 'ANJISHNU'
) Brch
where
Deposit.B_name = Brch.B_name
and not Deposit.C_name = 'ANJISHNU';
--16
select sum(Amount)
from Customer, (
select City
from Customer
where
C_name = 'SHOUNAK'
) Csh, Deposit
where
Customer.City = Csh.City
and Deposit.C_name = Customer.C_name;
--17
select C_name
from Deposit
where
B_name = 'Jadavpur'
and Amount = (
select max(Amount)
from Deposit
where B_name = 'Jadavpur'
group by B_name
);
--18
select C_name
from Deposit
where
Amount = (
select max(Amount)
from Deposit, (
select B_name
from Branch
where
Branch.City in (
select City
from Branch, Deposit
where
Branch.B_name = Deposit.B_name
and C_name = 'SHOUNAK'
union
select City
from Branch, Borrow
where
Branch.B_name = Borrow.B_name
and C_name = 'SHOUNAK'
)
) Sbrch
where
Deposit.B_name in Sbrch.B_name
);
--19
select Deposit.C_name
from Deposit, Customer
where
Customer.C_name = Deposit.C_name
and Customer.city = 'Delhi'
intersect (
select C_name
from Deposit
where B_name = 'Hauz Khas'
union
select C_name
from Borrow
where B_name = 'Hauz Khas'
);
--20
select depBr, dep_cnt, brw_cnt
from (
select B_name as depBr, count(distinct(C_name)) as dep_cnt
from Deposit
group by B_name
), (
select B_name as brwBr, count(distinct(C_name)) as brw_cnt
from Borrow
group by B_name
)
where
depBr = brwBr
and dep_cnt > brw_cnt;
--21
select req_city, C_name
from borrow, (
select req_city, max(amt) as Max_Borrow
from (
select C_name, amount as amt, city as req_city
from Borrow, Branch
where Branch.b_name = Borrow.b_name
)
group by req_city
)
where borrow.amount = Max_Borrow;
--22
update Deposit
set Amount = Amount * 1.1
where C_name in (
select Deposit.C_name
from Deposit, Customer, Branch
where
Deposit.B_name = Branch.B_name
and Customer.C_name = Deposit.C_name
and Customer.City = 'Kolkata'
and Customer.C_name in (
select C_name
from Deposit, Branch
where
Deposit.B_name = Branch.B_name
and Branch.city = 'Delhi'
union
select C_name
from Borrow, Branch
where
Borrow.B_name = Branch.B_name
and Branch.City = 'Delhi'
)
);
--23
update Deposit
set Amount = (
select max(Amount)
from Deposit
where B_name = 'Jadavpur'
)
where C_name = 'MAINAK';
--24
update Deposit
set Amount = Amount - 1000
where
C_name = 'ANJISHNU'
and B_name in (
select B_name
from Deposit
where C_name = 'MAINAK'
);
update Deposit
set Amount = Amount + 1000
where
C_name = 'MAINAK'
and b_name in (
select B_name
from Deposit
where C_name = 'ANJISHNU'
);
--25
update Deposit
set Amount = Amount + 1000
where
C_name in (
select C_name
from Deposit
where Amount = (
select max(Amount)
from Deposit
group by B_name
)
);
--26
update Deposit
set Amount = Amount + 1000
where C_name in (
select C_name
from Deposit
group by B_name
where
--27
delete from Borrow
where B_name in (
select B_name
from Borrow
group by B_name
having avg(Amount) < 1000
);
--28
delete from Borrow
where B_name in (
select B_name
from Borrow
group by B_name
having count(C_name) = (
select min(count(C_name))
from Borrow
group by B_name
)
);
--29
delete from Deposit
where C_name in (
select C_name
from Customer
where
C_name in ('SHOUNAK', 'MAINAK')
and City = (
select City
from Customer
where C_name = 'SHOUNAK'
)
and City = (
select City
from Customer
where c_name='MAINAK')
);