You are a database professor who need to complete the assignment. Consider the following…
- User
You are a database professor who need to complete the assignment. Consider the following schema of a Company database: Employees(eid: integer, ename: string, address: string, supereid: integer, salary: integer) Departments(did: integer, dname: string) Projects(pid: integer, pname: string, did: integer, budget: integer) Works_on(eid: integer, pid: integer, hours: integer) Primary keys are underlined. Each Employee has a supervisor (another Employee) referenced by their supereid. Projects are uniquely assigned to a Department. The Works_on relation records which Employee works on which Project for how many hours a week. The primary key of a relation consists of all its underlined attributes. There are foreign key constraints between Employees.supereid and Employees.eid, between Works_on.eid and Employees.eid, between Works_on.pid and Projects.pid and between Projects.did and Departments.did. Formulate the following natural language questions as SQL queries or explain why they cannot be expressed in SQL. Assignment 1.1 (10 marks) For the “IT” Department, return the pname of those projects of that Department for which at least one Employee works. Assignment 1.2 (15 marks) For each Employee, return their ename and the ename of their supervisees (the Employees directly supervised by that Employee). Assignment 1.3 (15 marks) For each Employee, return their ename and the ename of the Employees directly or indirectly supervised by that Employee. Assignment 1.4 (15 marks) Return the eids of Employees who do not work on every Project. Assignment 1.5 (15 marks) Return the eid of the Employees who are working for the maximum number of Departments. Assignment 1.6 (15 marks) Return the pid of the Projects that have the minimum budget. Assignment 1.7 (15 marks) Return the pids of the Projects for which at least two different Employees are working.
Assignment 1.5
- Select the employee with the number of departments they are working on. Department is connected to Projects which will need to be department must be unique in order to count the unique departments
SELECT W2.eid, COUNT(DISTINCT P2.did) AS working_departments FROM Works_on W2 JOIN Projects P2 ON W2.pid = P2.pid;- Get the maximum number of departments from the subquery which is to select the number of departments the employees are working on
SELECT MAX(working_departments) FROM (SELECT W2.eid, COUNT(DISTINCT P2.did) AS working_departments FROM Works_on W2 JOIN Projects P2 ON W2.pid = P2.pid) AS working_departments;- Select eid
SELECT W.eid FROM Works_on W JOIN Project P ON W.pid = P.pid GROUP BY W.eid HAVING COUNT(DISTINCT P.did) = ( SELECT MAX(working_departments) FROM (SELECT W2.eid, COUNT(DISTINCT P2.did) AS working_departments FROM Works_on W2 JOIN Projects P2 ON W2.pid = P2.pid) AS working_departments ); - Assistant