Cycle 3 Query 3 Company Database
Cycle 3 Query 3 Company Database
I. 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.
II. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10 percent raise.
III. 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
IV. Retrieve the name of each employee who works on all the projects controlled by department number 5
(use NOT EXISTS operator).
V. 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.
ER Diagram:
1. Create the above tables by properly specifying the primary keys and the foreign keys.
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘SSITISE01’,’VEENA’,’M’,’MYSORE’,’M’, 600000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘SSITIT01’,’NAGESH’,’HR’,’BANGALORE’,’M’, 500000);
DEPARTMENT
INSERT INTO DEPARTMENT VALUES (‘1’,’ACCOUNTS’,’01-JAN-01’,’SSITACC02’);
INSERT INTO DEPARTMENT VALUES (‘2’,’IT’,’01-AUG-16’,’SSITIT01’);
INSERT INTO DEPARTMENT VALUES (‘3’,’ECE’,’01-JUN-08’,’SSITECE01’);
INSERT INTO DEPARTMENT VALUES (‘4’,’ISE’,’01-AUG-15’,’SSITISE01’);
INSERT INTO DEPARTMENT VALUES (‘5’,’CSE’,’01-JUN-02’,’SSITCSE05’);
--Note: update entries of employee table to fill missing fields SUPERSSN and DNO
--1
UPDATE EMPLOYEE SET
SUPERSSN=NULL, DNO='3'
WHERE SSN='SSITECE01';
DNO='1', SUPERSSN=NULL
WHERE SSN='SSITACC02';
PROJECT:
INSERT INTO PROJECT VALUES (100,'IOT','BANGALORE','5');
INSERT INTO PROJECT VALUES (101,'CLOUD','BANGALORE','5');
INSERT INTO PROJECT VALUES (102,'BIGDATA','BANGALORE','5');
INSERT INTO PROJECT VALUES (103,'SENSORS','BANGALORE','3');
INSERT INTO PROJECT VALUES (104,'BANK MANAGEMENT','BANGALORE','1');
INSERT INTO PROJECT VALUES (105,'SALARY MANAGEMENT','BANGALORE','1');
INSERT INTO PROJECT VALUES (106,'OPENSTACK','BANGALORE','4');
INSERT INTO PROJECT VALUES (107,'SMART CITY','BANGALORE','2');
WORKS_ON
INSERT INTO WORKS_ON VALUES (4, 'SSITCSE01', 100);
INSERT INTO WORKS_ON VALUES (6, 'SSITCSE01', 101);
INSERT INTO WORKS_ON VALUES (8, 'SSITCSE01', 102);
INSERT INTO WORKS_ON VALUES (10, 'SSITCSE02', 100);
INSERT INTO WORKS_ON VALUES (3, 'SSITCSE04', 100);
INSERT INTO WORKS_ON VALUES (4, 'SSITCSE05', 101);
INSERT INTO WORKS_ON VALUES (5, 'SSITCSE06', 102);
INSERT INTO WORKS_ON VALUES (6, 'SSITCSE03', 102);
INSERT INTO WORKS_ON VALUES (7, 'SSITECE01', 103);
INSERT INTO WORKS_ON VALUES (5, 'SSITACC01', 104);
INSERT INTO WORKS_ON VALUES (6, 'SSITACC02', 105);
INSERT INTO WORKS_ON VALUES (4, 'SSITISE01', 106);
INSERT INTO WORKS_ON VALUES (10,'SSITITE01', 107);
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 P.PNO
FROM PROJECT P, DEPARTMENT D, EMPLOYEE E WHERE E.DNO=D.DNO
AND D.MGRSSN=E.SSN
AND E.LNAME=‘SCOTT‘)
UNION
(SELECT DISTINCT P1.PNO
FROM PROJECT P1, WORKS_ON W, EMPLOYEE E1 WHERE P1.PNO=W.PNO
AND E1.SSN=W.SSN
AND E1.LNAME=‘SCOTT‘);
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10 percent raise.
SELECT E.FNAME, E.LNAME, 1.1*E.SALARY AS INCR_SAL
FROM EMPLOYEE E, WORKS_ON W, PROJECT P
WHERE E.SSN=W.SSN
AND W.PNO=P.PNO
AND P.PNAME='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 (E.SALARY), MAX (E.SALARY), MIN (E.SALARY), AVG (E.SALARY)
FROM EMPLOYEE E, DEPARTMENT D
WHERE E.DNO=D.DNO
AND D.DNAME='ACCOUNTS';
4. Retrieve the name of each employee who works on all the projects controlled by department number
5 (use NOT EXISTS operator).
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE E
WHERE NOT EXISTS((SELECT PNO FROM PROJECT WHERE DNO='5') MINUS (SELECT
PNO FROM WORKS_ON
WHERE E.SSN=SSN));
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 D.DNO, COUNT (*)
FROM DEPARTMENT D, EMPLOYEE E
WHERE D.DNO=E.DNO
AND E.SALARY>600000
AND D.DNO IN (SELECT E1.DNO
FROM EMPLOYEE E1
GROUP BY E1.DNO
HAVING COUNT (*)>5)
GROUP BY D.DNO;