-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsql-commands.sql
More file actions
123 lines (61 loc) · 3.07 KB
/
sql-commands.sql
File metadata and controls
123 lines (61 loc) · 3.07 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
-- create a database
create database ust_db;
-- switch to the database
use ust_db;
-- create a table
create table trainee(id int auto_increment primary key, trainee_name varchar(50), location varchar(50));
-- insert some records
insert into trainee(trainee_name,location) values ('Sandra John','Kerala');
insert into trainee(trainee_name, location) values ('Shreya Gupta', 'Bangalore'),
('Suhas','Bengaluru'),
('Gururaj','Bengaluru'),
('Rose','Kerala'),
('Vishnu','Kerala'),
('Archit','Bangalore'),
('Ashwini','Bengaluru'),
('Varsha','Bangalore'),
('SUNNY','BANGALORE'),
('Abhishek','Bangalore');
-- retrieve the records
select * from trainee;
-- update the data
update trainee set location='Bengaluru' where location='Bangalore';
update trainee set location ='Kochi' where id =5;
update trainee set location='Kozhikode' where id=6;
update trainee set location ='Kochi' where id=1;
-- truncate records
truncate table trainee;
-- drop the table
drop table trainee;
-- alter a table
-- create a new column gender
alter table trainee add gender char(6);
update trainee set gender = 'F' WHERE id IN(1,2,5,8,9);
update trainee set gender='M' WHERE ID IN (3,4,6,7,10,11);
-- delete some records
delete from trainee where id = 12;
-- retrieveing data conditionally
-- find all the trainees from Bengaluru
select * from trainee where location ='Bengaluru';
-- find all the trainees from Bengaluru who are female
select * from trainee where location='Bengaluru' and gender ='F';
-- Group Functions
-- count the trainees location wise
select location, count(id) as 'total trainees' from trainee group by location;
-- find the location where most trainees belong to
select location, COUNT(*) from trainee group by location order by 2 desc limit 1;
-- create a new table project with columns id, title
create table project(id int auto_increment primary key,title varchar(40));
-- insert some data
insert into project (title) values ('Library Management System');
insert into project(title) values('Hospital Management');
insert into project(title) values('Amazon Clone');
insert into project(title) values('Baking App');
-- add a new column project_id to trainee
Alter table trainee add project_id int;
-- create a frreign key
alter table trainee add constraint fk_project_id foreign key(project_id) references project(id);
update trainee set project_id=1 where id=2;
-- find the project that does not have any trainee
select p.id, title from project p left join trainee t on p.id = t.project_id where t.project_id IS NULL;
select p.id, title from trainee t right join project p on p.id = t.project_id where t.project_id IS NULL;