0% found this document useful (0 votes)
13 views4 pages

Company Database

The document outlines the schema and SQL commands for a Company Database, including tables for EMPLOYEE, DEPARTMENT, DLOCATION, PROJECT, and WORKS_ON. It provides SQL commands for creating tables, inserting values, and executing various queries to retrieve specific information about employees, projects, and departments. Key queries include listing project numbers involving employees named 'Scott', calculating salary raises for employees on the 'IoT' project, and summarizing salaries in the 'Accounts' department.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Company Database

The document outlines the schema and SQL commands for a Company Database, including tables for EMPLOYEE, DEPARTMENT, DLOCATION, PROJECT, and WORKS_ON. It provides SQL commands for creating tables, inserting values, and executing various queries to retrieve specific information about employees, projects, and departments. Key queries include listing project numbers involving employees named 'Scott', calculating salary raises for employees on the 'IoT' project, and summarizing salaries in the 'Accounts' department.

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 PDF, TXT or read online on Scribd
You are on page 1/ 4

Consider the schema for Company Database:

1. EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)


2. DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
3. DLOCATION (DNo,DLoc)
4. PROJECT (PNo, PName, PLocation, DNo)
5. WORKS_ON (SSN, PNo, Hours)

Table Creation:
CREATE TABLE DEPARTMENT (DNO VARCHAR (20) PRIMARY KEY, DNAME VARCHAR (20), MGRSTARTDATE DATE);
CREATE TABLE EMPLOYEE (SSN VARCHAR (20) PRIMARY KEY, FNAME VARCHAR (20), LNAME VARCHAR (20),
ADDRESS VARCHAR (20), SEX CHAR (1), SALARY INTEGER, DNO VARCHAR (20) REFERENCES DEPARTMENT
(DNO));

ALTER TABLE EMPLOYEE ADD SUPERSSN VARCHAR (20) REFERENCES EMPLOYEE (SSN);

ALTER TABLE DEPARTMENT ADD MGRSSN VARCHAR(20) REFERENCES EMPLOYEE (SSN);

CREATE TABLE DLOCATION (DLOC VARCHAR (20), DNO VARCHAR (20) REFERENCES DEPARTMENT (DNO),
PRIMARY KEY (DNO, DLOC));

CREATE TABLE PROJECT (PNO INTEGER PRIMARY KEY, PNAME VARCHAR (20), PLOCATION VARCHAR (20), DNO
VARCHAR (20) REFERENCES DEPARTMENT (DNO));

CREATE TABLE WORKS_ON (HOURS INTEGER (2), SSN VARCHAR (20) REFERENCES EMPLOYEE (SSN), PNO
INTEGER REFERENCES PROJECT(PNO), PRIMARY KEY (SSN, PNO));
Insertion of values to tables:
INSERT INTO EMPLOYEE VALUES ('RNSECE01','JOHN','SCOTT','BANGALORE','M', 450000,'3',NULL);
INSERT INTO EMPLOYEE VALUES ('RNSCSE01','JAMES','SMITH','BANGALORE','M', 500000,'5','RNSCSE02');
INSERT INTO EMPLOYEE VALUES ('RNSCSE02','HEARN','BAKER','BANGALORE','M', 700000,'5','RNSCSE03');
INSERT INTO EMPLOYEE VALUES ('RNSCSE03','EDWARD','SCOTT','MYSORE','M', 500000,'5','RNSCSE04');
INSERT INTO EMPLOYEE VALUES ('RNSCSE04','PAVAN','HEGDE','MANGALORE','M', 650000,'5','RNSCSE05');
INSERT INTO EMPLOYEE VALUES ('RNSCSE05','GIRISH','MALYA','MYSORE','M', 450000,'5','RNSCSE06');
INSERT INTO EMPLOYEE VALUES ('RNSCSE06','NEHA','SN','BANGALORE','F', 800000,'5',NULL);
INSERT INTO EMPLOYEE VALUES ('RNSACC01','AHANA','K','MANGALORE','F', 350000,'1','RNSACC02');
INSERT INTO EMPLOYEE VALUES ('RNSACC02','SANTHOSH','KUMAR','MANGALORE','M', 300000,'1',NULL);
INSERT INTO EMPLOYEE VALUES ('RNSISE01','VEENA','M','MYSORE','M', 600000,'4',NULL);
INSERT INTO EMPLOYEE VALUES ('RNSIT01' ,'NAGESH','HR','BANGALORE','M', 500000,'2',NULL);
INSERT INTO DEPARTMENT VALUES ('1','ACCOUNTS','2001-01-01','RNSACC02');
INSERT INTO DEPARTMENT VALUES ('2','IT','2001-08-16','RNSIT01');
INSERT INTO DEPARTMENT VALUES ('3','ECE','2001-06-08','RNSECE01');
INSERT INTO DEPARTMENT VALUES ('4','ISE','2001-08-15','RNSISE01');
INSERT INTO DEPARTMENT VALUES ('5','CSE','2001-06-02','RNSCSE05');

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

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');
INSERT INTO WORKS_ON VALUES (4, 'RNSCSE01', 100);
INSERT INTO WORKS_ON VALUES (6, 'RNSCSE01', 101);
INSERT INTO WORKS_ON VALUES (8, 'RNSCSE01', 102);
INSERT INTO WORKS_ON VALUES (10, 'RNSCSE02', 100);
INSERT INTO WORKS_ON VALUES (3, 'RNSCSE04', 100);
INSERT INTO WORKS_ON VALUES (4, 'RNSCSE05', 101);
INSERT INTO WORKS_ON VALUES (5, 'RNSCSE06', 102);
INSERT INTO WORKS_ON VALUES (6, 'RNSCSE03', 102);
INSERT INTO WORKS_ON VALUES (7, 'RNSECE01', 103);
INSERT INTO WORKS_ON VALUES (5, 'RNSACC01', 104);
INSERT INTO WORKS_ON VALUES (6, 'RNSACC02', 105);
INSERT INTO WORKS_ON VALUES (4, 'RNSISE01', 106);
INSERT INTO WORKS_ON VALUES (10, 'RNSIT01', 107);
Queries:

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')
EXCEPT
(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;

You might also like