-
Notifications
You must be signed in to change notification settings - Fork 0
/
join_table.sql
220 lines (141 loc) · 6.33 KB
/
join_table.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
create database mydemo_final;
use mydemo_final;
show tables;
CREATE TABLE dataofcustomer
(
custid VARCHAR(6) PRIMARY KEY,
fname VARCHAR(50),
mname VARCHAR(30),
ltname VARCHAR(30),
city VARCHAR(15),
age int(10),
mobileno VARCHAR(10),
occupation VARCHAR(10),
dob DATE
);
show tables;
INSERT INTO dataofcustomer VALUES('C00001','Ramesh','Chandra','Sharma','Delhi',45,'9543198345','Service','1976-12-06');
INSERT INTO dataofcustomer VALUES('C00002','Avinash','Sunder','Minha','Delhi',32,'9876532109','Service','1974-10-16');
INSERT INTO dataofcustomer VALUES('C00003','Rahul',null,'Rastogi','Delhi',24,'9765178901','Student','1981-09-26');
INSERT INTO dataofcustomer VALUES('C00004','Parul',null,'Gandhi','Delhi',46,'9876532109','Housewife','1976-11-03');
INSERT INTO dataofcustomer VALUES('C00005','Naveen','Chandra','Aedekar','Mumbai',23,'8976523190','Service','1976-09-19');
INSERT INTO dataofcustomer VALUES('C00006','Chitresh',null,'Barwe','Mumbai',18,'7651298321','Student','1992-11-06');
INSERT INTO dataofcustomer VALUES('C00007','Amit','Kumar','Borkar','Mumbai',76,'9875189761','Student','1981-09-06');
INSERT INTO dataofcustomer VALUES('C00008','Nisha',null,'Damle','Mumbai',43,'7954198761','Service','1975-12-03');
INSERT INTO dataofcustomer VALUES('C00009','Abhishek',null,'Dutta','Kolkata',67,'9856198761','Service','1973-05-22');
INSERT INTO dataofcustomer VALUES('C00010','Shankar',null,'Nair','Chennai',90,'8765489076','Service','1976-07-12');
show tables;
select * from dataofcustomer;
select min(age) from dataofcustomer;
select fname from dataofcustomer where age = 18;
select max(age) from dataofcustomer;
select fname from dataofcustomer where age = 90;
select count(*) from dataofcustomer;
select count(age) as total_customer from dataofcustomer;
select count(age) as customer_from_Delhi from dataofcustomer where city ="Delhi";
select avg(age) as average_age from dataofcustomer;
select sum(age) as sum_of_age from dataofcustomer;
select count(fname),city from dataofcustomer group by city;
select count(fname),city from dataofcustomer group by city having count(fname)>2;
select count(fname),city from dataofcustomer group by city having count(fname)=2;
select count(fname),city from dataofcustomer group by city having count(fname)=1;
select count(fname),city from dataofcustomer group by city having count(fname)>2;
select count(fname),city from dataofcustomer group by city having count(fname)<2;
select count(fname),city from dataofcustomer group by city having count(fname)!=4;
CREATE TABLE customer
(
custid VARCHAR(6),
fname VARCHAR(30),
mname VARCHAR(30),
ltname VARCHAR(30),
city VARCHAR(15),
mobileno VARCHAR(10),
occupation VARCHAR(10),
dob DATE,
CONSTRAINT customer_custid_pk PRIMARY KEY(custid)
);
CREATE TABLE account
(
acnumber VARCHAR(6),
custid VARCHAR(6),
bid VARCHAR(6),
opening_balance INT(7),
aod DATE,
atype VARCHAR(10),
astatus VARCHAR(10),
CONSTRAINT account_acnumber_pk PRIMARY KEY(acnumber),
CONSTRAINT account_custid_fk FOREIGN KEY(custid) REFERENCES dataofcustomer(custid)
);
INSERT INTO account VALUES('A00001','C00001','B00001',1000,'2012-12-15','Saving','Active');
INSERT INTO account VALUES('A00002','C00002','B00001',1000,'2012-06-12','Saving','Active');
INSERT INTO account VALUES('A00003','C00003','B00002',1000,'2012-05-17','Saving','Active');
INSERT INTO account VALUES('A00004','C00002','B00005',1000,'2013-01-27','Saving','Active');
INSERT INTO account VALUES('A00005','C00006','B00006',1000,'2012-12-17','Saving','Active');
INSERT INTO account VALUES('A00006','C00007','B00007',1000,'2010-08-12','Saving','Suspended');
INSERT INTO account VALUES('A00007','C00007','B00001',1000,'2012-10-02','Saving','Active');
INSERT INTO account VALUES('A00008','C00001','B00003',1000,'2009-11-09','Saving','Terminated');
INSERT INTO account VALUES('A00009','C00003','B00007',1000,'2008-11-30','Saving','Terminated');
INSERT INTO account VALUES('A00010','C00004','B00002',1000,'2013-03-01','Saving','Active');
show tables;
select * from account;
select count(*), astatus from account group by astatus ;
select count(*), astatus from account group by astatus having count(atype)=7;
select count(*) from account;
select count(*) from customer;
select count(*) from account;
select fname,acnumber from customer inner join account on customer.custid = account.custid;
select count(fname) from customer inner join account on customer.custid = account.custid;
select count(custid) from customer group by city;
select * from dataofcustomer;
select * from dataofcustomer where custid in (select custid from dataofcustomer where age>=45);
CREATE TABLE customer
(
custid VARCHAR(6) PRIMARY KEY,
fname VARCHAR(50),
mname VARCHAR(30),
ltname VARCHAR(30),
city VARCHAR(15),
age int(10),
mobileno VARCHAR(10),
occupation VARCHAR(10),
dob DATE
);
drop table customer;
show tables;
INSERT INTO customer
SELECT * FROM dataofcustomer
WHERE age>=45;
select * from customer;
select * from dataofcustomer;
UPDATE customer
SET age = 47
WHERE age in (select age from dataofcustomer where age=45);
DELETE FROM customer
WHERE AGE IN (SELECT AGE FROM dataofCUSTOMER
WHERE AGE >= 27 );
select * from dataofcustomer;
select * from democustomer;
SELECT * FROM customer
WHERE custid IN (SELECT custid FROM customer
WHERE age>25);
SELECT custid,fname FROM customer
WHERE age>25;
select * from dataofcustomer;
select custid from account where custid = any (select custid from dataofcustomer where city = 'Delhi');
select custid from account where custid = all (select custid from dataofcustomer where city = 'Delhi');
select custid from dataofcustomer where city = 'Delhi';
select * from account;
SELECT custid, fname,age,
CASE
WHEN age > 30 THEN 'The age is greater than 30'
WHEN age = 30 THEN 'The age is 30'
ELSE 'The age is not group of 30'
END AS Age
FROM dataofcustomer;
SELECT fname, city, age
FROM dataofcustomer
ORDER BY
(CASE
WHEN City IS NULL THEN "no city"
ELSE City
END);