Professional Documents
Culture Documents
Question 1: Create given schema and insert data into mysql? [4 marks]
Table name = Employee
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:
[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 Salary
FROM Employees
WHERE (SELECT COUNT(*) FROM Employees WHERE Salary < Employees.Salary)
= 2;
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.
6. Calculate the annual salary and per day salary of all employees on the basis of
given monthly salary. Show their all details.