1 / 61

SQL: Structured Query Language (‘Sequel’)

SQL: Structured Query Language (‘Sequel’). Chapter 5. Running Example. R1. Instances of the Sailors and Reserves relations in our examples. S1. S2. Basic SQL Query. SELECT [DISTINCT] target-list FROM relation-list WHERE qualification.

avak
Télécharger la présentation

SQL: Structured Query Language (‘Sequel’)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SQL: Structured Query Language(‘Sequel’) Chapter 5

  2. Running Example R1 • Instances of the Sailors and Reserves relations in our examples. S1 S2

  3. Basic SQL Query SELECT [DISTINCT] target-list FROMrelation-list WHERE qualification • relation-list A list of relation names • target-list A list of attributes of relations in relation-list • qualification • Comparisons (Attr op const or Attr1 op Attr2, where op is one of ) combined using AND, OR and NOT. • DISTINCT is optional keyword indicating that answer should not contain duplicates. Default is that duplicates are not eliminated!

  4. Example Query SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103

  5. Comparisons in Oracle

  6. Conceptual Evaluation Strategy SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103 • Semantics of SQL query defined in terms of conceptual evaluation strategy: • Compute cross-product of relation-list. • Discard resulting tuples if they fail qualifications. • Delete attributes that are not in target-list. • If DISTINCT is specified, eliminate duplicate rows. • Probably the least efficient way! • Optimizer must find more efficient strategies to compute same answers.

  7. R S SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103

  8. A Note on Range Variables • Needed only if same relation appears twice in FROM clause. • The previous query can also be written as: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND bid=103 OR SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103 It is good style, however, to use range variables always!

  9. Find sailors who’ve reserved at least one boat SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid • Question: What is the output of this query ?

  10. Expressions and Strings SELECT S.age, age1=S.age-5, 2*S.age AS age2 FROM Sailors S WHERE S.sname LIKE‘B_%B’ • ASand = 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. • MEANING of Query: • Find sailors (age of sailor and two fields defined by expressions) whose names begin and end with B and contain at least three characters.

  11. SQL Queries Using Set Operations

  12. Find sid’s of sailors who’ve reserved a redor a green boat SELECT S.sid 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’) • UNIONcompute union of any two union-compatible sets of tuples SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ UNION SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’

  13. Find sid’s of sailors who’ve reserved a red and a green boat. • If we replace ORby AND as done below, what are the semantics? SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND (B.color=‘red’ AND B.color=‘green’)

  14. Key field! Find sid’s of sailors who’ve reserved a red and a green boat • INTERSECT: compute intersection of two union-compatible sets of tuples. • What happens if change S.sid as S.sname ? SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ INTERSECT SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’

  15. Find sid’s of sailors who’ve reserved a red and a green boat SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ INTERSECT SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’ • Could query be written without INTERSECT? SELECT S.sid FROM Sailors S, Boats B1, Reserves R1, Boats B2, Reserves R2 WHERE (S.sid=R1.sid AND R1.bid=B1.bid AND B1.color=‘red’ ) AND (S.sid=R2.sid AND R2.bid=B2.bid AND B2.color=‘green’) • Do we need Sailors relation in the FROM clauses?

  16. Find sid’s of sailors who’ve reserved a red and not a green boat SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ EXCEPT SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’ • What if we want to compute the DIFFERENCE instead? • SQL/92 standard, but some systems don’t support EXCEPT. • What then? SELECT S.sid FROM Sailors S, Boats B1, Reserves R1, Boats B2, Reserves R2 WHERE S.sid=R1.sid AND R1.bid=B1.bid AND S.sid=R2.sid AND R2.bid=B2.bid AND (B1.color='red' AND NOT(B2.color = 'green'))

  17. Find sid’s of sailors who’ve reserved a red and not a green boat SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ EXCEPT SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’ • What if we want to compute the DIFFERENCE instead? • Except is SQL/92 standard, but some systems don’t . support EXCEPT. SELECT S.sid FROM Sailors S, Boats B1, Reserves R1, Boats B2, Reserves R2 WHERE S.sid=R1.sid AND R1.bid=B1.bid AND S.sid=R2.sid AND R2.bid=B2.bid AND (B1.color='red' AND NOT(B2.color = 'green')) • Is the above query correct, or not correct ?

  18. About duplication • Default: Eliminate duplicate • Unless we use special keyword “ALL” : # of copies of a row in result UNION ALL (m+n) INTERSECT ALL min(m,n) EXCEPT ALL m-n

  19. Nested Queries

  20. Nested Queries Find names of sailors who’ve reserved boat #103: SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) • WHERE, FROM, HAVING clauses can itself contain another SQL query, called a subquery.  powerful feature of SQL.

  21. Nested Queries Evaluation Find names of sailors who’ve reserved boat #103: SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) • Nested loops evaluation: • For each Sailors tuple, • Evaluate the WHERE clause qualification • Which here means re-compute subquery.

  22. Nested Queries Find names of sailors who’ve reserved boat #103: SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) • Change query to find sailors who’ve not reserved #103: SELECT S.sname FROM Sailors S WHERE S.sid NOT IN (SELECT R.sid FROM Reserves R WHERE R.bid=103)

  23. Nested Queries Find names of sailors who’ve reserved boat #103: SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) • Could this query be rewritten without nesting?

  24. Nested Queries with Correlation Find names of sailors who’ve reserved boat #103: SELECT S.sname FROMSailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 ANDS.sid=R.sid) • EXISTSis another set comparison operator, like IN.

  25. Nested Queries with Correlation Find names of sailors who’ve reserved boat #103: SELECT S.sname FROMSailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 ANDS.sid=R.sid) • Why, in general, this subquery must be re-computed for each Sailors tuple ? • Could this query be rewritten into a flat SQL query?

  26. More on Set-Comparison Operators • op ANY andop ALL where op = { } • Find sailors whose rating is greater than that of some sailor called Horatio: • IN is equal to =ANY • NOT IN is equal to <>ANY SELECT * FROM Sailors S WHERE S.rating > ANY(SELECT S2.rating FROM Sailors S2 WHERE S2.sname=‘Horatio’)

  27. More on Set-Comparison Operators • Find sailors whose rating is greater than that of all sailors called Horatio: • What if there is no other sailor with that name? • “> ALL” would return TRUE if set is empty. SELECT * FROM Sailors S WHERE S.rating > ALL(SELECT S2.rating FROM Sailors S2 WHERE S2.sname=‘Horatio’)

  28. Rewriting INTERSECT Queries Using IN Find sid’s of sailors who’ve reserved both a red and a green boat: SELECT S.sid FROM Sailors S, 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 Sailors S2, Boats B2, Reserves R2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=‘green’)

  29. Rewriting INTERSECT Queries Using IN Find sname’s of sailors who’ve reserved both a red and a green boat: SELECT S.sname FROM Sailors S, 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 Sailors S2, Boats B2, Reserves R2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=‘green’) • INTERSECT queries re-written using IN. • Note: This query can return “sname” (and not just “sid) of Sailors who’ve reserved both red and green boats

  30. Division in SQL Find sailors who’ve reserved all boats.

  31. Division in SQL Find sailors who’ve reserved all boats. SELECT S.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))

  32. (1) SELECT S.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)) Division in SQL Find sailors who’ve reserved all boats. • Can we do it without EXCEPT ?

  33. (1) SELECT S.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)) Division in SQL Find sailors who’ve reserved all boats. • Let’s do it without EXCEPT: (2) SELECT S.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)) Sailors S such that ... there is no boat B without ... a Reserves tuple showing S reserved B

  34. Aggregation and Having Clauses

  35. Aggregate Operators • Significant extension of relational algebra. COUNT (*) COUNT ( [DISTINCT] A) SUM ( [DISTINCT] A) AVG ( [DISTINCT] A) MAX (A) MIN (A) Why no Distinct?

  36. COUNT (*) COUNT ( [DISTINCT] A) SUM ( [DISTINCT] A) AVG ( [DISTINCT] A) MAX (A) MIN (A) Aggregate Operators SELECT COUNT (*) FROM Sailors S Why no Distinct? SELECT AVG (S.age) FROM Sailors S WHERE S.rating=10 SELECT COUNT (DISTINCT S.rating) FROM Sailors S WHERE S.sname=‘Bob’

  37. More Examples of Aggregate Operators SELECT S.sname FROM Sailors S WHERE S.rating= (SELECT MAX(S2.rating) FROM Sailors S2) SELECT AVG ( DISTINCT S.age) FROM Sailors S WHERE S.rating=10

  38. Find name and age of the oldest sailor(s) SELECT S.sname, MAX (S.age) FROM Sailors S • Is this first query legal? • No! • Why not ? SELECT S.sname, S.age FROM Sailors S WHERE S.age = (SELECT MAX (S2.age) FROM Sailors S2) Is this second query legal?

  39. Motivation for Grouping • So far, aggregate operators to all (qualifying) tuples. • Question? • What if want to apply aggregate to each group of tuples. • Example : • Find the age of the youngest sailor for each rating level. • Example Procedure : • Suppose rating values  {1,2,…,10}, 10 queries: SELECT MIN (S.age) FROM Sailors S WHERE S.rating = i For i = 1, 2, ... , 10:

  40. Motivation for Grouping • What’s the problem with above ? • We don’t know how many rating levels exist. • Nor what the rating values for these levels are. SELECT MIN (S.age) FROM Sailors S WHERE S.rating = i For i = 1, 2, ... , 10:

  41. Add Group By to SQL

  42. Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROMrelation-list WHERE qualification GROUP BYgrouping-list HAVING group-qualification • A group is a set of tuples that each have the same value for all attributes in grouping-list. • HAVING clause is a restriction on each group.

  43. Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROMrelation-list WHERE qualification GROUP BYgrouping-list HAVING group-qualification • target-listcontains :(i) attribute names (ii) aggregate-op (column-name) (e.g., MIN (S.age)). • A group is a set of tuples that each have the same value for all attributes in grouping-list. • REQUIREMENT: - target-list (i)  grouping-list. - Why? - Each answer tuple of a group must have single value.

  44. Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors SELECT S.rating, MIN (S.age) AS min-age FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVINGCOUNT (*) > 1

  45. GroupBy --- Conceptual Evaluation • Compute the cross-product of relation-list • Discard tuples that fail qualification • Delete `unnecessary’ fields • Partition the remaining tuples into groups by the value of attributes in grouping-list. (GroupBy) • Eliminate groups using the group-qualification (Having) We will have a single value per group That is, one answer tuple is generated per qualifying group.

  46. Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors Sailors instance: SELECT S.rating, MIN (S.age) AS min-age FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVINGCOUNT (*) > 1

  47. Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors.

  48. Find age of the youngest sailor with age 18, for each rating with at least 2 sailors between 18 and 60. Sailors instance: SELECT S.rating, MIN (S.age) AS min-age FROM Sailors S WHERE S.age >= 18 AND S.age <= 60 GROUP BY S.rating HAVINGCOUNT (*) > 1 Answer relation:

  49. For each red boat, find the number of reservations for this boat SELECT B.bid, COUNT (*) AS s-count FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ GROUP BY B.bid • Grouping over Join of three relations. • Q: What do we get if we remove B.color=‘red’ from WHERE clause and add HAVING clause with this condition? • No difference. But Illegal. • Only column in GroupBy can appear in Having, unless in aggregate operator of Having

  50. Find age of the youngest sailor with age > 18, for each rating with at least 2 sailors (of any age) • HAVING clause can also contain a subquery. SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age > 18 GROUP BY S.rating HAVING 1 < (SELECT COUNT (*) FROM Sailors S2 WHERE S.rating=S2.rating) HAVING COUNT(*) >1 Find age of the youngest sailor with age > 18, for each rating with at least 2 such sailors (of age 18)

More Related