SQL Solution Full
SQL Solution Full
Relational Algebra:
1. Consider the following relational database where the primary keys are underlined. Give an
expression in the relational algebra to express each of the following queries:
branch(branch- name, branch-city, assets)
customer(customer-name, customer-street, customer-city)
loan(loan-number, branch-name, amount)
borrower(customer-name, loan-number)
account(account-number, branch-name, balance)
depositor(customer-name, account-number)
i. Select those tuples of the loan relation where the branch is “Perryridge”.
Ans:
σ branch-name=”Perryridge” (loan)
ii. Find all tuples in which the amount lent is more than $1200.
Ans:
σ amount > 1200 (loan)
iii. List all loan numbers and the amount of the loans.
Ans:
Π loan-number, amount (loan)
iv. Find those customers who live in Harrison.
Ans:
Π customer-name (σ customer-city=”Harrison” (customer) )
v. Find the names of all customers with a loan in the bank.
Ans:
Π customer-name (borrower)
vi. Find the names of all customers with an account in the bank.
Ans:
Π customer-name (depositor)
vii. Find the names of all bank customers who have either an account or a loan or both.
Ans:
Π customer-name (borrower) U Π customer-name (depositor)
viii. Find all customers of the bank who have an account but not a loan.
Ans:
Π customer-name (depositor) – Π customer-name (borrower)
ix. Find the names of all customers who have a loan at the Perryridge branch.
Ans:
Π customer-name (σ borrower.loan-number = loan.loan-number
(σ branch-name=”Perryridge” (borrower × loan)))
xii. Find all customers of the bank who have both a loan and an account.
Ans:
Π customer-name (borrower) ∩ Π customer-name (depositor)
OR:
Π customer-name (borrower |×| depositor)
xiii. Find the names of customers who have a loan at the bank, along with the loan
number and the amount.
Ans:
Π customer-name, loan.loan-number, amount
(σ borrower.loan-number = loan.loan- number (borrower × loan))
xiv. Find the names of all customers who have a loan at the bank and find the amount
of the loan.
Ans:
Π customer-name, .loan-number, amount (borrower |×| loan)
xv. Find the names of all branches with customers who have an account in the bank
and who live in Harrison.
Ans:
Π branch-name (σ customer-city = “Harrison” (customer |×| account |×| depositor))
xvi. Delete all of Smith‟s account records.
Ans:
depositor ← depositor - σ customer-name = “Smith” (depositor)
xviii. Insert the record that Smith has $1200 in account A-973 at the Perryridge
branch.
Ans:
account ← account U { (A-973, “Perryridge”, 1200) }
depositor ← depositor U { (“Smith”, A-973) }
xx. Update the records so that accounts with balances over $10,000 will receive 6%
interest, whereas all others will receive 5%.
Ans:
account ← Π (σ balance>10000 ( account))
account-number ,branch-name, balance*1.06
U Π account-number ,branch-name, balance*1.05 (σ balance≤10000 ( account))
xxi. Create a view consisting of branches and the names of the customers who have
either an account or a loan at the bank.
Ans:
create view all-customer as
Π branch-name, customer-name (depositor |×| account)
UΠ branch-name, customer-name (borrower |×| loan)
2. Consider the following relational database where the primary keys are underlined. Give an
expression in the relational algebra to express 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)
i. Find the names of all employees who work for First Bank Corporation.
Ans:
Π employee-name (σ company-name = “FBC” (works))
ii. Find the names of all employees who do not work for First Bank Corporation.
Ans:
Π employee-name (σ company-name ≠ “FBC” (works))
iii. Find the names, street address, and cities of residence of all employees who work
for First Bank Corporation and earn more than $50,000 per annum.
Ans:
Π employee.employee-name, street, city
(σ company-name = “FBC” ^ salary > 50000 (works |×| employee))
iv. Find the names of all employees who earn more than every employee of Small Bank
Corporation.
Ans:
Π employee-name (σ works.salary >SBC-employee.salary
(works × ρ SBC-employee (salary) (Π salary
(σ company-name=”SBC” (works)))))
OR:
v. Find the names and cities of residence of all employees who work for First Bank
Corporation.
Ans:
Π employee.employee-name, city (σ company-name = “FBC” (works |×| employee))
vi. Find the names of all employees who live in the same city as the company for which
they work.
Ans:
Π employee-address.e-name
(σ company.company-name=employee-address.e-company-name ^ company.city=employee-address.e-city
(company × ρ employee-address (e-name, e-city, e-company-name)
(Π employee.employee-name, city, company-name (employee|×| works)))))
vii. Find the names of all employees who live in the same city and on the same street as
do their managers.
Ans:
Π employee-address.e-name
(σ employee-address.e-manager-name=manager-address.m-name ^ , employee-address.e-street=manager-
address.m-street ^ employee-address.e-city=manager-address.m-city
(ρ employee-address (e-name, e-manager-name, e-street, e-city)
(Π employee.employee-name, manager-name, street, city (employee |×| manages ) )
× ρ manager-address (m-name, m-street, m-city)
(Π manager-name, street, city
(σ employee.employee-name=manager-name (employee × manages)))))
viii. Modify the database so that Jones now lives in Newtown.
Ans:
employee ← Π employee-name, street, “Newtown” (σ employee-name=’Jones’ ( employee))
U (employee - σ employee-name=’Jones’ ( employee))
ix. Give all employees of First Bank Corporation a 10% salary raise.
Ans:
works ← Π employee-name, company-name, salary*1.1 (σ company-name=’FBC’ (works))
U (works - σ company-name=’FBC’ (works))
x. Give all managers in this database a 10% salary raise.
Ans:
t 1 ← Π works.employee-name, company-name, salary
(σ works.employee-name=manager-name (works × manages ))
t 2 ← Π employee-name, company-name, salary*1.1 (t 1 )
works ← (works - t 1 ) U t 2
xi. Give all managers in this database a 10% salary raise, unless the salary becomes
greater than 100,000; in such cases, give only a 3 percent raise.
Ans:
t 1 ← Π works.employee-name, company-name, salary
(σ works.employee-name=manager-name (works × manages ))
t 2 ← Π works.employee-name, company-name, salary*1.03 (σ salary*1.1 > 100000 (t 1 ))
t 2 ← t 2 U Π works.employee-name, company-name, salary*1.1 (σ salary*1.1 ≤ 100000 (t 1 ))
works ← (works - t 1 ) U t 2
xii. Delete all tuples in the works relation for employees of Small Bank Corporation.
Ans:
works ← works - σ company-name=’SBC’ (works)
SQL Solution:
1. Consider the following database where the primary keys are underlined. Give an
expression in SQL for each of the following queries:
branch(branch-name, branch-city, assets)
customer(customer-name, customer-street, customer-city)
loan(loan-number, branch-name, amount)
borrower(customer-name, loan-number)
account(account-number, branch-name, balance)
depositor(customer-name, account number)
i) Find the names of all branches in the loan relation.
Ans:
select branch-name
from loan
ii) Find all loan numbers for loans made at the Perryridge branch with loan
amounts greater $1200.
Ans:
select loan-number
from loan
where branch-name=‟Perryridge‟ and amount > 1200
iii) Find all loan numbers with loan amounts between $2000 and $5000.
Ans:
select loan-number
from loan
where amount between 2000 and 5000
OR:
select loan-number
from loan
where amount <= 5000 and amount >= 2000
iv) For all customers who have a loan from the bank, find their names, loan
numbers and loan amount.
Ans:
select customer-name, borrower.loan-number, amount
from borrower, loan
where borrower.loan-number=loan.loan-number
v) Find the customer names, loan numbers, and loan amounts for all loans at
the Perryridge branch.
Ans:
select customer-name, borrower.loan-number, amount
from borrower, loan
where borrower.loan-number=loan.loan-number and
branch-name=‟Perriridge‟
vi) Find the names of all branches that have assets greater than at least one
branch located in Brooklyn.
Ans:
select distinct T.branch-name
from branch as T, branch as S
where T.assets > S.assets and S.branch-name=‟Brooklyn‟
OR:
select branch-name
from branch
where assets > some (select assets
from branch
where branch-city=‟Brooklyn‟)
vii) Find the names of all branches that have assets greater than that of each
branch in Brooklyn.
Ans:
select branch-name
from branch
where assets > all (select assets
from branch
where branch-city=‟Brooklyn‟)
viii) Find the names of all customers whose street address includes the substring
„Main‟.
Ans:
select customer-name, borrower.loan-number, amount
from customer
where customer-street like „%Main%‟
ix) Find the names of all customers in alphabetic order who have a loan at the
Perryridge branch.
Ans:
select distinct customer-name
from borrower, loan
where borrower.loan-number=loan.loan-number and
branch-name=‟Perriridge‟
order by customer-name
x) Find the names of all customers who have a loan or an account or both at
the bank.
Ans:
(select customer-name
from depositor)
union
(select customer-name
from borrower)
xi) Find the names of all customers who have both a loan and an account at the
bank.
Ans:
(select distinct customer-name
from depositor)
intersect
(select distinct customer-name
from borrower)
OR:
select distinct customer-name
from depositor
where customer-name in (select customer-name
from borrower)
xii) Find all customers who have both an account and a loan at the Perryridge
branch.
Ans:
(select distinct customer-name
from depositor, account
where depositor.account-number=account.account-number and
branch-name=‟Perryridge‟)
intersect
(select distinct customer-name
from borrower,loan
where borrower.loan-number=loan.loan-number and
branch-name=’Perryridge‟)
OR:
select distinct customer-name
from depositor, account
where depositor.account-number=account.account-number and
branch-name=‟Perryridge‟ and
(branch-name, customer-name) in
(select branch-name, customer-name
from borrower, loan
where borrower.loan-number=loan.loan-number)
xiii) Find the names of all customers who have an account but no loan at the bank.
Ans:
(select distinct customer-name
from depositor)
except
(select customer-name
from borrower)
OR:
select distinct customer-name
from depositor
where customer-name not in (select customer-name
from borrower)
xiv) Find the names of all customers who have a loan at the bank and whose
names are neither Smith nor Jones.
Ans:
select customer-name
from borrower
where customer-name!=‟Smith‟ and customer-name!=‟Jones‟
OR:
select customer-name
from borrower
where customer-name not in (‟Smith‟, ‟Jones‟)
xviii) Find the names of the branches where the average account balance is more than $1200.
Ans:
select branch-name, avg (balance)
from account
group by branch-name
having avg (balance)>1200
.
xxv) Insert the fact that there is an account A-9732 at the Perryridge branch that
has a balance of $1200.
Ans:
insert into account
values („A-9732‟, „Perryridge‟, 1200)
xxviii) Update the records so that accounts with balances over $10,000 will
receive 6% interest, whereas all others will receive 5%.
Ans:
update account
set balance=balance*1.06
where balance > 10,000
update account
set balance=balance*1.05
where balance <= 10,000
2. Consider the following employee 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)
i) Find the names of all employees who work for First Bank Corporation (FBC).
Ans:
select employee-name
from works
where company-name=‟FBC‟
ii) Find the names and cities of residence of all employees who work for First
Bank Corporation.
Ans:
select employee.employee-name, city
from employee, works
where employee.employee-name=works.employee-name and
company-name=‟FBC‟
iii) 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 month.
Ans:
select employee.employee-name, street, city
from employee, works
where employee.employee-name=works.employee-name and
company-name=‟FBC‟ and salary > 10000
OR:
select *
from employee
where employee-name in (select employee-name
from works
where company-name=‟FBC‟ and salary > 10000)
iv) Find the names of all employees who live in the same city as the company
for which they work.
Ans:
select employee.employee-name
from employee, works
where employee.employee-name=works.employee-name and
(company-name, city) in ( select company-name, city
from company )
v) Find the names of all employees who live in the same city and on the same
street as do their managers.
Ans:
select employee-name
from employee, manages
where employee.employee-name=manages.employee-name and
(manager-name, street, city)
in ( select manager-name, street, city
from manages, employee
where manager-name= employee.employee- name)
vi) Find the names of all employees who do not work for First Bank
Corporation.
Ans:
select employee-name
from employee
where employee-name not in (select employee-name
from works
where company-name=‟FBC‟)
vii) Find the names of all employees who earn more than every employee of
Small Bank Corporation.
Ans:
select employee-name
from works
where salary > all (select salary
from works
where company-name=‟SBC‟)
xvi. Find the names of all employees who earn more than the average salary of
all employees of their company.
Ans:
select employee-name
from works
where salary > (select avg (salary)
from works
group by company-name)
xx. Give all employees of First Bank Corporation a 10% salary raise.
Ans:
update works
set salary=salary*1.1
where company-name=’FBC’
xxii. Give all managers of FBC a 10% salary raise unless the salary becomes
greater than 100,000; in such cases, give only a 3 percent raise.
Ans:
update works
set salary=case
when salary <=100,000 then salary*1.1
else salary*1.03
end
where company-name=’FBC’ and
employee-name in (select manager-name
from manages)
OR:
update works
set salary=case
when salary*1.1 >100,000 then salary*1.03
else salary*1.1
end
where company-name=’FBC’ and
employee-name in (select manager-name
from manages)
xxiii. Delete all tuples in the works relation for employees of Small Bank
Corporation.
Ans:
delete from works
where company-name=’SBC’
3. Let the following relation schemas be given: R=(A, B, C) and S=(D, E, F). Let
relations r(R) and s(S) be given. Give an expression in SQL that is equivalent to
each of the following queries.
i. ∏A(r) ii. σB=17 (r) iii. r × s iv. ∏A, F(σC=D(r × s))
Ans:
i) select distinct A
from r
ii) select *
from r
where B=17
iii) select distinct *
from r, s
iv) select distinct A, F
from r, s
where C=D
4. Consider the insurance database of Fig.9, where the primary keys are underlined.
Construct the following SQL queries for this relational database.
a) Find the total number of people who owned cars that were involved in
accidents in 1989.
select count(distinct name) from accident, participated, person
where accident.report-number=participated.report-number
and participated.driver- id=person.driver-id
and date between „1989-01-01‟ and „1989-12-31‟
b) Find the number of accidents in which the cars belonging to “John Smith”
were involved.
select count(distinct *) from person, participated
where person.driver- id=participated.driver-id
and name=‟John Smith‟
c) Add a new accident to the database; assume any values for required
attributes.
We assume the driver was Jones although it could be someone else. We also assume
that Joes owns one Toyota, accident location= Barkely, date=1990-01-10, report
number=7001, and damage amount=$3000. Now we have to the relations.
insert into accident values(7001, „1990-01-01‟, „Barkely‟)
inset into participated (
select o.driver-id, c.license, 7001, 3000
from person p, owns o, car c
where p.name=‟Jones‟ and p.driver-id=o.driver- id
and o.license=c.license and c.model=‟Toyota‟ )
e) Update the damage amount for the car with license number “AABB2000” in
the accident with report number “AR2197” to $3000.
update participated set damage-amount=3000
where report-number=‟AR2197‟
person(driver-id#, name, address)
car(license, model, year)
accident(report-number, date, location)
owns(driver-id#, license)
participated(driver-id, car, report-number, damage-amount)
Fig. 9
10. Consider the database of question 3. Using SQL, define a view consisting of branch
names and the names of customers who have either an account or a loan at a branch.
Ans:
create view all-customer as
(select branch-name, customer-name
from depositor, account
where depositor.account-number=account.account-number)
union
(select branch-name, customer-name
from borrower, loan
where borrower.loan-number=loan.loan-number)
11. Give an SQL schema definition for the database of Question no. 3.
Ans:
create table customer
(customer-name char(20),
customer-street char(30),
customer-city char(30),
primary key (customer-name))
3.1 Design a relational database for a university registrar‟s office. The office maintains
data about each class, including the instructor, the number of students enrolled, and
the time and place of the class meetings. For each student-class pair, a grade is
recorded.
Ans: student(student-id, name, program)
course(course-no, title, syllabus, credits)
course-offering(course-no, sec-no, year, semester, time, room)
instructor(instructor-id, name, dept, designation)
enrolls(student-id, course-no, sec-no, semester, year, grade)
teaches(course-no, sec-no, semester, year, instructor-id)
requires(course-no, prerequisite)
3.2 Describe the differences in meaning between the terms relation and relation
schema. Illustrate your answer giving examples.
Ans:
A relation schema is a type definition, whereas a relation is an instance of schema.
student (student-id, name) is a relation schema.
student-id Name
301 Saila
302 Jesmin
303 Khokan
Chapter-2
2.2 Construct an E-R diagram for a car-insurance company whose customers own one
or more cars each. Each car has associated with it zero to any number of recorded
accidents.
r r
model-no
report-no
participated accident
t date
location
damage-amount
2.3 Construct an E-R diagram for a hospital with a set of patients and a set of medical
doctors. Associate with each patient a log of the various tests and examinations
conducted.
address date-admitted
name
insurance
ssn date-checked-out
patient
test-log doc-patient
date time
test-no doctor-id
result specialization
test-name name
2.4 A university registrar‟s office maintains data about the following entities: (a)
courses, including number, title, credits, syllabus, and prerequisites; (b) course
offerings, including course number, year, semester, section number, instructor(s),
timings and classrooms; (c) students, including student-id, name, and program; and
(d) instructors, including identification number, name, department, and title.
Further, the enrolment of students in courses and grades awarded to students in
each course they are enrolled for must be appropriately modeled.
Construct an E-R diagram for the registrar‟s office. Document all assumptions that
you make about the mapping constraints.
title
room
time dept
is-offered
c-no
prerequisites title
requires
main courses
courses
credits
syllabus
2.5 Consider a database used to record the marks that students get in different exams
of different course offerings.
a) Construct an E-R diagram that models exams as entities, and uses a ternary
relationship, for the above database.
b) Construct an alternative E-R diagram that uses only a binary relationship
between students and course-offerings. Make sure that only one
relationship exists between a particular student and course-offering pair,
yet you can represent the marks that a student gets in different exams of a
course offering.
time
room
mark
exams
c-no time
title place
participation time
room
mark
exams
c-no time
title place
score
players played matches
2.12 Consider the E-R diagram in fig.2.29, which models an online bookstore.
a. List the entity sets and their primary keys.
b. Suppose the bookstore adds music cassettes and compact disks to its
collections. The same music item may be present in cassette or compact
disk format, with differing prices. Extend the E-R diagram to model this
addition, ignoring the effect on shopping baskets.
c. Now extend the E-R diagram, using generalization, to model the case
where a shopping basket may contain any combination of books, music
cassettes, or compact disks.
2.14 Consider a university database for the scheduling of classrooms for final exams.
This database could be modeled as the single entity set exam, with attributes
course-name, section-number, room-number, and time. Alternatively, one or more
additional entity sets could be defined, along with relationship sets to replace some
of the attributes of the exam entity set, as
course with attributes name, department, and c-number
section with attributes s-number and enrolment, and dependent as a weak
entity set on course
room with attributes r-number, capacity, and building
Show an E-R diagram illustrating the use of all three additional entity sets listed.
name
c-number department s-number enrollment
room in exam
r-number time
building c-number
capacity date