SQL Database PRSQL
SQL Database PRSQL
e) Find the names of all employees in the database who earn more than every employee
of 'Small Bank Corporation'. Assume that all people work for at most one company.
select employee-name
from works
where salary > all (select salary
from works
where company-name = 'Small Bank Corporation')
f) Assume that the companies may be located in several cities. Find all companies
located in every city in which 'Small Bank Corporation' is located.
select s.company-name
from company s
where not exists
((select city from company where company-name = 'Small Bank
Corporation')
except
(select city from company t where s.company-name = t.company-name))
g) Find the names of all employees who earn more than the average salary of all
employees of their company. Assume that all people work for at most one company.
select employee-name
from works t
where salary >(select avg(salary) from works s
where t.company-name = s.company-name)
h) Find the name of the company that has the smallest payroll.
select company-name
from works
group by company-name
having sum(salary) <= all (select sum(salary)
from works
group by company-name)
2. Let R=(A, B, C), S=(C, D, E) and let q and r be relations on schema R and s be a
relation on schema S. Convert the following queries to SQL:
a) {<a> | b ( <a, b> r b = 10)}
/* 'distinct' is optional */
d) A, C(r) C, D(s)
select r.A, r.C, s.D from r, s where r.C = s.C
alternatively:
select r.A, r.C, s.D from r natural inner join s
e) r s
select distinct * from r, s
(based on text, questions 4.4 & 4.5)