-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql employee db querying.sql
262 lines (191 loc) · 7.52 KB
/
sql employee db querying.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
/* Basic querying */
create table EmployeeDemographics
(EmployeeID int, FirstName varchar(50),
LastName varchar(50),
Age int, Gender varchar (50)
);
create table EmployeeSalary
(EmployeeID int,
JobTtitle varchar(50),
Salary int);
insert into Employeedemographics values
(1001, 'Jim', 'Helpert', 18, 'Male'),
(1002, 'Pam', 'Beasley', 18, 'Female'),
(1003, 'Dwight', 'schrute', 19, 'Male'),
(1004, 'Angela', 'Martin', 19, 'Female'),
(1005, 'Toby', 'Flenderson', 20, 'Male'),
(1006, 'Micheal', 'Scott', 24, 'Male'),
(1007, 'Meredith', 'Palmer', 26, 'Female'),
(1008, 'Stanley', 'Hudson', 28, 'Male'),
(1009, 'Kevin', 'Malone', 20, 'Male');
insert into Employeesalary values
(1001, 'Salesman', 15000),
(1002, 'Receptionist', 10000),
(1003, 'Salesman', 18000),
(1004, 'Accountant', 16000),
(1005, 'HR', 14000),
(1006, 'Regional manager', 20000),
(1007, 'Supplier Relation', 10000),
(1008, 'Salesman', 13000),
(1009, 'Accountant', 17000);
/*basic queries*/
select * from employeedemographics;
select * from employeesalary;
select distinct * from employeesalary limit 3;
select count(employeedemographics.Lastname) from employeedemographics;
select Gender, count(Gender) as number from employeedemographics
where age>21
group by Gender
order by number desc;
SELECT
employeedemographics.Gender,
MAX(employeesalary.salary) AS maximumsalary
FROM
employeedemographics
LEFT OUTER JOIN
employeesalary ON employeedemographics.EmployeeID = employeesalary.EmployeeID
GROUP BY employeedemographics.Gender
ORDER BY maximumsalary DESC;
SELECT
employeedemographics.EmployeeID,
employeedemographics.Gender,
AVG(employeesalary.Salary) AS averagesalary
FROM
employeedemographics
LEFT OUTER JOIN
employeesalary ON employeedemographics.EmployeeID = employeesalary.EmployeeID
GROUP BY employeedemographics.Gender
ORDER BY averagesalary DESC;
SELECT * FROM employeedemographics AS emp LEFT OUTER JOIN employeesalary AS sal
ON emp.EmployeeID = sal.EmployeeID
GROUP BY emp.EmployeeID ASC;
SELECT sal.jobtitle, AVG(sal.salary) AS avgsalary FROM employeedemographics as emp
LEFT OUTER JOIN employeesalary AS sal ON emp.EmployeeID = sal.EmployeeID
GROUP BY sal.JobTtitle;
/*intermediate querying*/
insert into employeedemographics values
(1011, 'Ryan', 'Howards', 16, 'Male' ),
(NULL, 'Holly', 'Flax', null, 'Female' ),
(1013, 'darryl', 'Philibin', null, 'Male' );
insert into employeesalary values
(1010, null, 21000),
(null, 'Salesman', 17000);
select * from employeedemographics;
select * from employeesalary;
select * from employeedemographics inner join employeesalary
on employeedemographics.employeeid=employeesalary.employeeid ;
select * from employeedemographics left join employeesalary
on employeedemographics.EmployeeID=employeesalary.EmployeeID;
select * from employeedemographics right join employeesalary
on employeedemographics.EmployeeID=employeesalary.EmployeeID;
select * from employeedemographics full outer join employeesalary
on employeedemographics.EmployeeID=employeesalary.EmployeeID;
/* union by adding a new table*/
insert into employeedemographics values
(1011, 'Ryan', 'Howard', 26, 'Male'),
(null, 'Holy', 'Flax', null, 'null'),
(1013, 'Daryl', 'Philbin', null, 'Male');
create table warehouseemployeedemographics
(employeeid int, firstname varchar(50),
lastname varchar(50), age int, gender varchar(50));
insert into warehouseemployeedemographics values
(1013, 'darryl', 'philbin', null, 'male'),
(1050, 'roy', 'anderson', 20, 'male'),
(1051, 'hidetoshi', 'hasagawa', 22, 'male'),
(1052, 'val', 'johnson', 20, 'female');
select * from employeedemographics;
select * from employeesalary;
select * from warehouseemployeedemographics;
delete from employeedemographics
where employeedemographics.FirstName in ('ryan', 'holly', 'darryl');
select * from employeedemographics;
insert into employeedemographics values
(1011, 'Ryan', 'Howard', 26, 'Male'),
(null, 'Holy', 'Flax', null, 'null'),
(1013, 'Daryl', 'Philbin', null, 'Male');
select * from employeedemographics
union
select * from warehouseemployeedemographics;
select * from employeedemographics full outer join warehouseemployeedemographics
on employeedemographics.EmployeeID=warehouseemployeedemographics.employeeid;
select * from employeedemographics
union all
select * from warehouseemployeedemographics;
select employeedemographics.FirstName, employeedemographics.LastName, employeesalary.Salary, employeesalary.JobTtitle,
case
when salary > 19000 then 'high'
when salary between 17000 and 19000 then 'medium'
when salary between 14000 and 17000 then 'low'
else 'very low'
end as salaryband
from employeedemographics inner join employeesalary
on employeedemographics.EmployeeID=employeesalary.EmployeeID
order by salaryband desc;
select jobtitle, avg(salary) as avgsal from employeedemographics inner join employeesalary
on employeedemographics.EmployeeID = employeesalary.EmployeeID
order by jobtitle;
/* using CTE */
with salary_per_title(title,average_salary)
as
(select jobtitle, avg(salary) as avgssal from employeedemographics inner join employeesalary
on employeedemographics.employeeid=employeesalary.employeeid
group by jobtitle)
select * from salary_per_title
where title in ('hr', 'salesman');
/*using temp table*/
create table salary_per_title (title varchar(50), salary_average int);
insert into salary_per_title
select jobtitle , avg(salary) as avgsal from employeedemographics inner join employeesalary
on employeedemographics.employeeid=employeesalary.employeeid
group by jobtitle;
select title from salary_per_title;
/* string function on error data*/
create table employeeerrors( employeeid varchar(50), firstname varchar(50), lastname varchar(50));
insert into employeeerrors value
('1001 ', 'jimpo', 'helbert'),
(' 1002' , 'pamela', 'beesley'),
('1005', 'tont' , 'flendersob');
-- using trim , ltrim, rtrim
select * from employeeerrors;
select employeeid, trim(employeeid) as idtrim
from employeeerrors;
-- using replace
select lastname, REPLACE(lastname, 'flendersob','flenderson') as last_name_fixed
from employeeerrors ;
select lastname, REPLACE(lastname, 'beesley','beasley') as last_name_fixed
from employeeerrors ;
select lastname, REPLACE(lastname, 'helbert','helpert') as last_name_fixed
from employeeerrors ;
-- using substring
select substring(err.firstname,1,3) = substring(dem.firstname,1,3), substring(err.lastname,1,3),
substring(dem.lastname,1,3) from employeeerrors err
join employeedemographics dem
on substring(err.firstname,1,3) = substring(dem.firstname,1,3)
and substring(err.lastname,1,3) = substring(dem.lastname,1,3);
-- using upper and lower
select firstname, lower(firstname)
from employeeerrors;
select firstname, upper(firstname)
from employeeerrors;
/* using subqueries*/
select employeeid, jobtitle, salary
from employeesalary;
-- subquery in select
select employeeid, salary, (select avg(salary) from employeesalary) as allavgsalary
from employeesalary;
-- partition by
select employeeid, salary, avg(salary) over () as allavgsalary
from employeesalary;
-- using group by
select employeeid, salary, avg(salary) as allavgsalary
from employeesalary
group by employeeid, salary
order by employeeid;
-- subquery in from
select a.employeeid, allavgsalary
from (select employeeid, salary, avg(salary) over() as allavgsalary from employeesalary) a
order by a.employeeid;
-- subquery in where
select employeeid, jobtitle, salary
from employeesalary
where EmployeeID in (select employeeid from employeedemographics where age > 20);