Database 5
Company Database
Consider the schema for Company Database:
EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)
DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
DLOCATION (DNo,DLoc)
PROJECT (PNo, PName, PLocation, DNo)
WORKS_ON (SSN, PNo, Hours)
ER Diagram for Company Database
Create tables and insert values
Sample Query
1. Display the name of managers of all department
2. Display the manager name who earns highest salary
1. Make a list of all project numbers for projects that involve an employee whose last name is ‘Scott’,
either as a worker or as a manager of the department that controls the project.
(SELECT DISTINCT [Link]
FROM PROJECT P, DEPARTMENT D, EMPLOYEE E
WHERE [Link]=[Link]
AND [Link]=[Link]
AND [Link]=”SCOTT”)
UNION
(SELECT DISTINCT [Link]
FROM PROJECT P1, WORKS_ON W, EMPLOYEE E1
WHERE [Link]=[Link]
AND [Link]=[Link]
AND [Link]=”SCOTT”);
2. Show the resulting salaries if every employee working on the
‘IoT’ project is given a 10 percent raise.
SELECT [Link], [Link], 1.1*[Link] AS INCR_SAL
FROM EMPLOYEE E, WORKS_ON W, PROJECT P WHERE
[Link]=[Link]
AND [Link]=[Link]
AND [Link]=”IOT”;
3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well as
the maximum salary, the minimum salary, and the average salary in this department
SELECT SUM([Link]), MAX([Link]), MIN([Link]),
AVG([Link])
FROM EMPLOYEE E, DEPARTMENT D
WHERE [Link]=[Link]
AND [Link]=”ACCOUNTS”;
4. Retrieve the name of each employee who works on all the projects Controlled by department number 5
(use NOT EXISTS operator).
SELECT [Link], [Link]
FROM EMPLOYEE E
WHERE NOT EXISTS (SELECT PNUMBER FROM PROJECT WHERE
DNO = “5” AND PNUMBER NOT IN (SELECT [Link] FROM
WORKS_ON W WHERE [Link] = [Link]) );
5. For each department that has more than five employees, retrieve the department
number and the number of its employees who are making more than Rs. 6, 00,000.
SELECT [Link], COUNT(*)
FROM DEPARTMENT D, EMPLOYEE E
WHERE [Link]=[Link]
AND [Link]>600000
AND [Link] IN (SELECT [Link]
FROM EMPLOYEE E1
GROUP BY [Link]
HAVING COUNT(*)>5)
GROUP BY [Link];