-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst (2).sql
661 lines (426 loc) · 15.7 KB
/
first (2).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
show databases;
create database institute;
use institute;
drop database institute;
create table Students(rollno int,name char(50),address varchar(255),fee float);
show tables;
describe students;
insert into students values(22,"Aman","mega 62 noida",120.566);
insert into students values(23,"Sumit","mega 62 noida",120.566);
insert into students values(24,"Aman","mega 62 noida",120.566);
insert into students values (1,"abhishek","gzb",500.667),
(2,"aashish","gzb",50870.667),
(3,"rahul","gzb",5400.667),
(4,"arun","gzb",50980.667),
(5,"nikhil","gzb",55400.667);
select * from students;
drop table students;
-- 01/03/2023
create database megainstitute;
use megainstitute;
create table Employee(empid int,name varchar(255),
address varchar(255),salary double,mobileno bigint,gender char(1),
ismarried boolean);
desc employee;
insert into Employee values (100,"Aman Tiwari",'niit 62 noida',90000.5555,
9891062743,'M',false);
select * from employee;
insert into Employee values
(101,"Aman1",'621 noida',9010.55,9891062741,'M',false),
(102,"RAman2",'622 noida',9020.55,9891062742,'F',true),
(103,"Aman3",'623 noida',9030.55,9891062743,'M',false),
(104,"RAman4",'624 noida',9040.55,9891062744,'f',1),
(105,"Aman5",'625 noida',9050.55,9891062745,'M',0);
select empid,name,isMarried from employee;
insert into Employee(empid,name,salary,gender,ismarried) values
(200,"RajDeep",90000.5555,'M',false);
truncate employee;
drop table employee;
drop database megainstitute;
delete from employee where name='aman1';
delete from employee where name='raman1';
delete from employee where name='raman2';
select * from employee;
update employee set name='kajal' where name='raman4';
-- 03/03/2023
-- int -> integer
use megainstitute;
create table Batch(batchid int,stuname varchar(50),DOB date,
Fee decimal(8,2),description text,address varchar(255),
gender char(1));
desc batch;
insert into batch values (100,"kamal","1999/04/22",56.788,
"good in java","noida","M");
insert into batch values (100,"kamal","1999/04/22",56.789788,
"good in java","noida","M");
insert into batch values (100,"kamal","1999/04/22",12345666.78,
"good in java","noida","M");
select * from batch;
update batch set stuname ='rajveer' where stuname='kamal';
update batch set stuname ='rajdeep' where fee=56.79;
drop table batch;
create table Batch(batchid int,stuname varchar(50),
joining_date timestamp,logintime time,logouttime time);
insert into batch values(1001,"rahul","1999/04/22 10:12","11:30","7:30");
insert into batch values(1002,"Aman","2018/12/02 11:30","11:30","6:30");
insert into batch values(1002,"Aman","2018/12/02 11:30:20","15:30:12","20:30:14");
select * from batch;
-- 04-03-2023
use classicmodels;
show tables;
select * from customers;
select * from customers where city='singapore';
select customerName from customers where city='singapore';
select customername,creditLimit from customers where creditlimit>80000;
-- 8448179216
use megainstitute;
use classicmodels;
show tables;
select * from customers;
-- operators
select 56+35 as 'sum of 56 and 35 is ';
select 27%4 as 'remainder';
select customerName,creditlimit from customers where creditlimit<80000;
select customerName,creditlimit from customers where creditlimit>80000;
select customerName,creditlimit from customers where creditlimit=53100;
select customerName,creditlimit from customers where creditlimit<=80000;
select customerName,creditlimit from customers where creditlimit>=80000;
select customerName,creditlimit from customers where creditlimit<>53100;
select customerName,creditlimit from customers where not creditlimit=53100;
select customerName,creditlimit,country from customers where creditlimit>80000
and country='USA';
select customerName,creditlimit,country from customers where creditlimit>80000
or country='USA';
select contactfirstname,customerName,creditlimit
from customers
where contactfirstname like 'J%';
select contactfirstname,customerName,creditlimit
from customers
where contactfirstname like '%e';
select contactfirstname,customerName,creditlimit
from customers
where contactfirstname like '%e%';
select contactfirstname,customerName,creditlimit
from customers
where contactfirstname like 'J%' and creditlimit>80000 ;
select contactfirstname from customers order by contactfirstname desc;
select contactfirstname,country from customers order by contactfirstname,country;
-- 13/03/2023
-- use classicmodels;
/*
use classicmodels;
select * from customers;
select * from customers;
select * from customers;
select * from customers;
*/
-- keys
use megainstitute;
show tables;
create table Teachers(tid int,tname varchar(25),address varchar(50));
insert into Teachers values(100,"Aman","niit 62 noida"),
(100,"Aman1","niit 62 noida"),
(100,"kamal","niit 62 noida"),
(100,"ritu","niit 62 noida");
select * from teachers;
update teachers set tname="Rahul" where tid=100;
drop table teachers;
create table Teachers(tid int unique,tname varchar(25),address varchar(50));
insert into Teachers values(100,"Aman","niit 62 noida");
insert into Teachers values(100,"Aman","niit 62 noida");
select * from teachers;
insert into Teachers(tname,address) values("kamal","niit 62 noida");
insert into Teachers values(null,"Ritu","niit 62 noida");
drop table teachers;
create table Teachers(tid int not null unique,tname varchar(25),address varchar(50));
insert into Teachers values(100,"Aman","niit 62 noida");
insert into Teachers values(100,"Aman","niit 62 noida");
insert into Teachers(tname,address) values("Aman","niit 62 noida");
insert into Teachers(tid,address) values(102,"niit 62 noida");
select * from teachers;
describe teachers;
create table Teachers(tid int not null unique,tname varchar(25),
address varchar(50),country varchar(50) default 'india');
insert into teachers values(201,"aman","niit 62 noida","Australia");
insert into teachers(tid,tname,address) values(202,"aman","niit 62 noida");
select * from teachers;
create table Teachers(tid int not null unique auto_increment,tname varchar(25),
address varchar(50),country varchar(50) default 'india');
insert into teachers(tid,tname,address) values(201,"aman","niit 62 noida");
insert into teachers(tname,address) values("rahul","niit 62 noida");
insert into teachers(tname,address) values("sumit","niit 62 noida");
insert into teachers(tname,address) values("kajal","niit 62 noida");
drop table teachers;
create table Teachers(tid int not null unique check (tid>0),
tname varchar(25),address varchar(50),country varchar(50) default 'india');
insert into teachers(tid,tname,address) values(-18,"aman","niit 62 noida");
insert into teachers(tid,tname,address) values(18,"aman","niit 62 noida");
create table Teachers(tid int primary key auto_increment,
tname varchar(25),address varchar(50),country varchar(50) default 'india');
insert into teachers(tid,tname,address) values(null,"aman","niit 62 noida");
select * from teachers;
-- 14/03/2023
use megainstitute;
create table userdata(uname varchar(255),password varchar(255),
country varchar(100) default 'india');
insert into userdata(uname,password) values('sachinSingh',"password");
insert into userdata values('sachinSingh',"password","USA");
select * from userdata;
-- keys
-- primary key
-- drop table userdata;
create table Users(uname varchar(255) primary key,
password varchar(255),email varchar(255) unique not null,
mobileno varchar(15) unique not null,
country varchar(100) default 'india');
-- primary key = unique not null
-- there must be one primary key in a table
insert into users values
("sachin123","password","[email protected]","988988899","Australia"),
("Aman123","2235","[email protected]","9891062743","USA"),
("sumit","ilysmkatrina","[email protected]","6756768","Canada"),
("neha","albatross","[email protected]","3454565","France");
update users set country="Turkey" where uname="neha";
select * from users;
-- 15/03/2023
use megainstitute;
create table categories(category_id int primary key,cat_name varchar(250)
,description varchar(250));
desc categories;
create table products(category_id int,product_id int primary key,
product_name varchar(255),product_description varchar(245),
foreign key(category_id) references categories(category_id)
);
desc products;
insert into categories values(100,"Mobiles","new model mobile phones");
select * from categories;
insert into products values(100,200,"Realme xt","best phone under 20k");
select * from products;
insert into products values
(103,201,"Shirts","best shirt under 2k");
drop table categories; -- error
drop table products;
drop table categories;
select * from products;
-- 17-03-2023
use megainstitute;
create table categories(category_id int primary key,cat_name varchar(250)
,description varchar(250));
create table products(category_id int,product_id int primary key,
product_name varchar(255),product_description varchar(245),
foreign key(category_id) references categories(category_id)
);
insert into categories values (101,"phones","any description"),
(102,"laptop","any description"),
(103,"fashion","any description"),
(104,"food","any description"),
(105,"guns","any description");
insert into products values (101,201,"realme xt ","hello"),
(101,202,"redmi note 9 ","hello"),
(102,203,"hp ","hello"),
(102,204,"dell vestro ","hello"),
(105,205,"M416 ", "hello"),
(null,206,"shirt1 ","hello"),
(null,207,"jeans ","hello");
-- 20/3/2023
-- composite primary key
use megainstitute;
create table StudentsData(fname varchar(255),lname varchar(255),
fathersName varchar(255),address varchar(255),dob date,
primary key(fname,lname)
);
insert into studentsdata(fname,lname,address) values
("Aashish","Beniwal","noida 62"),
("Aashish","Sharma","delhi 62"),
("Sumit","Sharma","delhi 62"),
("Sumit","Beniwal","delhi 62");
-- insert into studentsdata(fname,lname,address) values
-- ("Aashish","Beniwal","noida 62"); -- error
-- Alter commands
create table Emp(empid int);
desc Emp;
alter table emp add column EmpName varchar(255);
desc Emp;
select * from emp;
alter table emp add column address varchar(255);
alter table emp add column dob date,xyz int; -- error
alter table emp drop column address;
alter table emp drop column empid,empname; -- error
alter table emp modify column empid int primary key ;
desc emp;
alter table emp modify column empid int primary key auto_increment;
alter table emp rename to Empl;
desc empl;
-- 21-03-2023
use megainstitute;
select * from empl;
alter table empl rename column empName to Employee_Name;
set foreign_key_checks = 0;
drop table categories;
drop table products;
set foreign_key_checks = 1;
desc products;
alter table products drop primary key;
-- between,insert into select,distinct,limit,constraints,f.k cascade,delete
use classicmodels;
select * from customers;
select customername,creditlimit
from customers
where creditlimit between 50000 and 80000
order by creditlimit
limit 10;
select customername,creditlimit
from customers
where creditlimit between 50000 and 80000
order by creditlimit
limit 10 offset 20;
select * from orders;
select distinct comments from orders;
select * from orders where shippeddate is null;
use megainstitute;
select * from empl;
select * from classicmodels.customers;
select contactfirstname,customernumber from classicmodels.customers;
insert into empl select contactfirstname,customernumber
from classicmodels.customers;
-- test 24-03-2023
create table employees(empid int primary key,empname varchar(255),
mobileno varchar(20),salary double);
create table manager(empid int,managerid int,
foreign key(empid) references employees(empid) );
create table departments(deptid int primary key,deptname varchar(255));
create table salesitems(deptid int,name varchar(255),supplierid int,weight float,
color varchar(255), QOh int,foreign key(deptid) references
departments(deptid));
insert into departments(deptid) values(49);
create table supplier(supplierid int primary key,supplies varchar(255),cityname varchar(255),
foreign key(cityname) references cities(cityname));
create table cities(cityname varchar(255) primary key);
-- Ans.1 select * from employees;
-- Ans.2 select deptname from departments;
-- Ans.3 select * from salesitem where Qoh=0;
-- Ans.4 select empname,salary from employees where salary between 10000 and 20000;
-- Ans.5 select name,price,price*0.1 from salesitems where deptid=49;
-- Ans.6 select empname,mobileno,salary from employees where empname like "s%";
-- joins
use megainstitute;
create table categories(category_id int primary key,cat_name varchar(250)
,description varchar(250));
create table products(category_id int,product_id int primary key,
product_name varchar(255),product_description varchar(245),
foreign key(category_id) references categories(category_id)
);
insert into categories values (101,"phones","any description"),
(102,"laptop","any description"),
(103,"fashion","any description"),
(104,"food","any description"),
(105,"guns","any description");
insert into products values (101,201,"realme xt ","hello"),
(101,202,"redmi note 9 ","hello"),
(102,203,"hp ","hello"),
(102,204,"dell vestro ","hello"),
(105,205,"M416 ", "hello"),
(null,206,"shirt1 ","hello"),
(null,207,"jeans ","hello");
select * from categories;
select * from products;
desc categories;
desc products;
use megainstitute;
select * from categories,products;
select * from categories
inner join products
on categories.category_id = products.category_id;
select categories.category_id,cat_name,product_id,product_name
from categories
inner join products
on categories.category_id = products.category_id;
select * from categories as C
inner join products as P
on C.category_id = P.category_id;
select * from categories C
inner join products P
on C.category_id = P.category_id;
select * from categories
inner join products
using (category_id); -- the column name and data type of that column must be same
-- left join
select * from Categories c
left join products P
on c.category_id=P.category_id;
-- Right join
select * from Categories c
right join products P
on c.category_id=P.category_id;
-- only left
select * from Categories c
left join products P
on c.category_id=P.category_id
where P.category_id is null;
-- only right
select * from Categories c
right join products P
on c.category_id=P.category_id
where c.category_id is null;
-- full outer join
select * from Categories c
left join products p
on c.category_id=p.category_id
union
select * from categories c
right join products p
on c.category_id=p.category_id;
select * from Categories c
left join products p
on c.category_id=p.category_id
union all
select * from categories c
right join products p
on c.category_id=p.category_id;
-- subquery
use classicmodels;
show tables;
select * from customers;
select customername,creditlimit
from customers
where customername='Alpha Cognac';
select customername,creditlimit
from customers where creditlimit>=61100;
-- or
select customername,creditlimit
from customers
where creditlimit>=(select creditlimit
from customers
where customername='alpha cognac')
order by customername;
select count(*)
from customers
where creditlimit>=(select creditlimit
from customers
where customername='alpha cognac')
order by customername;
-- group by
select count(*) from customers;
select sum(creditlimit) from customers;
select avg(creditlimit) from customers;
select min(creditlimit) from customers;
select max(creditlimit) from customers;
select city,count(*)
from customers
group by city order by city;
select * from customers order by city;
select city,count(*) as 'total persons in city',sum(creditlimit)
from customers
group by city order by city;
-- group by
use classicmodels;
select * from customers;
select state,country,count(*),sum(creditlimit)
from customers
group by state,country
order by state,country;
-- having
select country,count(*)
from customers
group by country
order by country;