Assignment 12: Introduction to SQL (Cont) : Họ và tên: Lê Thu Thảo Mã SV:21IT447

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Họ và tên : Lê Thu Thảo

Mã SV:21IT447

Assignment 12: Introduction to SQL


(Cont)
1. Consider the bank database of Figure 12.1, where the primary keys are
underlined. Construct the following SQL queries for this relational
database.
branch(branch name, branch city, assets)
customer (ID, customer name, customer street, customer city)
loan (loan number, branch name, amount)
borrower (ID, loan number)
account (account number, branch name, balance )
depositor (ID, account number)
Figure 12.1 Banking database
a. Find the ID of each customer of the bank who has an account but not a
loan.
(SELECT customer name FROM depositor)
EXPECT(SELECT customer name FROM borrower)
b. Find the ID of each customer who lives on the same street and in the
same city as customer '12345'.
select customer name
from customer join customer using(customer street, customer city)
where S.customer name = ’12345’
c. Find the name of each branch that has at least one customer who has
an account in the bank and who lives in “Harrison”.
select distinct branch name
from account a inner join depositor d inner join customer c on
a.account_number=d.acount_number and c.id=d.id
where customer city = ’Harrison’
2. Consider the relational database of Figure 12.2, where the primary keys
are underlined. Give an expression in SQL for each of the following
queries.
employee (ID, person name, street, city)
works (ID, company name, salary)
company (company name, city)
manages (ID, manager id)
Figure 12.2 Employee database.
a. Find the ID, name, and city of residence of each employee who works
for “First Bank Corporation”.
select id,name,city
from employee e, company c, works w
where e.id=w.id and company_name=’First Bank Corporation’

b. Find the ID, name, and city of residence of each employee who works
for “First Bank Corporation” and earns more than $10000.
Select ID, person name , city
From employee
Where person name in ( select person name
From works
Where company name =’First Bank Corporation’ and salary > 10000)
c. Find the ID of each employee who does not work for “First Bank
Corporation”.
select person_name
from employee
where person name not in (select person_name
from works
where company name = ’First Bank Corporation’)

d. Find the ID of each employee who earns more than every employee of
“Small Bank Corporation”.
select person name
from works
where salary > all (select salary
from works
where company name = ’Small Bank Corporation’)
e. Assume that companies may be located in several cities. Find the name
of each company that is located in every city in which “Small Bank
Corporation” is located.
select c.company_name
from company c where (select p.city
from company p
where p.company_name = c.company name)
contains (select s.city
from company s
where s.company_name = ’Small Bank Corporation’)

f. Find the name of the company that has the most employees (or
companies, in the case where there is a tie for the most).
select company_name
from works
group by company_name
having count (distinct employee_name) >= all (select count (distinct employee name)
from works
group by company_name)

g. Find the name of each company whose employees earn a higher


salary, on average, than the average salary at “First Bank Corporation”.
select company_name
from works
group by company_name
having avg (salary) > (select avg (salary)
from works
where company name = ’First Bank Corporation’)

3. Consider the relational database of Figure 12.2. Give an expression in


SQL for each of the following:
a. Modify the database so that the employee whose ID is '12345' now
lives in “Newtown”.
update employee
set city = ’Newton’
where person_name = ’Jones’
b. Give each manager of “First Bank Corporation” a 10 percent raise
unless the salary becomes greater than $100000; in such cases, give only a
3 percent raise.
update works w
set w.salary = w.salary * 1.03
where e.employee_name in (select manager_name
from manages) and w.salary * 1.1 > 100000 and w.company name =
’First Bank Corporation’
update works w
set w.salary = w.salary * 1.1
where e.employee_name in (select manager_name
from manages) and w.salary * 1.1 <= 100000 and w.company name =
’First Bank Corporation’

You might also like