0% found this document useful (0 votes)
6 views5 pages

Cycle 3 Query 3 Company Database

The document outlines a database laboratory assignment involving a Company Database schema with tables for DEPARTMENT, EMPLOYEE, DLOCATION, PROJECT, and WORKS_ON. It includes SQL queries for various tasks such as listing project numbers associated with employees named 'Scott', calculating salary raises, and analyzing employee salaries in specific departments. Additionally, it provides instructions for creating tables, inserting data, and performing updates to maintain referential integrity.

Uploaded by

sne.222316
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Cycle 3 Query 3 Company Database

The document outlines a database laboratory assignment involving a Company Database schema with tables for DEPARTMENT, EMPLOYEE, DLOCATION, PROJECT, and WORKS_ON. It includes SQL queries for various tasks such as listing project numbers associated with employees named 'Scott', calculating salary raises, and analyzing employee salaries in specific departments. Additionally, it provides instructions for creating tables, inserting data, and performing updates to maintain referential integrity.

Uploaded by

sne.222316
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

22IS406 - Database Laboratory

Consider the schema for Company Database:


DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)
DLOCATION (DNo,DLoc)
PROJECT (PNo, PName, PLocation, DNo)
WORKS_ON (SSN, PNo, Hours)

Write SQL queries to

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.

CREATE TABLE DEPARTMENT


(DNO VARCHAR2 (20) PRIMARY KEY,

Department of Information Science & Engineering Page 1


22IS406 - Database Laboratory

DNAME VARCHAR2 (20),


MGRSTARTDATE DATE);

CREATE TABLE EMPLOYEE


(SSN VARCHAR2 (20) PRIMARY KEY,
FNAME VARCHAR2 (20),
LNAME VARCHAR2 (20),
ADDRESS VARCHAR2 (20),
SEX CHAR (1),
SALARY INTEGER,
SUPERSSN REFERENCES EMPLOYEE (SSN),
DNO REFERENCES DEPARTMENT (DNO));
--NOTE: Once DEPARTMENT and EMPLOYEE tables are created we must
--alter department table to add foreign constraint MGRSSN using sql command
ALTER TABLE DEPARTMENT
ADD MGRSSN REFERENCES EMPLOYEE (SSN);

CREATE TABLE PROJECT


(PNO INTEGER PRIMARY KEY,
Prepared by Prof. Guru Prasad and Prof. Suresh Chimkode
PNAME VARCHAR2 (20),
PLOCATION VARCHAR2 (20),
DNO REFERENCES DEPARTMENT (DNO));

CREATE TABLE WORKS_ON


(HOURS NUMBER (2),
SSN REFERENCES EMPLOYEE (SSN),
PNO REFERENCES PROJECT(PNO),
PRIMARY KEY (SSN, PNO));
2. Enter tuples for each relation
EMPLOYEE
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘SSITECE01’,’JOHN’,’SCOTT’,’BANGALORE’,’M’, 450000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘SSITCSE01’,’JAMES’,’SMITH’,’BANGALORE’,’M’, 500000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘SSITCSE02’,’HEARN’,’BAKER’,’BANGALORE’,’M’, 700000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘SSITCSE03’,’EDWARD’,’SCOTT’,’MYSORE’,’M’, 500000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘SSITCSE04’,’PAVAN’,’HEGDE’,’MANGALORE’,’M’, 650000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘SSITCSE05’,’GIRISH’,’MALYA’,’MYSORE’,’M’, 450000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘SSITCSE06’,’NEHA’,’SN’,’BANGALORE’,’F’, 800000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘SSITACC01’,’AHANA’,’K’,’MANGALORE’,’F’, 350000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‘SSITACC02’,’SANTHOSH’,’KUMAR’,’MANGALORE’,’M’, 300000);
Department of Information Science & Engineering Page 2
22IS406 - Database Laboratory

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';

UPDATE EMPLOYEE SET


SUPERSSN='SSITCSE02', DNO='5'
WHERE SSN='SSITCSE01';

UPDATE EMPLOYEE SET


SUPERSSN='SSITCSE03', DNO='5'
WHERE SSN='SSITCSE02';

UPDATE EMPLOYEE SET


SUPERSSN='SSITCSE04', DNO='5'
WHERE SSN='SSITCSE03';

UPDATE EMPLOYEE SET


DNO='5', SUPERSSN='SSITCSE05'
WHERE SSN='SSITCSE04';

UPDATE EMPLOYEE SET


DNO='5', SUPERSSN='SSITCSE06'
WHERE SSN='SSITCSE05';

UPDATE EMPLOYEE SET


DNO='5', SUPERSSN=NULL
WHERE SSN='SSITCSE06';

UPDATE EMPLOYEE SET


DNO='1', SUPERSSN='SSITACC02'
WHERE SSN='SSITACC01';

UPDATE EMPLOYEE SET

Department of Information Science & Engineering Page 3


22IS406 - Database Laboratory

DNO='1', SUPERSSN=NULL
WHERE SSN='SSITACC02';

UPDATE EMPLOYEE SET


DNO='4', SUPERSSN=NULL
WHERE SSN='SSITISE01';

UPDATE EMPLOYEE SET


DNO='2', SUPERSSN=NULL
WHERE SSN='SSITIT01';

SELECT * FROM EMPLOYEE;


DLOCATION:
INSERT INTO DLOCATION VALUES ('BANGALORE', '1');
INSERT INTO DLOCATION VALUES ('BANGALORE', '2');
INSERT INTO DLOCATION VALUES ('BANGALORE', '3');
INSERT INTO DLOCATION VALUES ('MANGALORE', '4');
INSERT INTO DLOCATION VALUES ('MANGALORE', '5');

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

Department of Information Science & Engineering Page 4


22IS406 - Database Laboratory

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;

Department of Information Science & Engineering Page 5

You might also like