-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 2.sql
More file actions
131 lines (90 loc) · 3.23 KB
/
Assignment 2.sql
File metadata and controls
131 lines (90 loc) · 3.23 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
-- 1 List the department managers who are controlling more than 2 projects.
SELECT Fname , Lname
FROM EMPLOYEE
WHERE Ssn IN (
-- Get their ssn
SELECT Mgr_ssn
FROM DEPARTMENT
WHERE Dnumber = ANY
(
-- Get the number of managers from project table controlling more than two projects
SELECT Dnum
FROM PROJECT
GROUP BY Dnum
HAVING COUNT(*) > 2
)
);
-- 2 Retrieve the names of the departments that only located in ‘Houston’.
SELECT Dname
FROM DEPARTMENT
WHERE Dnumber IN (
SELECT DEPT_LOCATIONS.Dnumber
FROM DEPT_LOCATIONS
WHERE Dlocation = "Houston"
);
-- 3 List the names of employees all of whose dependents were born before 1960.
SELECT Fname , Lname
FROM EMPLOYEE
WHERE SSN IN (
-- Dependents born before 1960
SELECT Essn
FROM DEPENDENT
WHERE YEAR(Bdate) < 1960
);
-- 4 List the names of departments managed by the direct subordinate of the manager of ‘Headquarters’ department.
SELECT Dname
FROM DEPARTMENT
WHERE Mgr_ssn IN (
-- Get the ssn of the employee managing another department but subordinate to hq
SELECT Ssn
FROM EMPLOYEE
WHERE Super_ssn = ANY (
-- Get the manager's ssn and pass it to outter select to find who he/she supervises
SELECT Mgr_ssn
FROM DEPARTMENT
WHERE Dname = "Headquarters"
)
);
-- 5 Retrieve the average salary of male employees who work totally no less than 50 hours on projects.
SELECT AVG (SALARY) AS Average_Salary
FROM EMPLOYEE
WHERE SEX = "M" AND SSN IN
(
SELECT ESSN
FROM WORKS_ON
GROUP BY ESSN
HAVING SUM(Hours) >= 50
);
-- 6 Find the names of projects that all direct subordinates of James Borg work for.
SELECT Pname
FROM PROJECT
WHERE Pnumber IN (
-- Get the project number they are working on and pass it to projects to find the Pname
SELECT Pno
FROM WORKS_ON
WHERE Essn = ANY (
-- Get the ssn of employees who are direct subordinates of james
SELECT Ssn
FROM EMPLOYEE
WHERE Super_ssn = ANY (
-- Get the ssn of james and pass it to outter to find who his subordinates are
SELECT Ssn
FROM EMPLOYEE
WHERE Fname = "James" AND Lname = "Borg"
)
)
);
-- 7 Show the name of employee who and whose supervisor are in different departments.
SELECT E.FNAME , E.LNAME
FROM Company.Employee e
LEFT JOIN Company.Employee s
ON e.Super_ssn = s.Super_ssn
WHERE e.DNO != S.DNO;
-- 8 Find the name of employee who only has spouse as the dependent.
SELECT e.Fname , e.Lname
FROM Company.Employee e
JOIN Company.Dependent d ON d.Essn = e.Ssn
WHERE d.Relationship = 'Spouse' AND (
SELECT COUNT(*)
FROM Company.Dependent dd
WHERE e.Ssn = dd.Essn AND (dd.Relationship = 'daughter')) = 0;