Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly.

Similar presentations


Presentation on theme: "SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly."— Presentation transcript:

1 SQL Part I: Standard Queries

2 COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly 10 35 Sailors Reserves R S sid bid day 22101 2005/24/07 58103 2005/07/30 31103 2005/07/28 58 103 2005/07/31 bid bname color 101Interlake green 103 Snapper red Boat B

3 COMP-421: Database Systems - SQL Queries I 3 Principle Form of a Query q SELECT desired attributes FROM list of relations WHERE qualification ( where clause is optional) q SELECT rating,age FROM Sailors WHERE rating <= 6 OR age < 40 q Operational Semantics I Imagine a tuple variable ranging over all tuples of the relation  For each tuple: check if is satisfies the WHERE clause. If so, print the attributes in SELECT. q Conversion to Relational Algebra   rating,age (  rating<=6  age<40 (Sailors))  Start with the relation in the FROM clause  Apply , using condition in WHERE clause (selection)  Apply ∏, using attributes in SELECT clause (projection) I Difference: duplicates not eliminated sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly 10 35 ratingage 735 10 35

4 COMP-421: Database Systems - SQL Queries I 4 The WHERE Clause q Comparison:  attr1 op const, or  attr1 op attr2,  op is one of , , , , , , LIKE I We may apply the usual arithmetic operations +, *, etc. to numeric values before we compare  Qualification/Condition: Comparisons combined using AND, OR and NOT I name =‘Cheng’ AND NOT age = 18  name LIKE ‘%e_g’ (%: any string, _:any character)  Further string operations, e.g., concatenation, string-length, etc. q As default for most statements, SQL uses ‘multiset’ semantic, i.e., duplicates are allowed and not eliminated (as long as they do not violate a primary key / unique constraint)  DISTINCT is an optional keyword indicating that the answer should not contain duplicates.

5 COMP-421: Database Systems - SQL Queries I 5 Attribute Lists q Distinct SELECT DISTINCT sname FROM Sailors ( no WHERE clause OK) q Star as list of all attributes SELECT * FROM Sailors WHERE rating < 9 q Renaming columns SELECT sid, sname AS sailor FROM Sailors WHERE rating < 9 sid sname rating age 22 debby 7 35 31 debby 8 55 sailor 22 debby 31 debby sname debby lilly sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly 10 35

6 COMP-421: Database Systems - SQL Queries I 6 Attribute Lists (contd) q Expressions as values in columns SELECT sname, rating+1 AS upgrade FROM Sailors q Constants as attribute values SELECT rating AS reality, ‘10’ AS dream FROM Sailors WHERE sname LIKE ‘_e%y’ q Ordered Output SELECT * FROM Sailors ORDER BY age,rating sname upgrade debby 8 debby 9 lilly 11 reality dream 7 10 8 10 sid sname rating age 22 debby 7 35 58lilly 10 35 31 debby 8 55

7 COMP-421: Database Systems - SQL Queries I 7 Multirelational Queries  List of relations in FROM clause q Relation-dot-attribute disambiguates attributes from several relations. q Cross-Product: SELECT * FROM Sailors, Reserves Sailors X Reserves q Join: I Have to indicate comparison even with natural join I Example: “give me the names of all sailors that have reserved boat #103” SELECT sname FROM Sailors, Reserves WHERE Sailors.sid = Reserves.sid AND bid = 103  sname (  bid=103 (Reserves) Sailors)

8 COMP-421: Database Systems - SQL Queries I 8 Semantics q Conversion to Relational Algebra  Same as for single relation, but start with the product (X) of all the relations mentioned in the FROM clause q Parallel assignment of tuple variables  Consider a tuple variable for each relation in the FROM I Consider all possible assignments  For each such assignment of tuple variables to tuples that makes the WHERE true, output the attributes of the SELECT sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly 10 35 sid bid day 22101 2005/24/07 58103 2005/07/30 31103 2005/07/28 58 103 2005/07/31 sname debby Lilly lilly

9 COMP-421: Database Systems - SQL Queries I 9 Semantics (contd). q Nested Loop Assignment LET the tuple variables in the from clause range over relations R1, R2, …., Rn FOR each tuple t1 in relation R1 DO FOR each tuple t2 in relation R2 DO … FOR each tuple tn in relation Rn DO If the where clause is satisfied when the values from t1, t2, … tn are substituted for all attribute references THEN evaluate the attributes of the select clause according to t1, t2, …. tn and produce the tuple of values that results.

10 COMP-421: Database Systems - SQL Queries I 10 Range Variables  Optional use of range variables SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid AND R.bid = 103 q Use of range variable required when the same relation appears twice in the FROM clause q Example: “find pairs of sailors that have rented the same boat” SELECT r1.sid, r2.sid FROM Reserves r1, Reserves r2 WHERE r1.bid = r2.bid AND r1.sid < r2.sid (note that r1.sid < r2.sid is needed to avoid producing (58,58) and to avoid producing a pair in both directions.

11 COMP-421: Database Systems - SQL Queries I 11 Union, Intersection, Difference q Input relations for set operators must be set-compatible, I.e. they must have I Same number of attributes I The attributes, taken in order, must have same type q As default, result relation is a set!!! (no multiset) q Many systems do not provide primitives for intersection and difference

12 COMP-421: Database Systems - SQL Queries I 12 Union q Sailors(sid,sname,rating,age) Reserves(sid,bid,day) Boats(bid,bname,color) q Find sailors that have reserved a red or a green boat SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND (B.color = ‘red’ OR B.color = ‘green’) SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘red’ UNION SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘green’

13 COMP-421: Database Systems - SQL Queries I 13 Intersection q Find sailors that have reserved a red and a green boat (1) SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘red’ INTERSECT SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘green’ (2) SELECT R1.sid FROM Reserves R1, Reserves R2, Boats B1, Boats B2 WHERE (R1.bid = B1.bid AND B1.color = ‘red’) AND (R2.bid = B2.bid AND B2.color = ‘green’) AND R1.sid = R2.sid) sid bid day 22101 2005/24/07 58103 2005/07/30 22 103 2005/07/28 58 103 2005/07/31 sid bid day 22101 2005/24/07 58103 2005/07/30 22 103 2005/07/28 58 103 2005/07/31 bid color 101 green 103 red bid color 101 green 103 red

14 COMP-421: Database Systems - SQL Queries I 14 Difference q Find sailors that have reserved a red but not a green boat SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘red’ EXCEPT ( Oracle: MINUS) SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘green’

15 COMP-421: Database Systems - SQL Queries I 15 Multiset Semantic q A multiset (bag) may contain the same tuple more than once, although there is no specified order (unlike a list). I Example: {1, 2, 1, 3} is a multiset, but not a set q Multiset Union: I Sum the times an element appears in the two multisets I Example: {1, 2, 2}  {1, 2, 3, 3} = {1, 1, 2, 2, 2, 3, 3} q Multiset Intersection: I Take the minimum of the number of occurrences in each multiset. I Example: {1, 2, 2}  {1, 1, 2, 2, 3, 3} = {1, 2,2} q Multiset Difference: I Subtract the number of occurrences in the two multisets I Examples: {1, 2, 2} - {1, 2, 3, 3} = {2} q Some familiar laws for sets also hold for multisets (e.g., union is commutative); but other laws do not hold (e.g., R  (S  T)  (R  S)  (R  T)

16 COMP-421: Database Systems - SQL Queries I 16 Multiset Semantic in SQL q Although SQL generally works with multisets, it uses set semantic for union/intersection/difference q To enforce multiset semantic for these operators use I UNION ALL, INTERSECT ALL, EXCEPT ALL SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘red’ UNION ALL SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid AND B.color = ‘green’

17 COMP-421: Database Systems - SQL Queries I 17 Nested queries: The IN operator q A where clause can itself contain an SQL query. The inner query is called a subquery q Find names of sailors who have reserved boat #103 SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid = 103)  To find sailors who have NOT reserved boat #103 use NOT IN q Semantics best understood by nested loop assignment q Multiple attributes: I WHERE (a1,a2) IN (SELECT a3, a4…

18 COMP-421: Database Systems - SQL Queries I 18 Exists Operator  EXISTS ( relation) is true iff the relation is non-empty q Find names of sailors who have reserved boat #103 SELECT S.sname FROM Sailors S WHERE EXISTS(SELECT * FROM Reserves R WHERE R.bid = 103 AND R.sid = S.sid) q Scoping rule: to refer to outer Sailors in the inner subquery, we need to give a range variable to the outer relation. q A subquery that refers to values from a surrounding query is called a correlated subquery. q Since the inner query depends on the row of the outer query it must be reevaluated for each row in the outer query

19 COMP-421: Database Systems - SQL Queries I 19 Quantifiers  ANY and ALL behave as existential and universal quantifiers, respectively. q Syntax I WHERE attr op ANY (SELECT … I WHERE attr op ALL (SELECT  op is one of , , , , ,  q Find the sailors with the highest rating SELECT * FROM Sailors WHERE rating  ALL (SELECT rating FROM Sailors)

20 COMP-421: Database Systems - SQL Queries I 20 Complex queries (Division) q Find sailors who have reserved all boats SELECT sname FROM Sailors S WHERE NOT EXISTS ((SELECT B.bid FROM Boats B) EXCEPT (SELECT R.bid FROM Reserves R WHERE R.sid=S.sid)) SELECT sname FROM Sailors S WHERE NOT EXISTS (SELECT B.bid FROM Boats B WHERE NOT EXISTS (SELECT R.bid FROM Reserves R WHERE R.bid = B.bid AND R.sid = S.sid))


Download ppt "SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby 7 35 31 debby 8 55 58 lilly."

Similar presentations


Ads by Google