What is relational algebra for these two SQL queries:
Select * from film where film_rating = \'PG\' limit 20;
How can we show limit?
I'm only answering the question about joining here.
There is a m : n relation between actors and film. It must be realized through an intermediate table, say film_actors
. Also the country table probably has a short name like "In" and a long name like "India".
SELECT film.title, film_actors.role_type, actor.name, country.long_name
FROM
film
LEFT JOIN film_actors ON film.film_id = film_actors.film_id
LEFT JOIN actor ON film_actors.actor_id = actor.actor_id
LEFT JOIN country ON actor.country = country.short_name
If the country table has only one column with this short name, then it makes no sense to include it into your query at all. Do it only if country has some additional information that you want to include.
I used LEFT OUTER JOINS here. They are only requested, if the table on the right side may not always have related records. A cartoon probably has no actors. Otherwise use INNER JOIN
About the limit
in relational algebra. Traditional relational algebra does not support anything like the limit
in SQL. This problem has been recognized and studied by Li, Chang, Ilyas and Song in RankSQL: query algebra and optimization for relational top-k queries (SIGMOD 2005). They have proposed a monotonic scoring function F that ranks the results by the sorting operator tauF.