Company Database Lab Work
Company Database Lab Work
Assignment-I
Subject/Subject Code :DBMS Lab/BCS403
Semester/Section :4th A
Student Name :
USN :
Company Database
ER-Daigram
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
Schema Diagram
Referential Integrity
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
Database State
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
SQL Commands
/*Creation of EMPLOYEE Table*/
Minit varchar(1),
Bdate date,
Address varchar(30),
Sex varchar(1),
Salary float,
Super_ssn varchar(9),
Dno int NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Super_ssn) REFERENCES
EMPLOYEE(Ssn)
);
desc EMPLOYEE
Mgr_start_date date,
UNIQUE(Dname),
);
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
desc DEPARTMENT
Dlocation varchar(15),
);
desc DEPT_LOCATIONS
Plocation varchar(15),
UNIQUE (Pname),
);
desc DEPT_LOCATIONS
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
Plocation varchar(15),
UNIQUE (Pname),
);
desc PROJECT
);
desc WORKS_ON
Sex varchar(1),
Bdate date,
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
);
desc DEPENDENT
INSERT INTO EMPLOYEE values ('James', 'E', 'Borg', 888665555, '10-NOV-1937', '430 Stone,
Houston, TX', 'M', 55000, NULL, 1);
INSERT INTO EMPLOYEE values ('Jennifer', 'S', 'Wallace', 987654321, '20-JUN-1941', '291 Berry,
Bellaire, TX', 'F',43000, 888665555, 4);
INSERT INTO EMPLOYEE values ('Franklin', 'B', 'Wong', 333445555, '08-DEC-1955', '638 Voss,
Houston, TX', 'M', 40000, 888665555, 5);
INSERT INTO EMPLOYEE values ('Alicia', 'J', 'Zelaya', 999887777, '19-JAN-1968', '3321 Castle,
Spring, TX', 'F', 25000, 987654321, 4);
INSERT INTO EMPLOYEE values ('Ramesh', 'K', 'Narayan', 666884444, '15-SEP-1962', '975 Fire Oak,
Humble, TX', 'M', 38000, 333445555, 5);
INSERT INTO EMPLOYEE values ('Joyce', 'A', 'English', 453453453, '31-JUL-1972', '5631 Rice,
Houston, TX', 'F', 25000, 333445555, 5);
INSERT INTO EMPLOYEE values ('Ahmad', 'V', 'Jabbar', 987987987, '29-MAR-1969', '980 Dallas,
Houston, TX', 'M', 25000, 987654321, 4);
INSERT INTO EMPLOYEE values ('John', 'B', 'Smith', 123456789, '09-JAN-1965', '731 Fondren,
Houston, TX', 'M', 30000, 333445555, 5);
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
DESC EMPLOYEE
DESC DEPARTMENT
INSERT INTO PROJECT (Pname, Pnumber, Plocation, Dnum) values ('ProductX', 1, 'Bellaire', 5);
INSERT INTO PROJECT (Pname, Pnumber, Plocation, Dnum) values ('ProductY', 2, 'Sugarland', 5);
INSERT INTO PROJECT (Pname, Pnumber, Plocation, Dnum) values ('ProductZ', 3, 'Houston', 5);
INSERT INTO PROJECT (Pname, Pnumber, Plocation, Dnum) values ('Computerization', 10, 'Stafford',
4);
INSERT INTO PROJECT (Pname, Pnumber, Plocation, Dnum) values ('Reorganization', 20, 'Houston',
1);
INSERT INTO PROJECt (Pname, Pnumber, Plocation, Dnum) values ('Newbenfits', 30, 'Stafford', 4);
desc WORKS_ON
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
/*Query 1. Retrieve the name and address of all employees who work for the ‘Research’ department.*/
/*Query 2. For every project located in ‘Stafford’, list the project number, the controlling department
number, and the department manager’s last name, address, and birth date.*/
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
/*Queries 9 and 10. Select all EMPLOYEE Ssns (Q9) and all combinations of EMPLOYEE Ssn and
DEPARTMENT Dname (Q10) in the database.*/
where E.Dno=D.Dnumber;
/* query Q1D retrieves all the attributes of an EMPLOYEE and the attributes of the DEPARTMENT in
which he or she works for every employee of the ‘Research’ department, */
/*Q10A specifies the CROSS PRODUCT of the EMPLOYEE and DEPARTMENT relations.*/
/* Query 11. Retrieve the salary of every employee (Q11) and all distinct salary values (Q11A). */
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
/*Query 12A. Find all employees who were born during the 1950s.*/
/*Query 13. Show the resulting salaries if every employee working on the ‘ProductX’ project is given a
10% raise.*/
SELECT E.Fname, E.Lname, 1.1 * E.Salary AS Increased_sal
FROM EMPLOYEE E, WORKS_ON W, PROJECT P
WHERE E.Ssn = W.Essn AND W.Pno = P.Pnumber AND P.Pname = 'ProductX';
/*Query 14. Retrieve all employees in department 5 whose salary is between $30,000 and $40,000. */
SELECT * FROM EMPLOYEE
WHERE (Salary BETWEEN 30000 AND 40000) AND Dno = 5;
/*Query 15. Retrieve a list of employees and the projects they are working on, ordered by department and,
within each department, ordered alphabetically by last name, then first name.
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
INSERT INTO EMPLOYEE (Fname, Lname, Dno, Ssn) VALUES ('Richard', 'Marini', 4, '653298653');
INSERT INTO EMPLOYEE (Fname, Lname, Ssn, Dno) VALUES ('Robert', 'Hatcher', '980760540', 2);
/*cannot insert NULL into ("DB"."EMPLOYEE"."SSN")( U2A is rejected if NOT NULL checking is
provided by DBMS.)*/
INSERT INTO EMPLOYEE (Fname, Lname, Dno) VALUES ('Robert', 'Hatcher', 5);
* To create a temporary table that has the employee last name, project name, and hours per week for each
employee working on a project*/
CREATE TABLE WORKS_ON_INFO
( Emp_name VARCHAR(15),
Proj_name VARCHAR(15),
Hours_per_week DECIMAL(3,1)
);
desc WORKS_ON_INFO
/*To create a table D5EMPS with a similar structure to the EMPLOYEE table and load it with the rows of
employees who work in department 5, we can write the following SQL: */
CREATE TABLE D5EMPS LIKE EMPLOYEE
SELECT E.* FROM EMPLOYEE E
WHERE E.Dno = 5;
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
/*Query 18. Retrieve the names of all employees who do not have supervisors.*/
SELECT Fname, Lname
FROM EMPLOYEE
WHERE Super_ssn IS NULL;
/*Query 4A. Make a list of all project numbers for projects that involve an employee whose last name is
‘Smith’, either as a worker or as a manager of the department that controls the project.*/
/*SQL allows the use of tuples of values in comparisons by placing them within parentheses. To illustrate
this, consider the following query: */
SELECT DISTINCT Essn
FROM WORKS_ON
WHERE (Pno, Hours)
IN
( SELECT Pno, Hours
FROM WORKS_ON
WHERE Essn = '123456789' );
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
/*An example is the following query, which returns the names of employees whose salary is greater than
the salary of all the employees in department 5:*/
/*An example is the following query, which returns the names of employees whose salary is greater than
the salary of all the employees in department 5: */
/*Query 16. Retrieve the name of each employee who has a dependent with the same first name and is the
same sex as the employee.*/
/*Q16A:*/
SELECT E.Fname, E.Lname
FROM EMPLOYEE E, DEPENDENT D
WHERE E.Ssn = D.Essn AND E.Sex = D.Sex AND E.Fname = D.Dependent_name;
desc DEPENDENT
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
/*The query Q3: Retrieve the name of each employee who works on all the projects controlled by
department number 5*/
/*Query 7. List the names of managers who have at least one dependent.*/
/*In Q3A, the first subquery (which is not correlated with the outer query) selects all projects controlled
by department 5, and the second subquery (which is correlated) selects all projects that the particular
employee being considered works on. If the set difference of the first subquery result MINUS (EXCEPT)
the second subquery result is empty, it means that the employee works on all the projects and is therefore
selected.*/
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
/*The second option is shown as Q3B. Notice that we need two-level nesting in Q3B and that this
formulation is quite a bit more complex than Q3A.*/
SELECT Lname, Fname
FROM EMPLOYEE
WHERE NOT EXISTS
( SELECT * FROM WORKS_ON B
WHERE ( B.Pno IN ( SELECT Pnumber
FROM PROJECT
WHERE Dnum = 5 ) AND NOT EXISTS
( SELECT * FROM WORKS_ON C
WHERE C.Essn = Ssn
AND C.Pno = B.Pno )));
/*Query 17. Retrieve the Social Security numbers of all employees who work on project numbers 1, 2, or
3.*/
SELECT DISTINCT Essn
FROM WORKS_ON
WHERE Pno IN (1, 2, 3);
/*Q8A shows how query Q8 from Section 4.3.2 can be slightly changed to retrieve the last name of each
employee and his or her supervisor while renaming the resulting attribute names as Employee_name and
Supervisor_name. The new names will appear as column headers for the query result.*/
/* To retrieve the last name of each employee and his or her supervisor while renaming the resulting
attribute names as Employee_name and Supervisor_name.*/
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
/*Multiway Join*/
/*Query 19. Find the sum of the salaries of all employees, the maximum salary, the minimum salary, and
the average salary.*/
SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary)
FROM EMPLOYEE;
/*Query 20. Find the sum of the salaries of all employees of the ‘Research’ department, as well as the
maximum salary, the minimum salary, and the average salary in this department.*/
SELECT SUM (Salary),
MAX (Salary),
MIN (Salary),
AVG (Salary) FROM (EMPLOYEE JOIN DEPARTMENT ON Dno = Dnumber)
WHERE Dname = 'Research';
/*Queries 21 and 22. Retrieve the total number of employees in the company (Q21) and the number of
employees in the ‘Research’ department (Q22). */
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
SELECT COUNT (*) FROM EMPLOYEE, DEPARTMENT WHERE DNO = DNUMBER AND
DNAME = 'Research';
/*Query 23. Count the number of distinct salary values in the database.*/
/* For example, to retrieve the names of all employees who have two or more dependents (Query 5), we
can write the following: */
/*Query 25. For each project, retrieve the project number, the project name, and the number of employees
who work on that project.*/
/*Query 26. For each project on which more than two employees work, retrieve the project number, the
project name, and the number of employees who work on the project.*/
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
/*Query 27. For each project, retrieve the project number, the project name, and the number of employees
from department 5 who work on the project.*/
/* the condition (SALARY > 40000) applies only to the COUNT function in the SELECT clause.
Suppose that we write the following incorrect query: */
SELECT Dno, COUNT (*)
FROM EMPLOYEE
WHERE Salary>40000
GROUP BY Dno
HAVING COUNT (*) > 5;
/*Query 28. For each department that has more than five employees, retrieve the department number and
the number of its employees who are making more than $40,000.*/
/* Employees in department 5 get a $2,000 raise, those in department 4 get $1,500 and those in
department 1 get $3,00*/
UPDATE EMPLOYEE
SET Salary = CASE WHEN Dno = 5 THEN Salary + 2000
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
UPDATE WORKS_ON1
SET Pname = 'ProductY'
WHERE Lname = 'Smith' AND Fname = 'John' AND Pname = 'ProductX';
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga
JNNCE ISE-Department
/*Query 8. For each employee, retrieve the employee’s first and last name and the first and last name of
his or her immediate supervisor.*/
desc EMPLOYEE
select Super_ssn,Ssn from EMPLOYEE
Dr.Deepa V B, Asst.Professor,ISE,JNNCE,Shimoga