SExam2 PDF
SExam2 PDF
Your name:
Problem 1:
Consider the relational database, where the primary keys are underlined.
Give an expression in SQL for each of the following queries:
Employee(person_name,street,city)
Works(person_name,company_name,salary)
Company(company_name,city)
Manages(person_name,manager_name)
a. Find the names of all employees who work for the First Bank Corporation.
b. Find the names of all employees who live in the same city and on the same street
as do their managers.
Select E1.person_name
From Employee as E1, Employee as E2, Manages as M
Where E1.person_name=M.person_name and E2.person_name=M.manager_name
and E1.stree=E2.street and E1.city=E2.city
c. Find the names, street address, and cities of residence of all employees who work
for First Bank Corporation and earn more than $10,000 per annum.
with company_person_num as
(select company_name, count(distinct person_name) as employee_num
from Works
group by company_name)
select company_name from Works
group by company_name
having count(distinct person_name)=(select max(employee_num)
from company_person_num)
select company_name
from Works
group by company_name
having avg(salary)>(select avg(salary)
from Works
where company_name=’First Bank Corporation’)
g. Find the names of all employees in this database who live in the same city as the company
for which they work
select E.person_name
from Employee as E, Works as W, Company as C
where E.person_name=W.person_name and E.city=C.city
and W.company_name=C.company_name
h. Give all employees of First Bank Corporation a 10 percent salary raise.
update Works
set salary=salary*1.1
where company_name=’First Bank Corporation’
i. Delete all tuples in the works relation for employees of Small Bank Corporation.
Problem 2:
Using the “banking” example, write SQL to define the following views:
a. A view containing the account numbers and customer names (but not the balances)
for all accounts at the Stow branch.
c. A view containing the names and average account balance of every customer of the Kent branch.