




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 1
1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 2
Optional List of relations Attr1 op Attr OPS: <, >, =, <=, >=, <> Combine using AND, OR, NOT Attributes from input relations
Optimizer chooses efficient plan!!
1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 4
1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 5
sid bid day Reserves 58 rusty 10 35 8
rating 31 lubber 55 22 dustin 45 sid sname age Sailors 22 101 10/10 31 lubber 8 55 58 103 11/12 22 dustin 7 45 58 103 11/12 58 rusty 10 35 58 22 22 sid 103 101 101 bid 11/ 10/ 10/ day 58 rusty 10 35 8 7 rating 31 lubber 55 22 dustin 45 sid sname age Reserves x Sailors 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 6
What’s the effect of adding DISTINCT? What’s the equivalent relational algebra?
RA project removes duplicates!
1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 10
SELECT S.sname, S.age, S.rating FROM Sailors S ORDER BY S.age ASC, S.rating DESC Find the names, ages, and rankings of all sailors. Sort the result in increasing order of age. If there is a tie, sort those tuples in decreasing order of rating. What does this query compute? 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 11
SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND (B.color = ‘red’ OR B.color = ‘green’)) SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’ UNION SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid and R.bid = B.bid AND B.color = ‘green’ 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 12
SELECT S.sid FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’ INTERSECT SELECT S.sid FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid and R.bid = B.bid AND B.color = ‘green’ What if we replace sid with sname (non-key)?
1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 13
SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) What does this Compute? Conceptual evaluation: For each row of Sailors, evaluate the subquery over reserves To find sailors who have not reserved 103, use NOT IN 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 15
SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT UNIQUE R.bid FROM Reserves R WHERE S.sid=R.sid) Find names of sailors who have reserved exactly one boat. True if value appears once Illustrates why subquery must be evaluated for each tuple in Sailors.
1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 19
of the youngest sailor SELECT MIN (S.age), S.rating FROM Sailors S GROUP BY S.rating How many tuples in the result? 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 20
SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification Conceptual Evaluation:
rating age 1 33. 0 7 45. 0 7 35. 0 8 55. 5 10 35. 0 rating 7 35. 0 Answer relation rating
1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 22
58 rusty 10 NULL 8
rating 31 lubber 55 22 dustin 45 sid sname age What does this query sailors return? SELECT sname FROM sailors WHERE age > 45 OR age <= 45 1/27/09 EECS 484: Database Management Systems, Kristen LeFevre 24
rating 31 lubber 55 22 dustin 45 sid sname age sailors Returns 50!