0% found this document useful (0 votes)
2 views

Assignment 7.

The document outlines a database management system assignment focused on creating and querying a company's employee database. It includes SQL commands for creating tables for employees, bonuses, and job titles, along with various queries to retrieve specific information from these tables. The assignment covers a range of SQL operations, including selection, aggregation, and joins to manipulate and analyze the data.

Uploaded by

bmsatyam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 7.

The document outlines a database management system assignment focused on creating and querying a company's employee database. It includes SQL commands for creating tables for employees, bonuses, and job titles, along with various queries to retrieve specific information from these tables. The assignment covers a range of SQL operations, including selection, aggregation, and joins to manipulate and analyze the data.

Uploaded by

bmsatyam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Database Management System (LAB)

Assignment – 7

Name: Satyam Dwivedi Reg. No.: 2020CA081

Q 1. Consider a database that is being constructed for a Company’s office


organization where it stores the value of Employees, their Bonus and Job title. The
employee relation contains the details of id, first name, last name, salary, joining
date of office and the department in which they are working. The Bonus relation
contains the details of employee reference id, bonus date and amount of bonus.
Title relate to bonus table through the reference id and this table stores the value of
employer designation and their job affected from which date.

Consider the sales info system consisting of following schemas: -

Employee (E_id, First_Name, Last_Name, Salary, Joining_Date, Department)


Bonus (E_Ref_Id, Bonus_Date, Bonus_Amount)
Job_Title (E_Ref_Id, E_Title, Affected_From)

Write the SQL queries for the following with respect to database created: –

Employee (E_id, First_Name, Last_Name, Salary, Joining_Date, Department)

CREATE TABLE employee (


E_id varchar(10) Primary Key Not Null,
First_Name varchar(20) Not Null,
Last_Name varchar(20) Not Null,
Salary numeric(10,2) Not Null,
Joining_Date Date Not Null, Department
varchar(10) Not Null
);
INSERT INTO employee values
("E6","Manish","Kumar","200000","2014-02-11","Administration"),
("E3","Himanshu","Singh","30000","2014-03-23","Clerk"),
("E9","Mohan","Gupta","150000","2014-02-14","Training"),
("E20","Pankaj","Dubey","400000","2014-12-17","Management"),
("E7","Arun","Kumar","30000","2014-11-10","Training");
Bonus (E_Ref_Id, Bonus_Date, Bonus_Amount)

CREATE TABLE bonus (


E_Ref_Id varchar(10) Primary Key Not Null,
Bonus_Date Date Not Null,
Bonus_Amount numeric(10,2) Not Null
);

INSERT INTO bonus values


("R3","2014-10-12","35000"),
("R1","2014-12-11","12000"),
("R9","2014-03-12","32000");
Job_Title (E_Ref_Id, E_Title, Affected_From)

CREATE TABLE job_title(


E_Ref_Id varchar(10) Primary Key Not Null,
E_Title varchar(10) Not Null,
Affected_From Date Not Null
);

INSERT INTO job_title values


("R3","Clerk","2014-04-12"),
("R1","Manager","2014-12-12"),
("R7","Trainer","2014-12-02");
Tables: -

Employee

Bonus

Job_Title
a) Fetch first name of worker using alias name as E_Name.

SELECT First_Name AS E_Name FROM employee;

b) List the last name of employees in upper case.

SELECT UPPER(Last_Name) FROM employee;


c) Retrieve the first three characters of employees from their first name.

SELECT SUBSTRING(First_Name,1,3) FROM employee;

d) Retrieve the unique values of department and display its length.

SELECT DISTINCT Department, LENGTH(Department) FROM employee;


e) List the first name from employees table after replacing ‘a’ with ‘A’.

SELECT First_Name, REPLACE(First_Name,'a','A') FROM employee;

f) Display all worker details, use order by in first name asec and department in desc.

SELECT * FROM employee ORDER BY employee.Department DESC;


g) List the details of an employee whose first name ends with ‘h’ and contains six
alphabets.

SELECT * FROM employee WHERE First_Name LIKE ' h';

h) Display the details of employees who have joined in Feb 2014.

SELECT * FROM employee WHERE Joining_Date LIKE "2014-02-%";


i) Fetch the employee’s names with salaries over and equal to 50000 and less than
equal to 100000.

SELECT First_Name, Last_Name FROM employee WHERE Salary >= 500000 AND Salary <
1000000;

j) List the no. of employees for each department in desc order.

SELECT * FROM employee ORDER BY Department DESC;


k) Print the details of employees who are also managers.

SELECT * FROM `employee` WHERE Department="Management";

l) Fetch the duplicate records having matching data in some fields of a table.

SELECT First_Name,Last_Name,Department, COUNT(*) FROM employee GROUP BY Department


HAVING COUNT(*) > 1;
m) Fetch intersecting records of two tables.

SELECT * FROM bonus INNER JOIN job_title ON bonus.E_Ref_Id=job_title.E_Ref_Id;

n) Find the nth (say n=5) highest salary from a table.

SELECT salary FROM employee ORDER BY Salary DESC LIMIT 4,1;


o) Find the 4th highest salary without using TOP or limit method.

SELECT * FROM employee emp1 WHERE (4-1) = (SELECT COUNT(DISTINCT(emp2.Salary))


FROM employee emp2 WHERE emp2.Salary > emp1.Salary);

p) List the details of employees with the same salary.

SELECT * FROM employee WHERE Salary IN (SELECT Salary FROM employee GROUP BY
Salary HAVING COUNT(*) > 1) ORDER BY Department DESC;
q) Display the second highest salary from a table.

SELECT First_Name,Last_Name,Salary FROM employee ORDER BY Salary DESC LIMIT 1,1;

r) Display all departments along with the number of people in there.

SELECT Department, COUNT(*) FROM employee GROUP BY Department;


s) List the name of employees having the highest salary in each department.

SELECT First_Name,Last_Name, MAX(Salary) FROM employee GROUP BY Department;

t) Fetch three min salaries from a table

SELECT DISTINCT Salary FROM employee emp1 WHERE 3 >= (SELECT COUNT(DISTINCT
Salary) FROM employee emp2 WHERE emp2.Salary <= emp1.Salary) ORDER BY emp1.Salary
DESC;
u) Fetch departments along with the total salaries paid for each of them.

SELECT Department, SUM(Salary) FROM employee GROUP BY Department;

v) Fetch the names of employees who earn the highest salary.

SELECT First_Name,Last_Name,Salary FROM employee ORDER BY Salary DESC LIMIT 0,1;

You might also like