Docsity

CMPSCI 645 - Homework 1: Relational Algebra and SQL, Assignments of Principles of Database Management

Toggle sidebar
bg1
CMPSCI 645 — Homework 1
95 Points (plus 5 extra credit points)
Due February 21, at the beginning of class
1. (5 pts) Relational algebra to SQL
Given relational tables R(A, B), S(A, C ), T (C, D), express the following relational algebra
query in SQL.
ΠR.A,R.B,S.C (σR.A<20,T .D=21(R1S1T)))
2. (5 pts) Semijoin
The semijoin of relations Rand S, denoted R< S, is the set of tuples of Rthat agree with
at least one tuple of S on all attributes that are common to the schemas of Rand S. Give
an expression of the relational algebra that is equivalent to R< S.
3. (30 pts) Practical SQL exercises
You will use PostgreSQL and a simplified version of a dataset from the UCI KDD archive to
write and execute SQL queries. The dataset consists of three tables:
•FILMS(filmid, filmname, year, director, category)
•ACTORS(stagename, lname, fname, gender, dob, dod, country)
•CASTS(film, actor)
FILMS.filmid is a key for the FILMS table. ACTORS.stagename is a key for the ACTORS
table. CASTS.film refers to FILMS.filmid and CASTS.actor refers to ACTORS.stagename.
A database movies containing instances for the three tables has been loaded into Postgres
on an Edlab machine. Please see instructions on the website (linked under ”Assignments”)
for help connecting to the database.
Write SQL expressions for each of the following queries and execute them. Please turn in the
SQL queries along with the count of the number of rows returned (complete query results are
not necessary).
(a) List all actors in descending order of the number of films they acted in.
(b) Find the film(s) with the largest cast. Find the film(s) with the smallest cast. In both
cases, also return the size of the cast.
(c) The Bacon number of an actor is the length of the shortest path between the actor and
Kevin Bacon in the ”co-acting” graph. That is, Kevin Bacon has Bacon number 0; all
actors who acted in the same film as Kevin Bacon have Bacon number 1; all actors who
acted in the same film as some actor with Bacon number 1 have Bacon number 2, etc.
Return all actors whose Bacon number is 2.
1
pf3

Partial preview of the text

Download CMPSCI 645 - Homework 1: Relational Algebra and SQL and more Assignments Principles of Database Management in PDF only on Docsity!

CMPSCI 645 — Homework 1

95 Points (plus 5 extra credit points)

Due February 21, at the beginning of class

  1. (5 pts) Relational algebra to SQL

Given relational tables R(A, B), S(A, C), T (C, D), express the following relational algebra query in SQL. ΠR.A,R.B,S.C (σR.A< 20 ,T.D=21(R 1 S 1 T )))

  1. (5 pts) Semijoin

The semijoin of relations R and S, denoted R < S, is the set of tuples of R that agree with at least one tuple of S on all attributes that are common to the schemas of R and S. Give an expression of the relational algebra that is equivalent to R < S.

  1. (30 pts) Practical SQL exercises

You will use PostgreSQL and a simplified version of a dataset from the UCI KDD archive to write and execute SQL queries. The dataset consists of three tables:

  • FILMS(filmid, filmname, year, director, category)
  • ACTORS(stagename, lname, fname, gender, dob, dod, country)
  • CASTS(film, actor)

FILMS.filmid is a key for the FILMS table. ACTORS.stagename is a key for the ACTORS table. CASTS.film refers to FILMS.filmid and CASTS.actor refers to ACTORS.stagename. A database movies containing instances for the three tables has been loaded into Postgres on an Edlab machine. Please see instructions on the website (linked under ”Assignments”) for help connecting to the database. Write SQL expressions for each of the following queries and execute them. Please turn in the SQL queries along with the count of the number of rows returned (complete query results are not necessary).

(a) List all actors in descending order of the number of films they acted in. (b) Find the film(s) with the largest cast. Find the film(s) with the smallest cast. In both cases, also return the size of the cast. (c) The Bacon number of an actor is the length of the shortest path between the actor and Kevin Bacon in the ”co-acting” graph. That is, Kevin Bacon has Bacon number 0; all actors who acted in the same film as Kevin Bacon have Bacon number 1; all actors who acted in the same film as some actor with Bacon number 1 have Bacon number 2, etc. Return all actors whose Bacon number is 2.

(d) Find the actors who acted in films of all categories in the database. (e) Find all actors who acted only in films before 1965. (f) Find the films with more female actors than male actors.

  1. (15 pts) Monotonicity

A query or operator on relations is said to be monotone if whenever we add a tuple to one of its arguments, the result contains all the tuples that it contained before adding the tuple, plus perhaps more tuples. That is, there is no way to remove tuples from the output by adding tuples to the input.

(a) For each of the operators {∪, ∩, −, σ, Π, ×} state whether the operator is monotone. (b) For each of the queries in the previous exercise, state whether or not they are monotone. (c) Are all SELECT-FROM-WHERE queries in SQL monotone? Argue informally why or why not.

  1. (5 pts + ec) Join ordering

Recall that joins are commutative and associative. That is, the following relational equiva- lences hold: R 1 ./ R 2 ≡ R 2 ./ R 1

R 1 ./ (R 2 ./ R 3 ) ≡ (R 1 ./ R 2 ) ./ R 3 )

This allows us to freely reorder joins in an expression. Consider a query that performs the natural join of n relations:

Q = R 1 ./ (R 2 ./ (R 3 ./ ...(Rn− 1 ./ Rn))...)

(a) In a left-deep join expression, the right argument of each join operator is a base relation (rather than the result of a join). For example, ((A ./ B) ./ C) ./ D is a left deep expression, but (A ./ B) ./ (C ./ D) is not. (Imagine these expressions as operator trees and the terminology may seem more natural). How many different equivalent left-deep expressions are there for query Q? (b) (Extra credit – 5 pts) With no restrictions on the form of expressions, how many different equivalent expressions are there for Q? Hint: Catalan numbers.

  1. (10 pts) Consider the table Sailors(sid, sname, rating, age) and the query: Find the name of the sailors with a higher rating than all sailors with age < 21. The following two SQL queries attempt to obtain the answer to this question. Do they both compute the result? If not, explain why.

SELECT S.sname FROM Sailors S WHERE NOT EXISTS ( SELECT * FROM Sailors S WHERE S2.age < 21 AND S.rating ≤ S2.rating )