-
Notifications
You must be signed in to change notification settings - Fork 0
/
employee.sql
315 lines (219 loc) · 10.7 KB
/
employee.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
-- DQL(Data Query Language) -> select ,
-- DML(Data Manipulation Language) -> insert, update, delete,
-- DDL(Data Definition Language) -> create, alter, drop, truncate, rename (for schema change)
-- DCL(Data Control Language) -> Grant, Revoke
-- TCL(Transaction Control Language) -> Commit, Rollbqck,
-- show databases on system or server
show databases;
-- select db
use sql_practice;
-- create table
create table if not exists employee (
id int primary key,
name varchar(50),
age int,
department varchar(50),
city varchar(50),
salary int
);
-- insert into table
insert into employee (id,name,age,department,city,salary) values
(1,"Rahul",25,"IT","Mumbai",1500),
(2,"Ram",26,"HR","Pune",2000),
(3,"Abhimanyu",27,"IT","Mumbai",2500),
(4,"Aditya",25,"Marketing","Surat",2400),
(5,"Raj",24,"Finance","Indore",1500);
-- see all data in table
select * from employee;
-- update column
update employee set salary=1600 where salary=1500;
-- delete records (rows) from table
delete from employee where name="Raj";
-- where clause
select * from employee where age>25;
select id, name, age, city, department, salary from employee where age>25;
-- alter command
-- to add more column, to chenge column name, etc to modify schema
-- add column
alter table employee add dob varchar(20) default "np";
-- drop column
alter table employee drop column dob;
-- modify data type of column
-- modify clause
alter table employee modify age varchar(3);
-- change name of column and also the datatype
-- change command
alter table employee change age emp_age varchar(3);
alter table employee change emp_age age varchar(3);
-- rename command (to rename any table, column, index, constraint name) (DDL)
alter table employee rename column emp_age to age;
-- rename table
rename table employee to employees;
rename table employees to employee;
-- truncate (remove all rows but preserve structure)
create table emp (rollno int primary key);
desc emp;
insert into emp (rollno) values (1),(2);
select * from emp;
truncate table emp;
select * from emp;
desc emp;
-- diff between truncate delete drop
-- remove all rows from table | use to remove specific rows from table based on cond'n | used to completely remove the table along with schema
-- truncate table tablename; | delete from tablename where cond'n; | drop table tablename;
-- foreign key
create table if not exists parent (rollno int primary key, name varchar(50) not null);
create table if not exists child (c_id int primary key , c_name varchar(20) not null, rollno int, foreign key (rollno) references parent(rollno));
desc child;
-- distinct (to query non duplicate data)
select city,age from employee;
select distinct city from employee;
select distinct city,age from employee; -- to use it combinedly
-- operators in sql
-- arithmetic opr -> +, -, *, /, %
-- comparision opr -> =, <> or !=, >, <, >=, <=
-- logical opr -> and, or, not
-- in -> checks if a value is in list of values
-- is null / is not null
-- bitwise opr -> and(&), or(|)
-- and opr
select * from employee where age>=20 and department="IT";
-- or opr
select * from employee where age>=20 or department="IT";
-- in opr
select * from employee where department in ("IT","HR");
-- not opr
select * from employee where department not in ("IT","HR");
-- like & wildcard opr
select * from employee where name like 'R%';
select * from employee where name like 'R___%';
select * from employee where name like '_A%';
-- between opr
select * from employee where salary between 1500 and 2000;
-- clauses in sql -> tools/conditions that help us to make queries more specific -> where, group by, having, order by, limit
-- where clause
select * from employee where age > 25;
-- limit clause -> used to restrict the number of rows returned by query
select * from employee limit 3;
-- order by -> to make result sorted in a particular order as ASC or DESC by default asc
select * from employee;
select * from employee order by age;
select * from employee order by salary desc;
-- Q1 -> fetch emp with id 1
select * from employee where id = 1;
-- Q2 -> fetch emp with id 1 and city mumbai
select * from employee where id = 1 and city = "mumbai";
-- Q3 -> fetch emp with salary > 1200 and city mumbai
select * from employee where salary > 1200 and city = "mumbai";
-- Q4 -> fetch emp who are not from mumbai;
select * from employee where city != "mumbai";
-- Q5 -> fetch emp with salry in desc order
select * from employee order by salary desc;
-- Q6 -> fetch 2 emp with maximum salary
select * from employee order by salary desc limit 2;
-- Aggregate functions -> collated or collection -> perform opr on set of rows and return single value . used with select statements -> count(), sum(), avg(), min(), max(), group_concat()
-- count() -> count number of rows in table or no of non null values
select count(name) from employee; -- gives total no of emp in company
-- sum() -> sum of all the values in numeric column
select sum(salary) from employee; -- gives total amount company is paying
-- avg() -> avg of all the values in numeric column
select avg(salary) from employee; -- gives avg amount company is paying
-- min() -> to find minimum number in column
select min(salary) from employee; -- gives minimum salary company paying
-- max() -> to find maximum number in column
select max(salary) from employee; -- gives maximum salary company paying
-- Group by clause -> used to group rows having same values together. helps to organize data into groups for opr like total, avg , max, min, etc in the groups
-- group by
select department, avg(salary) as avg_salary from employee group by department; -- avg salary of department
select department, max(salary) as max_salary from employee group by department; -- max salary of department
select department, count(name) as total_emp from employee group by department; -- total employees in department
-- having clause -> used to apply some condition after making group. similar to where clause but only difference is that it works on aggregated data. helps to filter groups based on cond'n
-- having clause
select department, avg(salary) as avg_salary from employee group by department having avg_salary > 2000;
-- diff between where clause and having clause
-- where clause
-- used to filter rows based on condition before aggregation
-- used with select , update, delete
-- having clause
-- used to filter rows based on condition after aggregation
-- used with group by and aggregate functions
-- aggregation -> get a single value summarizing data by doing some manipulation on collection or collated data
-- Q1 -> fetch total emp based on city
select city, count(name) as total_emp from employee group by city;
-- Q2 -> fetch max salary of emp based on city in desc order
select city, max(salary) as max_salary from employee group by city order by max_salary desc;
-- Q3 -> display department name alongside total count of emp in each department , sorting the results by total no of emp in desc
select department, count(name) as total_emp from employee group by department order by total_emp desc;
-- Q4 -> fetch list of department where average salary > 1200 and display department name and avg sal
select department, avg(salary) as avg_salary from employee group by department having avg_salary > 1200;
-- general order/rank of sql commands
-- select
-- from
-- where
-- group by
-- having
-- order by
-- limit
-- sub queries / nested queries
-- Q1 -> fetch second highest salary of employee
select salary from employee order by salary desc;
select max(salary) as max_salary from employee where salary != (select max(salary) from employee);
-- Q2 -> find nth highest salary (with subquery it is complex).
-- So, we will do it using offset n-1 and limit 1
-- for first highest
select salary from employee order by salary desc limit 1;
-- for second highest
select salary from employee order by salary desc limit 1 offset 1;
-- for third highest
select salary from employee order by salary desc limit 1 offset 2;
-- Joins -> used to combine rows from two or more tables on a related, common, shared, column between them
-- types -> 4 types including -> inner join, Outer join (left join, right join, full join), self join, cross join
-- Q1 -> is foreign key necessary for performing joins
-- Ans -> Joins can be performed based on any columns that establish relationship between tables , not just FK. So, its not necessary.
-- Inner join -> Contains common rows from both tables
-- left join / left outer join -> contains all rows from left table along with common rows from right table and return null for the columns of right table if no matching record in right table
-- right join / right outer join -> contains all rows from right table along with common rows from left table and return null for the columns of left table if no matching record in left table
-- full join / full outer join -> contains all rows from both tables and fills null for left table values not present in right table and vice versa
-- self join -> join on same table
-- cross join -> to perform cartesian product of two tables
-- joins
create table if not exists customer (id int, customer_name varchar(20) not null);
create table if not exists orders (id int primary key, order_name varchar(20) not null);
insert into customer(id, customer_name) values (101, "Ram"),(102, "Rahul"),(103,"Riti");
insert into orders(id, order_name) values (102, "Fruit"),(103, "Ball"),(104,"Basket");
select * from customer;
select * from orders;
-- inner join
select * from customer inner join orders on customer.id = orders.id;
-- left join
select * from customer left join orders on customer.id = orders.id;
-- right join
select * from customer right join orders on customer.id = orders.id;
-- full join -> mysql does not dupport full join keyword directly but postgresql and sql server support so we can use here left join , riht join and union
-- union -> without duplicates rows of first table and second table
select * from customer union select * from orders;
-- full join
select * from customer left join orders on customer.id = orders.id
union
select * from customer right join orders on customer.id = orders.id;
-- cross join
select * from customer cross join orders;
-- self join
select * from customer self join customer;
-- stored procedure
create procedure getAllOrders()
begin
select * from orders;
end;
call getAllOrders();
-- parameterized procedure (in means its an input parameter) -> not working
create procedure getOrderById(in id int)
begin
select * from orders where id=id;
end;
call getOrderById(102);
-- views -> virtual table in sql
-- helps in providing filtered view of data for security purposes
-- provides abstraction and security
create view employee_details_view as select * from employee;
select * from employee_details_view;