Sie sind auf Seite 1von 3

Nama : Muhammad Faishal Dienul Haq

NPM : 140810190016
Kelas : B

TUGAS
SISTEM DATABASE I
Relational Algebra and SQL
Part 1:
Soal
Consider relation instances on the previous page, with the given schemas. In each question
below, write a relational algebra expression that computes the required answer.
a. List names of home countries of tennis players who were ranked first between 2013
and 2010 (inclusive).
b. List names and GDPs of countries from which there are no tennis player in our
database.
c. List pairs of tennis players such that (i) the ATP rank of the first is lower (better) than
that of the second, and (ii) the GDP of his home country is lower than that of the
second.
d. List name, age, ATP rank and country’s GDP of tennis players from Spain or Serbia.
e. List name, ATP rank and country of tennis players who were ranked first in 2010 or
later but not before 2010.
f. List names and populations of countries of tennis players who are currently ranked 5
or lower (better), are currently 30 years old or older, and were ranked first in some
year since 2004 (including 2004).
Jawaban

a. π country((σ 2010≤ year ≤2013(YRF))⊳ ⊲nameTP)


b. π name ,GDP (C) - π name ,GDP (C⊳ ⊲name=TP countryTP)
c. σ ( P 1. AT P < P 2. AT P ) ⋀ ( P 1.GDP< P 2.GDP )( ρ P 1(TP⊳ ⊲ TPcountry =C .name C) x ρ P 2(TP⊳ ⊲ TPcountry =
rank rank

C .name C))

d. π TP. name ,TP .age , TP. AT P ,C .GDP ((σ name ¿ Spain ⋁ name= Serbia (C))⊳ ⊲ C. name=TPcountry TP)
rank
' ' ' '

e. π name . AT P ( π name(σ 2010≤ year (YRF)) - π name(σ 2010≤ year (YRF)))⊳ ⊲ nameTP)
rank.country

f. π C .name , C . population((σ ATP ≤ 5 ⋀ age ≥30(TP)⊳ ⊲ name(σ year ≥ 2004(YRF)))⊳ ⊲ TP.name =C . nameC)
rank

Part 2:
Soal
Consider again relation instances on page 2, with the given schemas. In each question below,
write a SQL query that computes the required answer.
a. For each country, compute the number of years in which one of its tennis players was
ranked first. Result should have the schema (country, num_years).
b. List pairs of tennis players (player1, player2) in which player1 both has a lower
(better) ATP rank than player 2 and comes from a less populous country.
c. List pairs of players from the same country. List each pair exactly once. That is, you
should list either (Djokovic, Raonic, Serbia) or (Raonic, Djokovic, Serbia), but not
both. Result should have the schema (player1, player2, country).
d. For countries with at least 2 tennis players, list country name, GDP and average age
of its tennis players. Result should have the schema (country, GDP, avg_age).
e. List country name, GDP and population of each country. For countries that have
tennis players in our database, also list the minimum age of its tennis players. Result
should have the schema (country, GDP, population, min_age).
f. List names of countries who had a top-ranked tennis player both in 2010 or earlier
(i.e., between 2004 and 2010, inclusive) and after 2010 (i.e., between 2011 and 2015,
inclusive).

Jawaban
a. select TP.country as country, count(*) as num_years from Tennis_Players
TP,Years_Ranked_First YRF
where TP.name = YRF.name
group by TP.name;

b. select TP1.name player1, TP2.name player2 from Tennis_Players TP1,


Tennis_Players TP2, Countries C1, Countries C2
where TP1.country = C1.name and TP2.country = C2.name AND TP1.atp_rank <
TP2.atp_rank AND C1.population < C2.population;

c. select TP1.name player1, TP2.name player2, TP1.country from Tennis_Players TP1,


Tennis_Players TP2
where TP1.country = TP2.country and TP1.name < TP2.name;

d. select C.name, C.gdp, AVG(TP.age) from Tennis_Players TP, Countries C


where TP.country = C.name
group by C.name, C.gdp having count(*) >= 2;

e. select C.name as country, C.gdp, C.population, MIN(TP.age) as min_age from


Countries C left outer join Tennis_Players TP
on (C.name = TP.country)
group by C.name, C.gdp, C.population;

f. select distinct TP1.country from Tennis_Players TP1, Tennis_Players TP2,


Years_Ranked_First YRF1, Years_Ranked_First YRF2
where TP1.country = TP2.country AND TP1.name = YRF1.name AND TP2.name =
YRF2.name AND YRF1.year <= 2010 AND YRF2.year > 2010;
Part 3:
Soal

a. Write two equivalent SQL queries that lists dishes in which one of the ingredients is a
meat and another is a veg. List each dish exactly once. Sort results in alphabetical
order. Result should have the schema (dish).
b. Write a SQL query that computes the number of ingredients and the number of
calories per dish. Only return dishes that have fewer than 250 total calories. Result
should have the schema (dish, num_ingredients, total_calories).
c. Write a SQL query that list dishes with exactly 3 ingredients, along with the total
number of calories per dish. Only return dishes that have at least 200 total calories.
Result should have the schema (dish, total_calories).

Jawaban

a. select distinct D1.dish from Dishes D1, Dishes D2, Foods F1, Foods F2


where  D1.dish = D2.dish AND  D1.food = F1.food AND  D2.food = F2.foodand
F1.category = 'meat'  AND  F2.category = 'veg' order by D1.dish
select  distinct D1.dish from Dishes D1, Foods F1
where D1.food = F1.foodand F1.category = 'meat' AND D1.dish  in
(select distinct D2.dish from Dishes  D2, Foods F2
where D2.food = F2.food AND F2.category = 'veg')
order by D1.dish;

b. select D.dish, count(*) as num_ingredients ,sum(calories) as total_calories from


Dishes D, Foods F
where D.food = F.food
group by D.dish having sum(calories) < 250;

c. select D.dish, sum(calories) as total_calories from Dishes D, Foods F


where D.food = F.food
group by D.dish having sum(calories) >= 200 and count(*) = 3;

Das könnte Ihnen auch gefallen