-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL_Class_2.txt
277 lines (168 loc) · 6.27 KB
/
SQL_Class_2.txt
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
Create database class2_db;
use class2_db;
create table if not exists employee(
id int,
name VARCHAR(50),
address VARCHAR(50),
city VARCHAR(50)
);
insert into employee values(1, 'Shashank', 'RJPM', 'Lucknow');
select * from employee;
--- add new column named DOB in the TABLE
alter table employee add DOB date;
select * from employee;
--- modify existing column in a TABLE or change datatype of name column or increase lenght of name column
alter table employee modify column name varchar(100);
--- delete existing column from given TABLE or remove city column from employee table
alter table employee drop column city;
select * from employee;
--- rename the column name to full_name
alter table employee rename column name to full_name;
create table if not exists employee(
id int,
name VARCHAR(50),
age int,
hiring_date date,
salary int,
city varchar(50)
);
insert into employee values(1,'Shashank', 24, '2021-08-10', 10000, 'Lucknow');
insert into employee values(2,'Rahul', 25, '2021-08-10', 20000, 'Khajuraho');
insert into employee values(3,'Sunny', 22, '2021-08-11', 11000, 'Banaglore');
insert into employee values(5,'Amit', 25, '2021-08-11', 12000, 'Noida');
insert into employee values(6,'Puneet', 26, '2021-08-12', 50000, 'Gurgaon');
--- add unique integrity constraint on id COLUMN
alter table employee add constraint id_unique UNIQUE(id);
insert into employee values(1,'XYZ', 25, '2021-08-10', 50000, 'Gurgaon');
--- drop constraint from existing TABLE
alter table employee drop constraint id_unique;
insert into employee values(1,'XYZ', 25, '2021-08-10', 50000, 'Gurgaon');
--- create table with Primary_Key
Create table persons
(
id int,
name varchar(50),
age int,
---Primary Key (id)
constraint pk Primary Key (id)
);
insert into persons values(1,'Shashank',29);
--- Try to insert duplicate value for primary key COLUMN
insert into persons values(1,'Rahul',28);
--- Try to insert null value for primary key COLUMN
insert into persons values(null,'Rahul',28);
--- To check difference between Primary Key and Unique
alter table persons add constraint age_unq UNIQUE(age);
select * from persons;
insert into persons values(2,'Rahul',28);
insert into persons values(3,'Amit',28);
insert into persons values(3,'Amit',null);
select * from persons;
insert into persons values(4,'Charan',null);
insert into persons values(5,'Deepak',null);
--- create tables for Foreign Key demo
create table customer
(
cust_id int,
name VARCHAR(50),
age int,
constraint pk Primary Key (cust_id)
);
create table orders
(
order_id int,
order_num int,
customer_id int,
constraint pk Primary Key (order_id),
constraint fk Foreign Key (customer_id) REFERENCES customer(cust_id)
);
--- Differen between Drop & Truncate Command
select * from persons;
truncate table persons;
select * from persons;
drop table persons;
--- Operations with Select Command
select * from employee;
drop table employee;
create table if not exists employee(
id int,
name VARCHAR(50),
age int,
hiring_date date,
salary int,
city varchar(50)
);
insert into employee values(1,'Shashank', 24, '2021-08-10', 10000, 'Lucknow');
insert into employee values(2,'Rahul', 25, '2021-08-10', 20000, 'Khajuraho');
insert into employee values(3,'Sunny', 22, '2021-08-11', 11000, 'Bangalore');
insert into employee values(5,'Amit', 25, '2021-08-11', 12000, 'Noida');
insert into employee values(1,'Puneet', 26, '2021-08-12', 50000, 'Gurgaon');
select * from employee;
--- how to count total records
select count(*) from employee;
--- alias declaration
select count(*) as total_row_count from employee;
--- display all columns in the final result
select * from employee;
--- display specific columns in the final result
select name, salary from employee;
--- aliases for mutiple columns
select name as employee_name, salary as employee_salary from employee;
select * from employee;
--- print unique hiring_dates from the employee table when employees joined it
select Distinct(hiring_date) as distinct_hiring_dates from employee;
select * from employee;
--- How many unique age values in the table??
select count(distinct(age)) as total_unique_ages from employee;
--- Increment salary of each employee by 20% and display final result with new salary
SELECT id,
name,
salary as old_salary,
(salary + salary * 0.2) as new_salary
FROM employee;
-- Syntax for update command
select * from employee;
--- Upadtes will be made for all rows
UPDATE employee SET age = 20;
select * from employee;
--- update the salary of employee after giving 20% increment
UPDATE employee SET salary = salary + salary * 0.2;
select * from employee;
--- How to filter data using WHERE Clauses
select * from employee where hiring_date = '2021-08-10';
select * from employee;
--- Update the salary of employees who joined the company on 2021-08-10 to 80000
update employee SET salary = 80000 where hiring_date = '2021-08-10';
select * from employee;
--- how to delete specific records from table using delete command
--- delete records of those employess who joined company on 2021-08-10
delete from employee where hiring_date = '2021-08-10';
select * from employee;
--- How to apply auto increment
create table auto_inc_exmp
(
id int auto_increment,
name varchar(20),
primary key (id)
);
insert into auto_inc_exmp(name) values('Shashank');
insert into auto_inc_exmp(name) values('Rahul');
select * from auto_inc_exmp;
--- Use of limit
select * from employee;
select * from employee limit 2;
# sorting data in mysql by using 'Order By'
select * from employee;
# arrage data in ascending order
select * from employee order by name;
# arrage data in descending order
select * from employee order by name desc;
# display employee data in desc order of salary and if salaries are same for more than one employees
# arrange their data in ascedinding order of name
select * from employee order by salary desc, name asc;
# when we ignore multilevel ordering
select * from employee order by salary desc;
# Write a query to find the employee who is getting maximum salary?
select * from employee order by salary desc limit 1;
# Write a query to find the employee who is getting minium salary?
select * from employee order by salary limit 1;