Tugas Sistem Database I: Relational Algebra and SQL
Tugas Sistem Database I: Relational Algebra and SQL
Tugas Sistem Database I: Relational Algebra and SQL
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
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;
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