DBMS 2
DBMS 2
DBMS 2
(Vandana Garia)
Query:
Create table Employee
( EmpNo number(10) Primary Key,
EmpName varchar2(30),
Street varchar2(40),
City varchar2(20));
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');
Query:
Select * from Employee;
Select * from Department;
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;
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;
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 ;
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');