You are on page 1of 9

Assignment 2

Name: Paras Jain


Branch & Section: IT A
Roll No: 20I4040
Subject Code: ITR4C4
Subject Name: DBMS

SOLUTION1:

--  Insert <‘Robert’, ‘F’, ‘Scott’, ‘943775543’, ‘1972-06-21’, ‘2365 Newcastle Rd,


Bellaire, TX’, M, 58000, ‘888665555’, 1> into EMPLOYEE.

INSERT INTO EMPLOYEE VALUES ("Robert", "F", "Scott", "943775543", "1972-06-21", "2365
Newcastle Rd, Bellaire, TX", "M", 58000, "888665555", 1);

SOLUTION2:

--  Insert <"ProductA", 4, "Bellaire", 2> into PROJECT.


INSERT INTO PROJECT VALUES ("ProductA", 4, "Bellaire", 2);

SOLUTION3:

-- Insert <"Production", 4, "943775543", "2007-10-01"> into DEPARTMENT.


INSERT INTO DEPARTMENT VALUES ("Production", 4, "943775543", "2007-10-01");

Note: The above query will give an error, as dnumber field in DEPARTMENT table is primary key hence it
cannot have duplicate values. As entry with dnumber 4 already exist the entry with same dnumber cannot
be inserted again.
SOLUTION4:

-- Insert <"677678989", NULL, "40.0"> into WORKS_ON.


INSERT INTO WORKS_ON VALUES ("677678989", NULL, "40.0");

Note: The above query will again give an error, as pno field in WORKS_ON table set to not null, hence we
cannot insert null value to pno.

SOLUTION5:

-- Insert <"453453453", "John", "M", "1990-12-12", "spouse"> into DEPENDENT.


INSERT INTO DEPENDENT VALUES ("453453453", "John", "M", "1990-12-12", "spouse");
SOLUTION6:

-- Delete the WORKS_ON tuples with Essn = ‘333445555’


DELETE FROM WORKS_ON WHERE Essn = "333445555";

SOLUTION7:

-- Delete the EMPLOYEE tuple with Ssn = ‘987654321’.


DELETE FROM EMPLOYEE WHERE Ssn = "987654321";

SOLUTION8:

--  Delete the PROJECT tuple with Pname = ‘ProductX’.


DELETE FROM PROJECT WHERE Pname = "ProductX";

SOLUTION9:

-- Modify the Mgr_ssn and Mgr_start_date of the DEPARTMENT tuple with Dnumber = 5 to
-- ‘123456789’ and ‘2007-10-01’, respectively.

UPDATE DEPARTMENT SET mgrssn = "123456789", mgrstartdate = "2007-10-01" WHERE Dnumber = 5;


SOLUTION10:

-- Modify the Super_ssn attribute of the EMPLOYEE tuple with Ssn =‘999887777’ to
-- ‘943775543’.
UPDATE EMPLOYEE SET superssn = "943775543" WHERE ssn = "999887777";
SOLUTION11:

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

SOLUTION12:

-- 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
WHERE ssn = (SELECT mgrssn FROM DEPARTMENT WHERE dname = "Research");

SOLUTION13:

-- Retrieve the total number of employees in the company and the number of employees in
the
-- ‘Research’ department.
SELECT COUNT(ssn) FROM EMPLOYEE UNION ALL
SELECT COUNT(mgrssn) FROM DEPARTMENT WHERE dname = "Research";
SOLUTION14:

-- Retrieve the Social Security numbers of all employees who work on project numbers 1, 2,
or 3
SELECT ssn FROM EMPLOYEE WHERE
ssn = (SELECT mgrssn FROM DEPARTMENT WHERE dnumber IN (1,2,3));

SOLUTION15:

--  Retrieve the names of all employees who do not have supervisors.


SELECT COUNT(ssn) FROM EMPLOYEE WHERE superssn = NULL;

SOLUTION16:

--  Retrieve the names of employees who have no dependents.


SELECT fname,minit,lname FROM EMPLOYEE WHERE ssn NOT IN (SELECT essn FROM DEPENDENT);
SOLUTION17:

--  Retrieve the name of each employee who has a dependent with the same first name and is
-- the same sex as the employee.
SELECT fname,minit,lname FROM EMPLOYEE WHERE ssn
IN (SELECT essn FROM DEPENDENT WHERE EMPLOYEE.fname = DEPENDENT.dependent_name
AND EMPLOYEE.sex = DEPENDENT.sex );

SOLUTION18:

-- List the names of managers who have at least one dependent.


SELECT fname,minit,lname FROM EMPLOYEE WHERE ssn NOT IN (SELECT essn FROM DEPENDENT);
SOLUTION19:

-- For each department, retrieve the department number, the number of employees in the
-- department, and their average salary.
SELECT dep.dnumber, COUNT(*) total_employees, AVG(emp.salary) e_avg_salary
FROM EMPLOYEE emp, DEPARTMENT dep WHERE emp.dno = dep.dnumber
GROUP BY dep.dnumber;

SOLUTION20:

-- 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.
SELECT pro.pnumber, pro.pname, COUNT(*) no_of_employees FROM PROJECT pro, WORKS_ON work
WHERE work.pno=pro.pnumber GROUP BY pro.pnumber, pro.pname
HAVING COUNT(*)>2 ORDER BY pro.pnumber;

You might also like