-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLQuery2Dec29.sql
260 lines (163 loc) · 8 KB
/
SQLQuery2Dec29.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
--in operator is used to select the multiple match values
---not in ---records in those are not match
create table Department(deptId int primary key,
deptName varchar(40))
create table Employe(empId int primary key,empName varchar(20) not null,empemail varchar(40) unique,age int check(age>18),
country varchar(20) default'India',deptId int foreign key references Department(deptId))
alter table Employe add constraint fk_empdept foreign key (depId) references dept(deptId)
select * from Department
select * from Employe
insert into Department values(0,'Management')
insert into Department values(1,'HR')
insert into Department values(2,'Test')
insert into Department values(3,'Sales')
insert into Department values(4,'Production')
insert into Department values(5,'Administration/operations')
insert into Department values(6,'Research and development')
insert into Department values(7,'Marketing and sales')
insert into Employe (empId,empname,empemail,age,country,deptid)values(1,'Manisha','[email protected]',21,'India',0)
insert into Employe (empId,empname,empemail,age,country,deptid)values(2,'Rajvi','[email protected]',23,'Germany',1)
insert into Employe (empId,empname,empemail,age,country,deptid) values(3,'Raj','[email protected]',26,'US',2)
insert into Employe (empId,empname,empemail,age,country,deptid) values(4,'rajshreee','[email protected]',24,'UK',3)
insert into Employe (empId,empname,empemail,age,country,deptid) values(5,'Revati','[email protected]',25,'US',4)
insert into Employe (empId,empname,empemail,age,country,deptid) values(6,'Kiran','[email protected]',27,'Thailand',5)
insert into Employe (empId,empname,empemail,age,country,deptid) values(7,'Manav','[email protected]',23,'Cannada',6)
insert into Employe (empId,empname,empemail,age,country,deptid) values(8,'Jeet','[email protected]',27,'Germany',452)
insert into Employe (empId,empname,empemail,age,country,deptid) values(9,'Majiri','[email protected]',18,'India',555)
insert into Employe (empId,empname,empemail,age,country,deptid) values(10,'amruta','[email protected]',23,'Nigera',654)
insert into Employe (empId,empname,empemail,age,country,deptid) values(11,'saurabh','[email protected]',23,'Japan',954)
insert into Employe (empId,empname,empemail,age,country,deptid) values(12,'Danisha','[email protected]',27,'Chaina',748)
insert into Employe (empId,empname,empemail,age,country,deptid) values(13,'Ruchaa','[email protected]',25,'London',954)
insert into Employe (empId,empname,empemail,age,country,deptid) values(14,'Aryaa','[email protected]',22,'Manmar',101)
insert into Employe (empId,empname,empemail,age,country,deptid) values(15,'Vinay','[email protected]',25,'Indonesia',105)
insert into Employe (empId,empname,empemail,age,country,deptid) values(16,'Nehaa','[email protected]',28,'Australia',201)
insert into Employe (empId,empname,empemail,age,country,deptid)values(17,'Ayushi','[email protected]',18,'Africa',222)
insert into Employe (empId,empname,empemail,age,country,deptid)values(18,'Purvi','[email protected]',26,'India',256)
alter table Employe add Salary decimal
update Employe set Salary=220000,age =25 where empId=4
update Employe set Salary=200000,age =21 where empId=1
update Employe set Salary=210000,age =23 where empId=2
update Employe set Salary=230000,age =26 where empId=3
--------------4JAN
-- if column contains null value we can not compare using operator
-- is null
-- is not null
update Employe set country=null where empid=8
select * from Employe
select * from Employe where country is not null
select * from Employe where country is null
--order by
-- Sort the records either in asending or desending order
select * from Employe order by empname -- by default in asending order
select * from Employe order by empname desc
select * from Employe order by Salary desc
select * from Employe order by age,empname
--offset --> you want to skip any perticular records
-- fetch --> is used to select the perticular records after the offset
select * from Employe
order by empname
offset 3 rows
fetch next 3 rows only
-- display emp whose has 2nd, 3rd & 4th hightest salary
select * from Employe
order by salary desc
offset 1 rows
fetch next 3 rows only
------------OFFSET can be used with select & order by clause
------Fetch can be used with offset.
--------------We can not set offset as negative
-- inner join
select e.*,d.deptname
from Employe e
inner join Department d on d.deptid=e.deptid
select e.empname as 'Employee Name',e.empemail as 'Email Id' , d.deptname as 'Dept Name'
from Employe e
inner join Department d on d.deptid=e.deptid
select e.empname ,e.empemail , d.deptname
from Employe e
inner join Department d on d.deptid=e.deptid
select Employe.empname ,Employe.empemail , Department.deptname
from Employe
inner join Department on Department.deptid=Employe.deptid
-- inner join
select e.*, d.deptName from Employe e
inner join Department d on d.deptId=e.deptid
-- left join
-- display all records from the left table & match records from another table
-- when there is no match record right table returns null
select e.*, d.deptName from Employe e
left join Department d on d.deptid=e.deptId
--right join
select e.*, d.deptName from Employe e
right join Department d on d.deptId=e.deptId
--full join
select e.*, d.deptName from Employe e
full join Department d on d.deptid=e.deptId
select * from Employe
insert into Employe values(11,'Rajesh','[email protected]',33,'India',null,34000)
insert into Employe values(12,'Ajay','[email protected]',23,'India',null,34000)
--customer --> orders
-- inner join --> match data e.g. show those customer list who purchased the product
-- left join --> all customer list & their ordered products
-- right join --> all records from orders tables & match records from customer table
-- full join --> all records from both the table (left + right combination)
select * from Employe
alter table employe add Managerid int
update Employe set Managerid=1 where empId in (2,3,4)
update Employe set Managerid=5 where empId in(6,7,8)
select emp.empName as 'Employe' man.empName as 'Manager'
from Employe emp,Employe man
where man.empid=emp.managerid
-------Group BY
select* from Employe
select* from Department
select deptId, count(empId) as ' total employe' from Employe
group by deptId
select country,(count(empId))as total employe from Employe
order by totalemp
---count the emp in each dept
--count the emp which is in development dept
--count the emp in each dept
select d.deptName,count(e.empId) as 'emp count' from Employe e
inner join Department d on d.deptId=e.deptId
------where d.Department='HR'
group by d.deptName
order by[No of Employe] desc
-------------Having clause
select country, (count(empId)) as totalemp from Employe e group by country having count (empId)>12
-----list of employe whose salary less than the average salary
select e.empname,avg(e.Salary)
from Employe e
group by e.empName
having e.Salary<avg(e.Salary)
select * from Employe
where Salary<(select avg(Salary) from Employe)
-----------------------------------------------------------
--display count of employee who's from india & usa
-- and sort the list in asending order of count
select country,count(empid) as 'count' from Employe
where country in ('India','usa')
group by country
order by count(empid)
---------Create view
create view
Department_Employecount
as
select d.deptName , count(e.empid) as 'emp count' from Employe e
inner join Department d on d.deptId=e.deptid
group by d.deptname
--modify the existing view
alter view
Department_Employecount
as
select d.deptName , count(e.empid) as 'emp count' from Employe e
inner join Department d on d.deptid=e.deptid
group by d.deptName
select * from Department_Employecount
-- delete the view
drop view Department_Employecount
--create view
Salary_greaterthan3k
as
select * from Employe where Salary > 30000
select * from Salary_greaterthan3k