Database Exam Midterm12-Sol
Database Exam Midterm12-Sol
Problem 1.(10%) Consider a table T (A, B, C), namely, the table’s name is T , and its attributes
are A, B, and C. It has 2 candidate keys: {A, B} and {B, C}. Can the following pairs of tuples
co-exist in T , respectively?
Answer.
(i) Πname (σcountry=“HK” (ACTOR))
(iii) T1 ← PLAY
T2 ← PLAY
Πpay (PLAY) − Πpay (σT1 .pay<T 2.pay (T1 × T2 ))
1
Problem 3.(40%) Write SQL queries for the following tasks based on the tables in Problem 2.
(i) Find the names of all directors at least 50 years old.
(ii) For each actor, display her/his aid, and the total amount of money s/he has made from all
movies.
(iii) For each director, display her/his name, and the number of distinct actors s/he has worked
with.
(iv) For each actor that has played in at least 5 movies, display her/his name and country.
(v) Find the country with the largest number of actors.
Answer.
(i) select dname from DIRECTOR where age >= 50
(i)
select sum(C) from T
group by B
having count(*) >= 2
(ii)
select A from T as R
where not exists (
select * from T
where T.B >= R.B and T.C >= R.C)
Answer.
2
(i) 110