You are on page 1of 5

Student Name: _________________________ Student ID: _______________________

Instructor’s Name: Ms. Mamoona Majid Paper: Database Systems _______

Date: 19-05-2022________________ Section: Y4____________________

Time Allowed: 2 hours 0 minutes________________ Total Marks: 40_____________________

Instructions for Candidates:


1. Mention your Name and ID.
2. Just focus on your own paper, discussion is strictly prohibited.
3. Make sure you only mark one answer for each question. Answer shall not be valid if more
than one option is marked. Any question not attempted will be marked as incorrect.
4. Question paper must be returned after 15 Minutes. Cutting and overwriting is not
allowed. Best of Luck

Question 1: Create given schema and insert data into mysql? [4 marks]
Table name = Employee

EmpI EmpFnam EmpLnam Departme Projec Gende


Address DOB
D e e nt t r

Hyderabad(HYD 01/12/197
1 Sanjay Mehra HR P1 M
) 6

02/05/196
2 Ananya Mishra Admin P2 Delhi(DEL) F
8

01/01/198
3 Rohan Diwan Account P3 Mumbai(BOM) M
0

Hyderabad(HYD 02/05/199
4 Sonia Kulkarni HR P1 F
) 2

03/07/199
5 Ankit Kapoor Admin P2 Delhi(DEL) M
4

EmployeePosition Table:

EmpID EmpPosition DateOfJoining Salary


1 Manager 01/05/2022 500000

2 Executive 02/05/2022 75000

3 Manager 01/05/2022 90000

2 Lead 02/05/2022 85000

1 Executive 01/05/2022 300000

[18 marks]
1. Write a query to find all the employees whose salary is between 30000 to 70000
using subquery.
SELECT * FROM Employee
WHERE EmpID IN (SELECT EmpID FROM EmployeePosition WHERE Salary
BETWEEN 30000 AND 70000)
2. Write a query to find the names of employees that begin with ‘S.
SELECT EmpFname, EmpLname FROM Employee
WHERE EmpFname LIKE 'S%'
3. Write a query to retrieve the EmpFname and EmpLname in a single column as
“FullName”. The first name and the last name must be separated with space.
SELECT EmpFname || ' ' || EmpLname AS "FullName" FROM Employee
4. Insert the above query result into another table (create new one).
CREATE TABLE FullName AS
SELECT EmpFname || ' ' || EmpLname AS "FullName" FROM Employee
5. Write a query find number of employees whose DOB is between 02/05/1970 to
31/12/1975 and are grouped according to gender.
SELECT Gender, COUNT(*) FROM Employee
WHERE DOB BETWEEN '1970-05-02' AND '1975-12-31'
GROUP BY Gender
6. Write a query to retrieve the list of employees working in the same department.
SELECT a.EmpID, a.EmpFname, a.EmpLname, a.Department
FROM Employee a
INNER JOIN Employee b
ON a.Department = b.Department
WHERE a.EmpID != b.EmpID
7. Write a query to retrieve Departments who have no employees working in it using
aggregate function.
SELECT Department, COUNT(*) FROM Employee
GROUP BY Department
HAVING COUNT(*) = 0
8. Write a SQL query to fetch common records between two tables?
SELECT * FROM Employee
WHERE EmpID IN (SELECT EmpID FROM EmployeePosition)
9. Write a query to display the first and the last record from the EmployeeInfo table.
SELECT * FROM Employee
WHERE EmpID IN (
SELECT EmpID FROM Employee
ORDER BY EmpID
LIMIT 1, 1
)
UNION
SELECT * FROM Employee
WHERE EmpID IN (
SELECT EmpID FROM Employee
ORDER BY EmpID DESC
LIMIT 1, 1
)

[18 marks]

1. Create 2 separate views having all the employees at manager level and executive
level.
CREATE VIEW Manager_Level AS

SELECT EmpID, EmpFname, EmpLname, Department, Project, Address, DOB, Gender,


EmpPosition, DateOfJoining, Salary
FROM Employees
WHERE EmpPosition = 'Manager';

CREATE VIEW Executive_Level AS


SELECT EmpID, EmpFname, EmpLname, Department, Project, Address, DOB, Gender,
EmpPosition, DateOfJoining, Salary
FROM Employees
WHERE EmpPosition = 'Executive';
2. Write a query to retrieve third minimum salaries from the EmployeePosition table
using subquery.

SELECT Salary
FROM Employees
WHERE (SELECT COUNT(*) FROM Employees WHERE Salary < Employees.Salary)
= 2;

3. Write a query to retrieve duplicate records from a table.

SELECT EmpID, EmpFname, EmpLname, Department, Project, Address, DOB, Gender,


EmpPosition, DateOfJoining, Salary, COUNT(*)
FROM Employees
GROUP BY EmpID, EmpFname, EmpLname, Department, Project, Address, DOB,
Gender, EmpPosition, DateOfJoining, Salary
HAVING COUNT(*) > 1;

4. Write a query to retrieve Departments who have less than 2 employees working in
it.

SELECT Department
FROM Employees
GROUP BY Department
HAVING COUNT(*) < 2;

5. Write a SQL query to identify those employees whose salaries exceed 3000 after
receiving a 25% salary increase. Return complete information about the employees.

SELECT EmpID, EmpFname, EmpLname, Department, Project, Address, DOB, Gender,


EmpPosition, DateOfJoining, Salary
FROM Employees
WHERE Salary * 1.25 > 3000;

6. Calculate the annual salary and per day salary of all employees on the basis of
given monthly salary. Show their all details.

SELECT EmpID, EmpFname, EmpLname, Department, Project, Address, DOB, Gender,


EmpPosition, DateOfJoining, Salary, Salary * 12 AS Annual_Salary, (Salary * 12) / 365
AS Per_Day_Salary
FROM Employees;

You might also like