-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexpt6.sql
More file actions
207 lines (168 loc) · 5.34 KB
/
expt6.sql
File metadata and controls
207 lines (168 loc) · 5.34 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
create database expt6;
use expt6;
create table branch(branch_id int,
branch_name varchar(20),
branch_city varchar(40));
create table customer(customer_id int,
customer_name varchar(20),
customer_city varchar(20));
create table savings(customer_id int,
branch_id int,
savings_accno varchar(20),
balance int);
create table loan(customer_id int,
branch_id int,
loan_accno varchar(20),
balance int);
insert into branch
values(401,"Thiruvananthapuram","Thiruvananthapuram"),
(402,"Kottayam","Kottayam"),
(403,"Ernakulam","Kochi"),
(404,"Kozhikode","Kozhikode");
insert into customer
values (6001,"Ananthakrishnan","Thiruvananthapuram"),
(6002,"Irfan","Thiruvananthapuram"),
(6003,"Suneeth","Thiruvananthapuram"),
(6004,"Sreejith","Kottayam"),
(6005,"Jafar","Kazhakoottam"),
(6006,"Radika","Pampady"),
(6007,"Jameela","Kanjikuzhi"),
(6008,"Bindu","Kottayam"),
(6009,"Purushothaman","Kollam"),
(6010,"Vincy","Kottayam"),
(6011,"Abdul Rahman","Thrissur"),
(6012,"Vishwanathan","Ernakulam"),
(6013,"Marykutty","Mattancheri"),
(6014,"Hajara","Ernakulam"),
(6015,"Revathy","Kozhikode"),
(6016,"Hameed","Perambra"),
(6017,"Suchithra","Kozhikode"),
(6018,"Saneesh","North Paravoor"),
(6019,"Gokul Das","Kozhikode"),
(6020,"Abraham","Kappad");
insert into savings
values (6001,401,6400101,15000),
(6002,401,6400102,200000),
(6005,401,6400105,30000),
(6007,401,6400107,70000),
(6004,402,6400204,400000),
(6006,402,6400206,100000),
(6007,402,6400207,40000),
(6008,402,6400208,74000),
(6010,402,6400210,128507),
(6011,403,6400311,700000),
(6001,403,6400301,200000),
(6012,403,6400312,500000),
(6013,403,6400313,250000),
(6015,404,6400415,100000),
(6016,404,6400416,90756);
insert into loan
values (6005,401,4600105,100000),
(6003,401,4600103,200000),
(6009,401,4600109,150000),
(6019,401,4600119,100000),
(6011,403,4600311,1000000),
(6013,403,4600313,500000),
(6014,403,4600314,300000),
(6018,403,4600318,300000),
(6006,403,4600306,100000),
(6009,403,4600309,200000),
(6014,404,4600414,100000),
(6016,404,4600416,150000),
(6017,404,4600417,200000),
(6019,404,4600419,300000),
(6020,404,4600419,400000),
(6008,404,4600408,100000);
/******** displaying tables **********/
SELECT * FROM branch;
SELECT * FROM customer;
SELECT * FROM savings;
SELECT * FROM loan;
/* creating views */
create view savings_details as
select s.customer_id,s.branch_id,s.savings_accno as accno,s.balance,b.branch_name,b.branch_city,c.customer_name,c.customer_city
from branch as b,customer as c,savings as s
where s.customer_id=c.customer_id and s.branch_id=b.branch_id;
create view loan_details as
select l.customer_id,l.branch_id,l.loan_accno as accno,l.balance,b.branch_name,b.branch_city,c.customer_name,c.customer_city
from loan as l,branch as b,customer as c
where l.customer_id=c.customer_id and l.branch_id=b.branch_id;
create view details as
select * from savings_details
union
select * from loan_details;
/* displaying views */
select * from details;
select * from loan_details;
select * from savings_details;
/* 6.a */
select customer_id,customer_name,customer_city,branch_city
from details
where customer_city=branch_city;
/* 6b */
select distinct customer_id,customer_name,branch_city
from details
where branch_city="Kottayam";
/* 6.c */
select * from details;
/* 6.d.1 */
select distinct d1.customer_id,d1.customer_name
from details as d1,details as d2
where d1.branch_id!=d2.branch_id and d1.customer_id=d2.customer_id;
/* 6.d.2 */
select distinct customer_id,customer_name
from details
where customer_id not in (select customer_id from loan);
/* 6.d.3 */
SELECT customer_id,customer_name
FROM details
WHERE customer_id IN (SELECT savings.customer_id FROM savings,loan
WHERE savings.customer_id=loan.customer_id
GROUP BY savings.customer_id
HAVING count(*)=1);
/* 6.e */
SELECT customer_name
FROM loan,customer
WHERE loan.customer_id=customer.customer_id AND loan.customer_id NOT IN (SELECT customer_id FROM savings)
GROUP BY loan.customer_id
HAVING COUNT(*)>1;
/* 6.f total number of customers */
SELECT DISTINCT COUNT(*) as total_customers,branch_name
FROM details
GROUP BY branch_id;
/* 6.f customers with loan only */
SELECT COUNT(*) as loan_only_customers,branch_name
FROM details
WHERE customer_id NOT IN (SELECT customer_id FROM savings)
GROUP BY branch_id;
/* 6.f customers with savings only */
SELECT COUNT(*) as savings_only_customers,branch_name
FROM details
WHERE customer_id NOT IN (SELECT customer_id FROM loan)
GROUP BY branch_id;
/*6.f customers with loan and savings*/
SELECT COUNT(*) as both_loan_and_savings,branch_name
FROM details
WHERE customer_id IN (SELECT savings.customer_id FROM savings,loan
WHERE savings.customer_id=loan.customer_id
GROUP BY savings.customer_id
HAVING count(*)=1)
GROUP BY branch_id;
/*6.g*/
SELECT branch_name,branch_city, MAX(balance) as max_loan
FROM details
WHERE balance = (SELECT max(balance) FROM loan);
/* 6.h */
SELECT branch_id,branch_name as branch_that_not_issued_any_loan,branch_city
FROM details
WHERE branch_id NOT IN (SELECT branch_id FROM loan)
GROUP BY branch_id;
/*6.i*/
SELECT *
FROM details
ORDER BY customer_id;
drop table branch;
drop table customer;
drop table savings;
drop table loan;
drop database expt6;