PRACTICAL 4
Create the following tables, enter at least 5 records in each table and answer the queries given
below.
Employee ( Employee_ID, Person_Name, Street, City )
Works ( Employee_ID, Company_ID, Salary )
Company( Company_ID, Company_Name, City )
Manages ( Employee_ID, Manager_ID)
a) Identify primary and foreign keys.
PRIMARY KEYS:
Employee Company Works Manages
Emp_ID Emp_ID Emp_ID Emp_ID
Company_ID Company_ID Manager_ID
FOREIGN KEYS:
Employee Company Works Manages
Emp_ID Emp_ID Emp_ID
Company_ID Manager_ID
b) Alter table employee, add a column “Email” of type varchar(20).
c) Find the name of all managers who work for both Samba Bank or NCB Bank.
mysql> select Person_Name from Employee,Manages,Works,Company
-> where (Company_Name='Samba Bank'or
-> Company_Name='NPB Bank') AND
-> works.Company_ID=Company.Company_ID AND
-> works.Emp_ID=Manages.Manager_ID AND
-> Employee.Emp_ID=works.Emp_ID;
d) Find the names, street address and cities of residence and salary of all employees
who work for “Samba Bank” and earn more than Rs.10,000.
mysql> select * from Employee
-> where Emp_ID IN
-> (select Emp_ID from Company,works where Company_Name="Samba Bank" and
salary>10000);
e) Find the names of all employees who live in the same city as the company for which
they work.
mysql> Select Person_Name from Employee,Company,Works
-> where Employee.City=Company.City and Employee.Emp_ID=Works.Emp_ID and
Company.Company_ID=Works.Company_ID;
f) Find the highest salary, lowest salary and average salary paid by each company.
g) Find the sum of salary and number of employees in each company.
h) Find the name of the company that pays the highest payroll.