Database Management Systems Chapter 5 SQL: Queries, Constraints, Triggers Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for the Reserves relation contained only the attributes sid and bid, how would the semantics differ? S1 S2 R1 sid bid day 22 101 10/10/96 58 103 11/12/96 sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 1
Basic SQL Query select-list: columns to be retained. from-list: a cross-product of tables. qualification: selection conditions DISTINCT is an optional keyword indicating that the answer should not contain duplicates. Default is that duplicates are not eliminated! SELECT FROM WHERE [DISTINCT] select-list from-list qualification Conceptual Evaluation Strategy Semantics of an SQL query defined in terms of the following conceptual evaluation strategy: Compute the cross-product of from-list. Discard resulting tuples if they fail qualifications. Delete attributes that are not in select-list. If DISTINCT is specified, eliminate duplicate rows. This strategy is probably the least efficient way to compute a query! An optimizer will find more efficient strategies to compute the same answers. 2
Example of Conceptual Evaluation, Reserves R WHERE S.sid=R.sid AND R.bid=103 (sid) sname rating age (sid) bid day 22 dustin 7 45.0 22 101 10/10/96 22 dustin 7 45.0 58 103 11/12/96 31 lubber 8 55.5 22 101 10/10/96 31 lubber 8 55.5 58 103 11/12/96 58 rusty 10 35.0 22 101 10/10/96 58 rusty 10 35.0 58 103 11/12/96 A Note on Range Variables OR Really needed only if the same relation appears twice in the FROM clause. The previous query can also be written as: FROM Sailors AS S, Reserves AS R WHERE S.sid=R.sid AND bid=103; SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103; Optional It is good style, however, to use range variables always! 3
Q1: Find the sids of sailors who ve reserved a red boat SELECT R.sid FROM Boats B, Reserves R WHERE B.bid=R.bid AND B.color= red ; A join of two tables followed by a selection on the color of boats. (( Boats) Re ) sid color ' red' serves Q2: Find the names of sailors who ve reserved a red boat, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid = B.bid AND B.color = red ; A join of three tables, followed by a selection on the color of boats. sname (( Boats) Re serves Sailors) color ' red ' 4
Q4: Find names of sailors who ve reserved at least one boat, Reserves R WHERE S.sid=R.sid; sname(reserves Sailors) Expressions and Strings in select-list Select-list: expression AS column_name expression: any arithmetic or string expression over column names and constants. column_name: a new name for this column in the output of the query. Qualification: expression1 = expression2. 5
Q17: Compute increments for the ratings of persons who have sailed two different boats on the same day. Q17_1: who have sailed two different boats on the same day? Please show their sailor ids. SELECT DISTINCT R1.sid 1, Reserves R2 WHERE R1.sid=R2.sid AND R1.day = R2.day AND R1.bid<>R2.bid; Q17_2: Names of persons who have sailed two different boats on the same day. SELECT distinct S.sname, Reserves R1, Reserves R2 WHERE S.sid=R1.sid AND R1.sid=R2.sid AND R1.day = R2.day AND R1.bid<>R2.bid; Q17: Compute increments for the ratings of persons who have sailed two different boats on the same day. SELECT distinct S.sname, S.rating+1 AS rating, Reserves R1, Reserves R2 WHERE S.sid=R1.sid AND S.sid=R2.sid AND R1.day = R2.day AND R1.bid<>R2.bid; 6
Q18: Find names of sailors whose ratings are different in the given way. SELECT S1.sname AS name1, S2.sname AS name2 1, Sailors S2 WHERE 2*S1.rating = S2.rating; Expressions and Strings in select-list AS and = are two ways to name fields in result. LIKE is used for string matching. `_ stands for any one character and `% stands for 0 or more arbitrary characters. Q: Find triples (of ages of sailors and two fields defined by expressions) for sailors whose names begin and end with B and contain at least three characters. SELECT S.age, age1=s.age-5, 2*S.age AS age2 WHERE S.sname LIKE B_%B 7
Union UNION/OR: Can be used to compute the union of any two union-compatible sets of tuples. Q: Find sids of sailors who ve reserved a red or a green boat SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND (B.color= red OR B.color= green ); SELECT R1.sid FROM Boats B1, Reserves R1 WHERE R1.bid=B1.bid AND B1.color= red UNION SELECT R2.sid FROM Boats B2, Reserves R2 WHERE R2.bid=B2.bid AND B2.color= green Union Q: Find all sids of sailors who ve a rating of 10 or reserved boat 104. SELECT S.sid WHERE S.rating =10 UNION SELECT R.sid WHERE R.bid = 104 8
Intersect INTERSECT/AND: Can be used to compute the intersection of any two intersect-compatible sets of tuples. Q: Find sid s of sailors who ve reserved a red and a green boat SELECT R1.sid 1, Reserves R2, Boats B1, Boats B2 WHERE R1.sid = R2.sid and R1.bid=B1.bid AND R2.bid=B2.bid AND (B1.color= red AND B2.color= green ) SELECT R1.sid FROM Boats B1, Reserves R1 WHERE R1.bid=B1.bid AND B1.color= red INTERSECT SELECT R2.sid FROM Boats B2, Reserves R2 WHERE R2.bid=B2.bid AND B2.color= green Not supported in mysql Except (Difference) EXCEPT: Can be used to compute the set-difference of any two except-compatible sets of tuples. Q: Find sid s of sailors who ve reserved red boats but not green boats. SELECT R1.sid FROM Boats B1, Reserves R1 WHERE R1.bid=B1.bid AND B1.color= red EXCEPT SELECT R2.sid FROM Boats B2, Reserves R2 WHERE R2.bid=B2.bid AND B2.color= green Not supported in mysql 9
Nested Query One of the most powerful features of SQL. A query that has another query embedded within it. Q: Find names of sailors who ve reserved boat #103: WHERE S.sid IN (SELECT R.sid WHERE R.bid=103) SELECT distinct S.sname, Reserves R WHERE S.sid=R.sid AND bid=103 Non-nested Query Nested Query Example Q: Find names of sailors who ve not reserved boat #103: WHERE S.sid NOT IN (SELECT R.sid WHERE R.bid=103) Works as Difference Q: Find names of sailors who ve not reserved boat #103, but other boats., Reserves R WHERE S.sid=R.sid AND bid<>103 Non-nested Query 10
Nested Query Example Q: Find names of sailors who ve reserved a red boat: WHERE S.sid IN (SELECT R.sid WHERE R.bid IN ( SELECT B.bid FROM Boats B WHERE B.color = red )) Non-nested Query, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid = B.bid AND B.color = red ; Q: Find names of sailors who ve not reserved a red boat: WHERE S.sid NOT IN (SELECT R.sid WHERE R.bid IN ( SELECT B.bid FROM Boats B WHERE B.color = red )) Nested Query Example Works as WHERE S.sid NOT IN (SELECT R.sid Difference, Boats B WHERE R.bid = B.bid and B.color = red )) 11
Q: Find names of sailors who ve reserved a boat that is not red: WHERE S.sid IN (SELECT R.sid WHERE R.bid NOT IN ( SELECT B.bid FROM Boats B WHERE B.color = red )), Reserves R, Boats B WHERE S.sid=R.sid AND R.bid = B.bid and B.color <> red; Q: Find names of sailors who ve reserved both a red and a green boat., Boats B, Reserves R WHERE S.sid = R.sid AND R.bid=B.bid AND B.color= red AND S.sid IN ( SELECT S2.sid FROM Sailor S2, Boats B2, Reserves R2 WHERE S2.sid = R2.sid AND R2.bid=B2.bid AND B2.color= green ) SELECT R1.sid FROM Boats B1, Reserves R1 WHERE R1.bid=B1.bid AND B1.color= red INTERSECT SELECT R2.sid FROM Boats B2, Reserves R2 WHERE R2.bid=B2.bid AND B2.color= green 12
Correlated Nested Queries The inner query could depend on the row currently being examined in the outer query. Illustrates why, in general, subquery must be recomputed for each Sailors tuple. EXISTS is another set comparison operator, like IN. Q: Find names of sailors who ve reserved boat #103: WHERE EXISTS (SELECT * WHERE R.bid=103 AND S.sid=R.sid) Set-comparison Operators Also available: op ANY, op ALL, op IN,,,,, Q: Find sailors whose rating is greater than some sailor called Horatio: SELECT * WHERE S.rating > ANY (SELECT S2.rating 2 WHERE S2.sname= Horatio ) If there were no sailor called this name, the comparison is false, an empty set will be returned. 13
Q: Find sailors whose rating is greater than every sailor called Horatio: SELECT * WHERE S.rating > ALL (SELECT S2.rating 2 WHERE S2.sname= Horatio ) If there were no sailor called this name, the comparison is true, all sailors will be returned. Q: Find sailors with highest rating. SELECT S.sid WHERE S.rating > =ALL (SELECT S2.rating 2) Division is SQL Q: Find sailors who ve reserved all boats. WHERE NOT EXISTS ((SELECT B.bid FROM Boats B) EXCEPT (SELECT R.bid WHERE R.sid=S.sid)) 14