You are on page 1of 7

Mohan Aggarwal MCA - 1 B

ASSIGNMENT-2

Consider the relation given below:

EMPLOYEE (EmpolyeeID,EmployeeName,Street,City)
COMPANY (CompanyID,CompanyName,City)
WORKS (EmployeeID,CompanyID,Salary)
MANAGES (EmployeeID,ManagerID)

1. Create above relations using your own data types and print the structure of the each
relation.
Mohan Aggarwal MCA - 1 B

2.Insert at least 10 records in the relation EMPLOYEE and 5 records in the relation
COMPANY. Insert appropriate records in the relations WORKS and MANAGES.
3. Print the contents of the each relation.
Mohan Aggarwal MCA - 1 B

4. Give an expression in SQL with output for each of the following queries:

Question1 : Find the names of all employee who work for First Bank Corporation.
Answer: Select EmpName from Employee,Company,Works
Mohan Aggarwal MCA - 1 B

Where Employee.EmpId=Works.EmpId
And Company.CmpId=Works.CmpId
And CmpName=”FirstBankCrp”;

Question 2: Find the names and cities of residence of all employees who for the First
Bank Corporation.
Answer: Select EmpName,City,CmpName from Employee,Works,Company where
Employee.EmpId=Works.EmpId And Company.CmpId=Works.CmpId and
CmpName="FirstBankCrp";

Question 3: Find the name, street and cities of residence of all employees who for the
First Bank Corporation and earn more than RS 10,000/-
Answer: Select EmpName,City,Street from Employee e,Works w,Company c where
e.EmpId=w.EmpId And c.CmpId=w.CmpId and CmpName="FirstBankCrp" and
Salary>10000;

Question 4: Find the employees who live in the same cities as the companies for which
they work.
Answer: Select EmpName from Employee,Works,Company where
Employee.EmpId=Works.EmpId And Company.CmpId=Works.CmpId and
Employee.City=Company.CmpCity;
Mohan Aggarwal MCA - 1 B

Question 5: Find the employee who live in the same cities and on the same streets as do
their managers.
Answer: Select EmpName from Employee e join Manages m On e.EmpId=m.EmpId where
e.city=city and e.street=street;

Question 6: Find all employee who do not work for First Bank Corporation.
Answer: Select EmpName from Employee e,Company c,Works w where e.EmpId=w.EmpId
and c.CmpId=w.CmpId and CmpName!="FirstBankCrp";

Question 7: Find all employee who earn more than every employee of Small Bank
Corporation.
Answer: Select EmpName from Employee e,Works w,Company c where
e.Empid=w.EmpId and c.CmpId=w.CmpId and Salary>(Select Max(salary) from
works,Company where CmpName="SmallBankCrp" and Works.CmpId=Company.Cmpid);
Mohan Aggarwal MCA - 1 B

Question 8: Find all companies located in every city in which Small Bank Corporation
is located.
Answer: Select CmpName from Company c where CmpCity In(Select CmpCity from
Company c where c.CmpName="SmallBankCrp" and c.CmpId!=CmpId);

Question 9: Find all employees who earn more than the average salary of all employees
of their company.
Answer: Select EmpName from Employee e,Works w,Company c where e.EmpId=w.EmpId
and c.CmpId=w.CmpId and Salary>(Select avg(salary) from Works where
CmpId=w.CmpId);

Question 10: Find all company that has most employees.


Answer: Select CmpName from Company c join Works w on c.CmpId=w.CmpId join
Employee e On e.EmpId=w.EmpId group by c.CmpId having Count(w.CmpId)>=(select
max(e) from (Select Count(CmpId) as e from works group by CmpId)as e);

Question 11: Find all company that has smallest payroll.


Answer: Select CmpName from Company c, Works w where c.CmpId=w.CmpId and
Salary=(Select Min(Salary) from Works);
Mohan Aggarwal MCA - 1 B

Question 12: Find those companies whose employees earn a higher salary, on average,
than the average salary at First Bank Corporation.
Answer: Select CmpName from Company c join Works w On c.CmpId=w.CmpId group by
CmpName having avg(salary)>(select avg(salary) from Works w join Company c On
c.CmpId=w.CmpId where CmpName="FirstBankCrp");

You might also like