Download correct answers

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Database wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Functional Database Model wikipedia , lookup

Clusterpoint wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
PART III: SQL Queries (a)
We are given the following database schema.
borrower(customer_name, loan_number)
depositor(customer_name,account_number)
branch(branch_name, branch_city, assets)
account(account_number, branch_name, balance)
customer(customer_name, customer_street, customer_city)
loan(loan_number, branch_name, amount)
Primary keys are in bold.
Please fill in the missing parts of the queries. Use only the provided space. If your answer does not
fit into the provided space, that is not the intended solution and you will not get any points! Also,
all parts of a query must be correct before you can get credit for the question (no partial credits).5
pts. each query.
1. People who have both an account and a loan
Select depositor.customer_name
From ______________________ depositor natural inner join borrower
2. Customers who have both and account and a loan in the same branch.
Select customer_name
From depositor as D, account as A
Where D.account_number=A.account_number and
A.branch_name in
( Select branch_name
From borrower as B, Loan as L
Where B.loan_number=L.loan_number and
_____________________________D.customer_name=B.customer_name)
3. People who have an account in all the branches in “Istanbul”.
Select distinct customer_name
From customer as C
Where not exists (
1
(Select _________________branch_name
From ________________branch
Where ______________________branch_city = “Istanbul”)
Except
(Select branch_name
From depositor, account
Where depositor.customer_name=C.customer_name and
depositor.account_number=account.account_number) )
4. All customers who live in a street which has the word “east” in it.
Select customer_name
From customer
Where ________________________customer_street like ‘%east%’
5. Cities with the least number of branches
With how_many(branch_city,n) as
Select branch_city, count(*)
From branch
Group by _______________branch_city
Select branch_city
From how_many
Where _______________n <= all (
Select ___________n
From how_many)
2
6. Customers who have accounts but who do not have loans
Select customer_name
From depositor natural left outer join borrower
Where __________________loan_number is null
PART IV: SQL Queries (b)
Consider insurance database (primary key attributes are underlined):
Person(d_id#, name, city, street, street_number)
Car( license, model, year)
Accident( report#, date, city, street, street_number)
Owns( d_id#, license)
Participated( d_id#, car, rep#, damage)
1. (7 points) Find all cars (license and model) that participated in accidents in 2001 and
belong to John Smith
a) select car.license, model --without subqueries
from car, owns, person, participated, accident
where car.license=owns.license and owns.d_id#=person.d_id# and name=’John
Smith’ and car.license=car and rep#=report# and
year(date)=2001
b) select license, model -- with subqueries
from car
where license in
(select license
from owns, person, participated, accident
where license=car and owns.d_id#=person.d_id#
and name=’John Smith” and rep#=report#
and year(date)=2001
)
2. (7 points) Find the names of drivers having the same address as owners of the cars on
which they (drivers) participated in accidents in 2001 (for example, driver David, on car
Mazda belonging to John, participated in accident in 2001; if addresses of David and John
are the same – then output David).
a) select pd.name -- without subqueries
from person pd, person po, owns o, participated pt, accident
where pd.d_id#=pt.d_id# and rep#=report# and year(date)=2001 and -- driver
car=license and o.d_id#=po.d_id# and -- respective owner
po.street=pd.street and po.city=pd.city and --same address
po.street_number=pd.street_number -- have driver and owner
b) select name -- with subqueries
from person pd
3
where exists
(select car
from participated pt, accident, owns, person po
where rep#=report# and year(date)=2001 and pt.d_id#=pd.d_id#
-- found accident in which pd.d_id# took part
and car=license and owns.d_id#=po.d_id# -- owner of respective car
and po.city=pd.city and po.street=pd.street -- test addresses
and po.street_number=pd_street_number -- of driver and owner
)
3. (8 points) Find the names of drivers with the maximum number of accidents in which they
participated in 2001 and the respective total damage amount (for example, John and David
participated in 2001 in 10 accidents; other drivers participated this year in less numbers of
accidents. Total damage amount of accidents for John was $30.000, and for David $40.000.
Result set should have (John, 30 000), (David , 40 000)).
Select max(name) as d_name, p.d_id#, sum(damage) as total_damage
From participated pt, accident, person p
Where rep#=report# and year(date)=2001 and pt.d_id#=p.d_id#
Group by p.d_id#
Having count(rep#)>=all
( select count(rep#)
from participated, accident
where rep#=report# and year(date)=2001
group by d_id#
)
4. (8 points) Find the average damage per accident in accidents that occurred in Gazimagusa
for each year in the period of 1990-2002 (for example, 1990 – total damage amount =$1
000.000, 20 accidents, average is $50.000 per accident; 1991 – total damage amount = $1
210.000, 10 accidents, average is $121. 000 per accident and so on. Hint: use function
“year(date)” for grouping).
Select year(date) as year, sum(damage)/count(distinct rep#) as avg_dam_per_accident
From participated, accident
Where rep#=report# and city=’Gazimagusa’ and year(date) between 1990 and 2002
Group by year(date)
PART V: General
1. A database-management system is a
a) collection of data
b) set of programs
c) collection of data and set of programs
d) collection of interrelated data and set of programs to access those data
e) none from the above
2. Data of database-management systems are stored in
4
a)
b)
c)
d)
e)
RAM
ROM
registers
hard disks
none from the above
3. To access data of database-management system it should interact with
a) security system
b) computer system
c) operating system
d) banking system
e) none from the above
4. How many levels of data abstraction are provided by database-management systems?
a) 1
b) 2
c) 3
d) 4
e) none from the above
5. Database schema corresponds to the notion of
a) variable
b) vector
c) type
d) task
e) none from the above
6. An entity in E-R data model has
a) attributes
b) methods
c) programs
d) types
e) none from the above
7. Metadata is a
a) set of data
b) initial data
c) result set
d) data description
e) none from the above
8. ODBC stands for
a) octal database conversion
b) ordinary database clusters
c) oracle database connectivity
d) open database connectivity
e) none from the above
9. Transaction is an action which is
5
a)
b)
c)
d)
e)
atomic
rapid
important
reliable
none from the above
10. Database administrator is a person responsible for
a) writing programs
b) insertion of data into tables
c) introducing users to the database system
d) task distribution
e) none from the above
6