DBMS Lab Assignment 3 (SQL)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

DBMS (CS-612)

ASSIGNMENT - 3

Name :- Birender Singh


Roll.no. :- CSE/19021/448

DBMS Lab Assignment 3 (SQL)

1. Write a query to list all employees (Fname and Lname) who work in the
ADMINISTRATION department, and whose managers' name starts with ‘M.’

Soln.
SELECT Fname, Lname
FROM Employee
WHERE Ssn
IN
(

SELECT Mgr_ssn
FROM Department
WHERE Department = 'Administration'
AND Mgr_ssn IN
(
SELECT Super_ssn
FROM Employee
WHERE Fname LIKE 'M%'
); )

2. Write a query to find all employees (Fname and Lname) who have at least one
dependent born between 01.01.2010 and 01. 01. 2014.
DBMS (CS-612)
ASSIGNMENT - 3

Soln. SELECT Fname, Lname


FROM Employee
WHERE Ssn IN
(
SELECT Essn
FROM Dependent
WHERE Bdate BETWEEN '2010-01-01' AND '2014-01-01'
);

3. Write a query to output the number of employees whose names and their
managers’ names start with any alphabet from {‘A’, ‘B’, ‘C’, ‘D’}.

Soln. SELECT Fname, Lname


FROM Employee
WHERE
(Fname REGEXP '^[ABCD]')
AND
Super_ssn IN
(
SELECT Ssn
FROM Employee
WHERE
Fname REGEXP
'^[ABCD]'
);

4. Write a query to list all employees (Fname and Lname) who do not work in the
ADMINISTRATION department and whose names do not start with ‘M’.
DBMS (CS-612)
ASSIGNMENT - 3

Soln. SELECT Fname, Lname


FROM Employee
WHERE Ssn
IN
(
SELECT Mgr_ssn
FROM Department
WHERE Department <> 'Administration'
)
AND Fname NOT LIKE
'M%';

5. Write a query to list all male employees (Fname and Lname) who work in the
`SEMICONDUCTOR` project in KOLKATA under the RESEARCH department.
DBMS (CS-612)
ASSIGNMENT - 3

Soln. SELECT Fname, Lname


FROM Employee
WHERE Sex = 'M' AND
(
Ssn IN
(
SELECT Mgr_ssn
FROM Department
WHERE Department =
'Research' AND
(
Dnumber IN
(
SELECT
Dnum
FROM Project
WHERE Pname =
'SEMICONDUCTOR' AND
Plocation = 'KOLKATA'
)
)
)
);

6. Write a query to list the number of employees who work in the ADMINISTRATION
department in the MANAGER POST; locations should make groups to show the
results.
DBMS (CS-612)
ASSIGNMENT - 3

Soln. SELECT COUNT(Ssn) AS 'No. of Employees in Administration'


FROM Employee
WHERE Ssn
IN
(
SELECT Department.Mgr_ssn
FROM Department, Dept_Locations
WHERE Department.Department = 'Administration'
GROUP BY Dept_Locations.Dnumber
);

7. Write a query to list the names of all employees who work in the
ADMINISTRATION department in the MANAGER post; the result should be
grouped by sex first and then by locations.

Soln. SELECT DISTINCT Fname, Lname


FROM Employee, Dept_Locations
WHERE Ssn
IN
(
SELECT Department.Mgr_ssn
FROM Department
WHERE Department.Department =
'Administration'
)
GROUP BY Employee.Sex, Dept_Locations.Dlocation;
DBMS (CS-612)
ASSIGNMENT - 3

8. Write a query to list all the employees who are less than 45 years of age but are
managers. Now, generate the complement of the previous output.

Soln. SELECT Fname, Lname


FROM employee
WHERE truncate(sysdate(), 'YEAR') - truncate(Bdate,
'YEAR') < 45
AND Ssn IN
(
SELECT Mgr_ssn
FROM department
);

SELECT Fname, Lname


FROM employee
WHERE truncate(sysdate(), 'YEAR') - truncate(Bdate,
'YEAR') > 45
AND Ssn NOT IN
(
SELECT Mgr_ssn
FROM department
);

9. Write a query to generate a report about the company's cost for salary purposes
department-wise and then location-wise.
DBMS (CS-612)
ASSIGNMENT - 3

Soln. SELECT e.Fname, e.Lname, e.Ssn, e.Dno, d.Department, dl.Dlocation, e.Salary FROM
employee e
LEFT JOIN department d
ON e.Dno = d.Dnumber
LEFT JOIN dept_locations dl
ON e.Dno = dl.Dnumber
GROUP BY d.Department, dl.Dlocation;

10. Write a query to list the Fname, Lname of all employees who work in a project
where at least four employees are attached.

Soln. SELECT employee.Fname, employee.Lname, project.Pnumber,


COUNT(employee.Dno) as Team_Size
FROM employee, project
WHERE employee.Dno = project.Dnum
GROUP BY employee.Dno HAVING Team_Size > 3;

11. List the employees who do not belong to department name RESEARCH.
DBMS (CS-612)
ASSIGNMENT - 3

Soln. SELECT Fname, Lname


FROM employee
WHERE employee.Dno IN
(
SELECT Dnumber
FROM department
WHERE Department <> 'Research'
);

12. Select the employees getting a salary greater than the average salary of all the
employees in that department.

Soln. SELECT e.Fname, e.Lname, d.Department, e.Salary, AVG(e.Salary) AS Avg_Salary


FROM employee e
LEFT JOIN department d
ON e.Dno = d.Dnumber
GROUP BY d.Department HAVING e.Salary > AVG(e.Salary)

13. Write a query to find the department with the highest number of employees.
DBMS (CS-612)
ASSIGNMENT - 3

Soln. SELECT Department


FROM department
JOIN employee
ON department.Dnumber = employee.Dno
GROUP BY Department
ORDER BY COUNT(*) DESC
LIMIT 1;

14. Write a query to list all project numbers involving an employee whose last name
is 'Narayan’ either as a worker or as a department manager who controls the
project.

Soln. SELECT Pno


FROM works_on
WHERE Essn IN
(
SELECT Ssn
FROM employee
WHERE Lname = 'Narayan'
);
DBMS (CS-612)
ASSIGNMENT - 3

15. Output the location and department number for all projects having more than five
employees of age greater than or equal to 50 Yrs.

Soln. SELECT p.Plocation, p.Dnum


FROM project p
LEFT JOIN works_on w
ON p.Pnumber = w.Pno
LEFT JOIN employee e
ON w.Essn = e.Ssn
GROUP BY p.Pnumber HAVING
(
COUNT(truncate(sysdate(), 'YEAR') - truncate(sysdate(), 'YEAR')) > 5
);
DBMS (CS-612)
ASSIGNMENT - 3

Database Schemas and Tables

Employee –
CREATE TABLE Employee(
Fname VARCHAR(20),
Minit CHAR,
Lname VARCHAR(20),
Ssn BIGINT PRIMARY KEY,

Bdate DATE,
Address TINYTEXT,
Sex CHAR,
Salary MEDIUMINT,
Super_ssn BIGINT,
Dno CHAR
);

INSERT INTO Employee


VALUES
('John', 'B', 'Smith', 123456789, '1965-01-09', '731 Fondren, Houston, TX', 'M', 30000,
333445555, '5'),
('Franklin', 'T', 'Wong', 333445555, '1955-12-08', '638 Voss, Houston, TX', 'M', 40000,
888665555, '5'),
('Alicia', 'J', 'Zelaya', 999887777, '1968-01-19', '3321 Castle, Spring, TX', 'F', 25000,
987654321, '4'),
('Jennifer', 'S', 'Wallace', 987654321, '1941-06-20', '291 Berry, Bellaire, TX', 'F', 43000,
888665555, '4'),
('Ramesh', 'K', 'Narayan', 666884444, '1962-09-15', '975 Fire Oak, Humble, TX', 'M',
38000,
333445555, '5'),
('Joyce', 'A', 'English', 453453453, '1972-07-31', '5631 Rice, Houston, TX', 'F', 25000,
333445555, '5'),
('Ahmed', 'V', 'Jabbar', 987987987, '1969-03-29', '980 Dallas, Houston, TX', 'M', 25000,
987654321, '4'),
DBMS (CS-612)
ASSIGNMENT - 3

('James', 'E', 'Borg', 888665555, '1937-11-10', '450 Stone, Houston, TX', 'M', 55000, NULL,
'1');

Department –
CREATE TABLE Department(
Department VARCHAR(20),
Dnumber CHAR PRIMARY KEY,
Mgr_ssn BIGINT,
Mgr_start_date DATE
);

INSERT INTO Department

VALUES
('Research', '5', 333445555, '1988-05-22'),
('Administration', '4', 987654321, '1995-01-01'),
('Headquarters', '1', 888665555, '1981-06-19');

Dept_Locations – CREATE
TABLE Dept_Loacations(
Dnumber CHAR,
DBMS (CS-612)
ASSIGNMENT - 3

Dlocation VARCHAR(10)
);

INSERT INTO Dept_Loacations


VALUES
('1', 'Houston'),
('4', 'Stafford'),
('5', 'Bellaire'),
('5', 'Sugarland'),
('5', 'Houston');

Works_On – CREATE
TABLE Works_On(
Essn BIGINT,
Pno CHAR(2),
Hours DECIMAL(3, 1)
);

INSERT INTO Works_On


VALUES
(123456789, '1', 32.5), (123456789, '2', 7.5),
(666884444, '3', 40.0),
(453453453, '1', 20.0),

(453453453, '2', 20.0),


(333445555, '2', 10.0),
(333445555, '3', 10.0),
DBMS (CS-612)
ASSIGNMENT - 3

(333445555, '10', 10.0),


(333445555, '20', 10.0),
(999887777, '30', 30.0),
(999887777, '10', 10.0),
(987987987, '10', 35.0),
(987987987, '30', 5.0),

(987654321, '30', 20.0),


(987654321, '20', 15.0),
(888665555, '20', NULL);

Project –
CREATE TABLE Project(

Pname VARCHAR(20),
Pnumber CHAR(2) PRIMARY KEY,
Plocation VARCHAR(10),
Dnum CHAR
DBMS (CS-612)
ASSIGNMENT - 3

);

INSERT INTO Project


VALUES
('ProductX', '1', 'Bellaire', '5'),
('ProductY', '2', 'Sugarland', '5'),
('ProductZ', '3', 'Houston', '5'),
('Computerization', '10', 'Stafford', '4'),
('Reorganization', '20', 'Houston', '1'),
('Newbenefits', '30', 'Stafford', '4');

Dependent –
CREATE TABLE Dependent(
Essn BIGINT,
Dependent_name VARCHAR(15),

Sex CHAR,
Bdate DATE,
Relationship VARCHAR(10)
);

INSERT INTO Dependent


DBMS (CS-612)
ASSIGNMENT - 3

VALUES
(333445555, 'Alice', 'F', '1986-04-05', 'Daughter'),
(333445555, 'Theodore', 'M', '1983-10-25', 'Son'),
(333445555, 'Joy', 'F', '1958-05-03', 'Spouse'),
(987654321, 'Abner', 'M', '1942-02-28', 'Spouse'),
(123456789, 'Michael', 'M', '1988-01-04', 'Son'),
(123456789, 'Alice', 'F', '1988-12-30', 'Daughter'),
(123456789, 'Elizabeth', 'F', '1967-05-05', 'Spouse');

You might also like