forked from Jinglin-LI/SQL-Query
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment2 (1).sql
More file actions
225 lines (167 loc) · 6.57 KB
/
Assignment2 (1).sql
File metadata and controls
225 lines (167 loc) · 6.57 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
------------------------------------------------------------------
-------------Jinglin Li (jxl163530) Database --------------------
--------------------Assignment 2-------------------------------
-------------------------------------------------------------
----------------Part I-----------------------------------
-- Create all the tables givin in following schema.
CREATE TABLE DEPARTMENT (
Dname varchar(25) not null,
Dnumber int not null,
Mgr_ssn char(9) not null,
Mgr_start_date date,
primary key (dnumber),
UNIQUE (dname)
);
CREATE TABLE EMPLOYEE (
Fname varchar(15) not null,
Minit varchar(1),
Lname varchar(15) not null,
Ssn char(9),
Bdate date,
Address varchar(50),
Sex char,
Salary decimal(10,2),
Super_ssn char(9),
Dno int,
primary key (ssn),
foreign key (dno) references DEPARTMENT(dnumber)
);
CREATE TABLE DEPENDENT (
Essn char(9),
Dependent_name varchar(15),
Sex char,
Bdate date,
Relationship varchar(8),
primary key (essn,dependent_name),
foreign key (essn) references EMPLOYEE(ssn)
);
CREATE TABLE DEPT_LOCATIONS (
Dnumber int,
Dlocation varchar(15),
primary key (dnumber,dlocation),
foreign key (dnumber) references DEPARTMENT(dnumber)
);
CREATE TABLE PROJECT (
Pname varchar(25) not null,
Pnumber int,
Plocation varchar(15),
Dnum int not null,
primary key (pnumber),
unique (pname),
foreign key (dnum) references DEPARTMENT(dnumber)
);
CREATE TABLE WORKS_ON (
Essn char(9),
Pno int,
Hours decimal(4,1),
primary key (essn,pno),
foreign key (essn) references EMPLOYEE(ssn),
foreign key (pno) references PROJECT(pnumber)
);
-- Adding FK constraint after loading data into system
Alter table EMPLOYEE
ADD foreign key (super_ssn) references EMPLOYEE(ssn);
Alter table DEPARTMENT
ADD foreign key (Mgr_ssn) references EMPLOYEE(Ssn);
-------------------------------------------------------------------
---------------Part II---------------------------------------------
-------------------------------------------------------------------
-- 1.a. For each department whose average employee salasy is more than $30,000, retrive the department name and the number of employees working for that department.
use Company;
select Dname, COUNT(*) Number_Of_Employee
from DEPARTMENT, EMPLOYEE
where Dno = Dnumber
group by Dname
having AVG(Salary) > 30000;
-- 1.b. Same as a, except ouput the number of male employees instead of the number of employees.
use Company;
select Dname, COUNT(*) Number_Of_Male_Employee
from DEPARTMENT, EMPLOYEE
where Dno = Dnumber and sex = 'M' and dname in (select Dname
from DEPARTMENT, EMPLOYEE
where Dno = Dnumber
group by Dname
having AVG(salary) > 30000)
group by dname
--1.c. Retrieve the names of all employees who work in the department that has the employee with the highest salary among all employees
select Fname, Lname
from EMPLOYEE
where Dno = (select Dno
from EMPLOYEE
where Salary = (select MAX(Salary)
from employee))
--1.d. Retrieve the names of all employees who make at least $10,000 more than the employe who is paid the least in the company.
use Company;
select Fname, Lname
from EMPLOYEE
where Salary > 10000 + (select MIN(Salary)
from EMPLOYEE)
--1.e. Retrieve the names of employes who is making least in their departments and have more than one dependent (solve using correlated nested queries)
use Company;
select Fname, Lname
from EMPLOYEE E1
where Salary = (select min(Salary)
from Employee E2
where E1.Dno = E2.Dno)
and
Ssn in (select Ssn
from EMPLOYEE, DEPENDENT
where Ssn = Essn
group by Ssn
having COUNT(*) > 1)
--------------------------------------------------------------------------------------------------------------
-- 2.Specify following views in SQL. Solve questions using correlated nested queries(except a).
-- 2.a. A view that has the department name, manager name and manager salary for every department.
create view manger_info
as select Dname, Fname, Lname, Salary
from DEPARTMENT, EMPLOYEE
where Mgr_ssn = Ssn
-- 2.b. A view that has the department name, its manager's name, number of employees working in that department, and the number of projects controlled by that department (for each department).
create view manger_info
as select Dname, Fname, Lname, (select COUNT(*)
from EMPLOYEE E2
where E2.Dno = D1.Dnumber) as Num_Employee,
(select COUNT(*)
from PROJECT P
where P.Dnum = D1.Dnumber) as Num_Project
from DEPARTMENT D1, EMPLOYEE E1
where D1.Mgr_ssn = E1.Ssn
-- 2.c. A view that has the project name, controlling department name, number of employees, and total hours worked per week on the project for each project with more than one employee working on it.
create view project_info
as select Pname, Dname, (select COUNT(*)
from WORKS_ON W1
where W1.Pno = P1.Pnumber) as Num_Employee,
(select SUM(W2.Hours)
from WORKS_ON W2
where W2.Pno = P1.Pnumber
group by Pno) as Total_Hours
from PROJECT P1, DEPARTMENT D1
where P1.Dnum = D1.Dnumber
-- 2.d. A view that has the project name, controlling departmet name, number of employees, and total hours worked per week on the project for each project with more than one employee working on it.
create view project_info2
as select Pname, Dname, (select COUNT(*)
from WORKS_ON W1
where W1.Pno = P1.Pnumber) as Num_Employee,
(select SUM(W2.Hours)
from WORKS_ON W2
where W2.Pno = P1.Pnumber
group by Pno) as Total_Hours
from PROJECT P1, DEPARTMENT D1
where P1.Dnum = D1.Dnumber and (select COUNT(*)
from WORKS_ON W2
where W2.Pno = P1.Pnumber
group by W2.Pno) > 1
-- 2.e. A view that has the employee name, employee salary, department that the employee works in, department manager name, manger salary, and average salary for the department
create view employee_info
as select Fname + ' ' + Lname as Employee_Name, Salary, Dname, (select Fname + ' ' + Lname
from EMPLOYEE E2
where D1.Mgr_ssn = E2.Ssn) as Manger_Name,
(select Salary
from EMPLOYEE E3
where D1.Mgr_ssn = E3.Ssn) as manger_Salary,
(select AVG(salary)
from EMPLOYEE E4
where E4.Dno = D1.Dnumber
group by E4.Dno) as average_salary
from EMPLOYEE E1, DEPARTMENT D1
where E1.Dno = D1.Dnumber