-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassignment B full 20.sql
More file actions
266 lines (177 loc) · 6.12 KB
/
assignment B full 20.sql
File metadata and controls
266 lines (177 loc) · 6.12 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
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
--Request 1
--List the last name, first name and employee number of all employees that
--have a last name starting with M.
use CGDatabase
Select Last_Name, First_Name, Employee_No from dbo.Employees
where Last_Name like 'M%'
--Request 2
--List the department number, last name, first name and phone number of all sales
--representatives who were hired on or after 24 Mar 1998 sorted in ascending order of last name.
use CGDatabase
select Department_No, First_Name, Last_Name, Phone_Number
from employees e, Jobs j
where e.Job_ID = j.Job_ID
and Hire_Date > '1998-03-23'
and Job_Title = 'Sales Representative'
order by Last_Name ASC
--Request 3
--List all the data for all jobs where the minimum salary is less than or
--equal to 4500 sorted in descending order of the minimum salary.
use CGDatabase
select * from Jobs
where Min_Salary <= 4500
order by Min_Salary DESC
--Request 4
--Which jobs are found in the Marketing and Accounting departments?
use CGdatabase
select Job_Title from Jobs j, Employees e, Departments d
where j.Job_ID = e.Job_ID
and d.Department_No = e.Department_No
and (Department_Name = 'Marketing' or Department_Name = 'Accounting')
--Request 5
--List the department name, location, last name and salary of employees who
--work in location 1700 sorted in ascending order of department name.
use CGDatabase
select Department_Name, City, Locations.Location_ID, Last_Name, Annual_Salary
from Employees
join Departments
on Employees.Department_No = Departments.Department_No
join Locations
on Departments.Location_ID = Locations.Location_ID
where
Locations.Location_ID = 1700
--Request 6
--List the last name and first name for all employees who were hired in
--the months of June or August (for all years) sorted in ascending order of last name.
use CGDatabase
select Last_Name, First_Name
from Employees
where
MONTH(Hire_Date)=6
or
MONTH(Hire_Date)=8
order by Last_Name ASC
--Request 7
--Show the average salary for employees for one year (rounded to 2 decimal places).
use CGDatabase
select ROUND(AVG(CAST(Annual_Salary AS MONEY)),2)
from Employees
--Request 8
--Show the total monthly salaries figure (0 decimal places) for all employees
--in departments 80 and 60.
use CGDatabase
select
ROUND(CAST(SUM(Annual_Salary/12) as int),0) as "Total_Monthly_Salary"
from
Employees
where
Department_No = 60
or
Department_No = 80
--Request 9
--List the department number, department name and the number of employees
--for each department that has less than 4 employees grouping by department number
--and department name.
use CGDatabase
select Departments.Department_No, Departments.Department_Name, Emp_Num
from Departments
join(
select Department_No,
COUNT(Employee_No) as Emp_Num
from Employees
group by Department_No
)
as Emp1
on Emp1.Department_No = Departments.Department_No
where Emp_Num < 4
group by Departments.Department_No, Department_Name, Emp_Num
--Request 10
--List the department number, department name and the number of employees for
--the department that has the lowest number of employees using appropriate grouping.
use CGDatabase
select Departments.Department_No, Department_Name, Emp_Num
from Departments
join (
select Department_No,
COUNT(Employee_No) as Emp_Num
from Employees
group by Department_No
) as Emp1
on Emp1.Department_No = Departments.Department_No
where Emp_Num = (
select Min(Emp_Num) as Min_Emp
from Departments
join (
select Department_No, count(*)as Emp_Num
from Employees
group by Department_No
) as Empl
on Empl.Department_No = Departments.Department_No
)
group by Departments.Department_No, Department_Name, Emp_Num
--Request 11
--List the department number and name for all departments where no sales representatives work.
use CGDatabase
select Departments.Department_No, Department_Name
from
Departments
join Employees
on Employees.Department_No= Departments.Department_No
where Department_Name <> 'Sales'
group by Departments.Department_No, Department_Name
--Request 11 Proper Method.
select distinct d.Department_Name, d.Department_No from Departments d, Jobs j, Employees e
where d.Department_No = e.Department_No
and j.Job_ID = e.Job_ID
and d.Department_No NOT IN(
Select d.Department_No from Departments d, Jobs j, Employees e
where d.Department_No = e.Department_No
and j.Job_ID = e.Job_ID
and job_title = 'Sales Representative')
--Request 12
--Add the following new job
--IT_ASST, IT Assistant, 5000, 8000
use CGDatabase
insert into Jobs(Job_ID, Job_Title, Min_Salary,Max_Salary)
values('IT_ASST', 'IT Assistant', 5000, 8000)
--Request 13
--Update all the minimum salaries for jobs with an increase of 500.
use CGDatabase
update Jobs
set Min_Salary = (Min_Salary+500)
--Request 14
--List all the data for jobs sorted in ascending order of job id.
use CGDatabase
select * from Jobs
order by Job_ID ASC
--15.
--Request 16
--Print a copy of the data dictionary entry for the table departments.
use CGDatabase
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'Departments'
sp_help Departments
--Request 17
--Drop the table for job history.
drop table Job_History
--Request 18
--Create a new table called SAL_HISTORY to include the fields
--EMPID, FIRSTNAME, LASTNAME, HIREDATE and SAL with the
--same data types as the employees table. The EMPID must not be NULL.
CREATE TABLE SAL_HISTORY(
Emp_ID int not null,
First_Name varchar(20),
Last_Name varchar(25),
Hire_Date datetime,
Annual_Salary decimal(8,2)
)
--Request 19
--Insert data from the employees table where the employee number is less than or
-- equal to 130 into the SAL_HISTORY table.
INSERT INTO SAL_HISTORY (Emp_ID, First_Name, Last_Name, Hire_Date, Annual_Salary)
SELECT Employee_No, First_Name, Last_Name, Hire_Date, Annual_Salary
FROM Employees
where Employee_No < 130
--Request 20
--Display all the records and all the fields in the SAL_HISTORY table.
select * from SAL_History