You are on page 1of 9

DBMS Assignment-2

(Vandana Garia)

Ques I: Create the relations given below:


EMPLOYEE (EmpNo,EmpName, Street, City)
DEPARTMENT (DeptID, DepartmentName, City) Record-{10,'Finance','Delhi'}
WORKS (EmpNo, DeptID, Salary)
MANAGES (EmpNo, ManagerID)
Create above relations using your own data types and constraints of the each relation.

Query:
Create table Employee
( EmpNo number(10) Primary Key,
EmpName varchar2(30),
Street varchar2(40),
City varchar2(20));

Create table Department


(DeptID number(10) Primary Key,
DepartmentName varchar2(20),
City varchar2(20)
);

Create table Works


(EmpNo number(10) references Employee(EmpNo),
DeptID number(10) references Department(DeptID),
Salary number(10)
);

Create table Manages


(EmpNo number(10) references Employee(EmpNo),
ManagerID number(10)
);

Ques II: Insert at least 10 records in the relation EMPLOYEE and 4 records in the relation
DEPARTMENT(Finance, Sales, Accounts and Inventory) . Insert appropriate records in the relations
WORKS and MANAGES.

Query:
Insert into Employee values(1001,'Drishti','ABC','Delhi');
Insert into Employee values(1002,'Ram','DEF','Kolkata');
Insert into Employee values(1003,'Yashi','GHI','Delhi');
Insert into Employee values(1004,'Shyam','JKL','Jammu');
Insert into Employee values(1005,'Manisha','MNO','Jaipur');
Insert into Employee values(1006,'Devi','PQR','Dhanbad');
Insert into Employee values(1007,'Rohit','STU','Delhi');
Insert into Employee values(1008,'Sita','VWX','Mumbai');
Insert into Employee values(1009,'Dev','XYZ','Chennai');
Insert into Employee values(1010,'Tia','ACD','Mumbai');
Insert into Department values(201,'Finance','Delhi');
Insert into Department values(202,'Sales','Dhanbad');
Insert into Department values(203,'Accounts','Jaipur');
Insert into Department values(204,'Inventory','Mumbai');

Insert into Works values(1001,201,20000);


Insert into Works values(1002,201,20000);
Insert into Works values(1003,201,30000);
Insert into Works values(1004,202,40000);
Insert into Works values(1005,202,20000);
Insert into Works values(1006,202,30000);
Insert into Works values(1007,203,20000);
Insert into Works values(1008,204,45000);
Insert into Works values(1009,204,43000);
Insert into Works values(1010,203,45000);

Insert into Manages values(1001,301);


Insert into Manages values(1002,301);
Insert into Manages values(1003,301);
Insert into Manages values(1004,302);
Insert into Manages values(1005,302);
Insert into Manages values(1006,302);
Insert into Manages values(1007,303);
Insert into Manages values(1008,304);
Insert into Manages values(1009,304);
Insert into Manages values(1010,303);

Ques III:Print the contents of the each relation.

Query:
Select * from Employee;
Select * from Department;

Select * from Works;


Select * from Manages;

Ques IV: Give an expression in SQL with output for each of the following queries:

1.Find the names of all employees who work for Finance Department
Query:
Select EmpName,DepartmentName from Employee ,Department,Works where
Works.EmpNo=Employee.EmpNo and Works.DeptID='201' and DepartmentName='Finance';

2.Find the names and cities of residence of all employees who work for the Finance Department.

Query:
Select Employee.EmpName,Employee.City,Department.DepartmentName from Employee
,Department,Works where Works.EmpNo=Employee.EmpNo and
Works.DeptID=Department.DeptID and DepartmentName='Finance';

3.Find the names, street, and cities of residence of all employees who work for Finance
Department and earn more than Rs. 10,000/-.

Query:
Select Employee.EmpName,Employee.Street,Employee.City from Employee ,Department,Works
where Works.EmpNo=Employee.EmpNo and Works.DeptID='201' and Works.Salary>='10000' and
DepartmentName='Finance';

4.Find the employees who live in the same cities as the Departments for which they work.
Query:
Select Employee.EmpName,Employee.City from Employee,Department where
Employee.City=Department.City;

5.Find all employees who live in the same cities and on the same streets as do their managers.

Query:
select empname,manages.managerid,employee.city,employee.street,manager.city as
managercity,manager.street as managerstreet from employee,manages,(select city,street from
employee,manages where manages.managerid=employee.empno group by street,city ) manager
where manages.empno=employee.empno and manager.city=employee.city and
manager.street=employee.street;

6.Find all employees who do not work for Finance Department.

Query:
Select Employee.EmpName,Employee.City,Department.DepartmentName from Employee
,Department,Works where Works.EmpNo=Employee.EmpNo and
Works.DeptID=Department.DeptID and DepartmentName!='Finance';
7.Find all employees who earn more than every employee of Sales Department

Query:
Select Employee.EmpName,Works.Salary from Employee,Department,Works where
Works.EmpNo=Employee.EmpNo and Works.DeptID=Department.DeptID and Works.Salary>(Select
Max(Works.Salary) from Works,Department where Works.DeptID=Department.DeptID and
Department.DepartmentName='Sales');

8.Find all Departments located in every city in which Sales Department is located.

Query:
Select Department.DeptID,Department.DepartmentName,Department.City from Department,(Select
Department.city from Department where DepartmentName='Sales') as Sale_City where
Department.City=Sale_City.city;

9.Find all employees who earn more than the average salary of all employees of their Department.
Query:
Select Employee.EmpName,Department.DepartmentName from
Department,Works,employee,(select Departmentname,avg(salary) Sal from
Works,Department,Employee where Works.deptID=Department.deptID and
Employee.empno=Works.empno group by Departmentname) avgsal where
Works.deptID=Department.deptID and Works.empno=Employee.empno and
Department.departmentname=Avgsal.departmentname and salary>avgsal.sal;

10.Find the Department that has the most employees.

Query:
Select Departmentname, Count(*) as Count from Works,Department,Employee where
Works.DeptID=Department.deptID and Works.Empno=Employee.empno group by departmentname
order by count desc fetch first 1 rows only ;

11.Find the Department that has the smallest payroll.

Query:
Select department.deptid,department.departmentname,salary from department,works where
works.deptid=department.deptid and salary=(select min(salary) from department,works where
works.deptid=department.deptid);
12.Find those Departments whose employees earn a higher salary, on average, than the average
salary at Finance Department.

Query :
Select distinct department.deptid,department.departmentname,avgsalary.sal from
department,works,(select departmentname,avg(salary) as sal from department,works where
department.deptid=works.deptid and departmentname!='Finance' group by departmentname)
avgsalary where works.deptid=department.deptid and
department.departmentname=avgsalary.departmentname and
avgsalary.sal>(select avg(salary) from department,works where department.deptid=works.deptid
and departmentname='Finance');

You might also like