SQL Structured Query Language Sequel Chapter 5 1


![Basic SQL Query SELECT FROM WHERE v v v [DISTINCT] target-list relation-list qualification relation-list Basic SQL Query SELECT FROM WHERE v v v [DISTINCT] target-list relation-list qualification relation-list](https://slidetodoc.com/presentation_image_h/0b759da7f4e3d52590e9ae0454b4ccdf/image-3.jpg)































![Aggregate Operators v Significant extension of relational algebra. COUNT (*) COUNT ( [DISTINCT] A) Aggregate Operators v Significant extension of relational algebra. COUNT (*) COUNT ( [DISTINCT] A)](https://slidetodoc.com/presentation_image_h/0b759da7f4e3d52590e9ae0454b4ccdf/image-35.jpg)
![Aggregate Operators COUNT (*) COUNT ( [DISTINCT] A) SUM ( [DISTINCT] A) AVG ( Aggregate Operators COUNT (*) COUNT ( [DISTINCT] A) SUM ( [DISTINCT] A) AVG (](https://slidetodoc.com/presentation_image_h/0b759da7f4e3d52590e9ae0454b4ccdf/image-36.jpg)





![Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP](https://slidetodoc.com/presentation_image_h/0b759da7f4e3d52590e9ae0454b4ccdf/image-42.jpg)
![Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP](https://slidetodoc.com/presentation_image_h/0b759da7f4e3d52590e9ae0454b4ccdf/image-43.jpg)
















![SQL Queries - Summary SELECT [DISTINCT] a 1, a 2, …, an FROM R SQL Queries - Summary SELECT [DISTINCT] a 1, a 2, …, an FROM R](https://slidetodoc.com/presentation_image_h/0b759da7f4e3d52590e9ae0454b4ccdf/image-60.jpg)

- Slides: 61

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

Running Example v Instances of the Sailors and Reserves relations in our examples. R 1 S 2 3
![Basic SQL Query SELECT FROM WHERE v v v DISTINCT targetlist relationlist qualification relationlist Basic SQL Query SELECT FROM WHERE v v v [DISTINCT] target-list relation-list qualification relation-list](https://slidetodoc.com/presentation_image_h/0b759da7f4e3d52590e9ae0454b4ccdf/image-3.jpg)
Basic SQL Query SELECT FROM WHERE v v v [DISTINCT] target-list relation-list 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 Attr 1 op Attr 2, where op is one of ) combined using AND, OR and NOT. v is optional keyword indicating that answer should not contain duplicates. Default is that duplicates are not eliminated! DISTINCT 4

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

Comparisons in Oracle 6

Conceptual Evaluation Strategy SELECT FROM WHERE v S. sname Sailors S, Reserves R 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! v Optimizer must find more efficient strategies to compute same answers. v 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 v v 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! 10

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

Expressions and Strings SELECT S. age, age 1=S. age-5, 2*S. age AS FROM Sailors S WHERE S. sname LIKE ‘B_%B’ v v age 2 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. 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. 13

SQL Queries Using Set Operations 14

Find sid’s of sailors who’ve reserved a red or 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’) v UNION compute union of any two unioncompatible 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’ 15

Find sid’s of sailors who’ve reserved a red and a green boat. v If we replace OR by AND as done below, are the semantics? what 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’) 17

Find sid’s of sailors who’ve reserved a red and a green boat v INTERSECT: compute intersection of two union -compatible sets of tuples. v What happens if change S. sid as S. sname ? Key field! 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’ 18

Find sid’s of sailors who’ve reserved a red and a green boat SELECT S. sid v Could query be written without INTERSECT? 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’ SELECT S. sid FROM Sailors S, Boats B 1, Reserves R 1, Boats B 2, Reserves R 2 WHERE (S. sid=R 1. sid AND R 1. bid=B 1. bid AND B 1. color=‘red’ ) AND (S. sid=R 2. sid AND R 2. bid=B 2. bid AND B 2. color=‘green’) v Do we need Sailors relation in the FROM clauses? 20

Find sid’s of sailors who’ve reserved a red and not a green boat v What if we want to compute the DIFFERENCE instead? v. SQL/92 standard, but some systems don’t support EXCEPT. v. What then? 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’ SELECT S. sid FROM Sailors S, Boats B 1, Reserves R 1, Boats B 2, Reserves R 2 WHERE S. sid=R 1. sid AND R 1. bid=B 1. bid AND S. sid=R 2. sid AND R 2. bid=B 2. bid AND (B 1. color='red' AND NOT(B 2. color = 'green')) 22

Find sid’s of sailors who’ve reserved a red and not a green boat v What if we want to compute the DIFFERENCE instead? v. Except is SQL/92 standard, but some systems don’t. support EXCEPT. 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’ SELECT S. sid FROM Sailors S, Boats B 1, Reserves R 1, Boats B 2, Reserves R 2 WHERE S. sid=R 1. sid AND R 1. bid=B 1. bid AND S. sid=R 2. sid AND R 2. bid=B 2. bid AND (B 1. color='red' AND NOT(B 2. color = 'green')) v Is the above query correct, or not correct ? 23

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

Nested Queries 25

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) v clauses can itself contain another SQL query, called a subquery. powerful feature of SQL. WHERE, FROM, HAVING 26

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) v Nested loops evaluation: § For each Sailors tuple, • Evaluate the WHERE clause qualification • Which here means re-compute subquery. 27

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) v Change query to find sailors who’ve not reserved #103: S. sname FROM Sailors S WHERE S. sid NOT IN (SELECT R. sid FROM Reserves R WHERE R. bid=103) SELECT 28

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) v Could this query be rewritten without nesting? 29

Nested Queries with Correlation Find names of sailors who’ve reserved boat #103: SELECT S. sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R. bid=103 AND v EXISTS S. sid=R. sid) is another set comparison operator, like IN. 30

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

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

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

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 S 2. sid FROM Sailors S 2, Boats B 2, Reserves R 2 WHERE S 2. sid=R 2. sid AND R 2. bid=B 2. bid AND B 2. color=‘green’) 35

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 S 2. sid FROM Sailors S 2, Boats B 2, Reserves R 2 WHERE S 2. sid=R 2. sid AND R 2. bid=B 2. bid AND B 2. color=‘green’) v INTERSECT queries re-written using IN. v Note: This query can return “sname” (and not just “sid) of Sailors who’ve reserved both red and green boats 36

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

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)) 38

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

(1) Division in SQL Find sailors who’ve reserved all boats. v Let’s do it without EXCEPT: 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)) (2) SELECT S. sname FROM Sailors S WHERE NOT EXISTS (SELECT B. bid FROM Boats B WHERE NOT EXISTS (SELECT R. bid Sailors S such that. . . FROM Reserves R there is no boat B without. . . WHERE R. bid=B. bid AND R. sid=S. sid)) a Reserves tuple showing S reserved B 40

Aggregation and Having Clauses 41
![Aggregate Operators v Significant extension of relational algebra COUNT COUNT DISTINCT A Aggregate Operators v Significant extension of relational algebra. COUNT (*) COUNT ( [DISTINCT] A)](https://slidetodoc.com/presentation_image_h/0b759da7f4e3d52590e9ae0454b4ccdf/image-35.jpg)
Aggregate Operators v Significant extension of relational algebra. COUNT (*) COUNT ( [DISTINCT] A) SUM ( [DISTINCT] A) AVG ( [DISTINCT] A) MAX (A) MIN (A) Why no Distinct? 42
![Aggregate Operators COUNT COUNT DISTINCT A SUM DISTINCT A AVG Aggregate Operators COUNT (*) COUNT ( [DISTINCT] A) SUM ( [DISTINCT] A) AVG (](https://slidetodoc.com/presentation_image_h/0b759da7f4e3d52590e9ae0454b4ccdf/image-36.jpg)
Aggregate Operators COUNT (*) COUNT ( [DISTINCT] A) SUM ( [DISTINCT] A) AVG ( [DISTINCT] A) MAX (A) MIN (A) SELECT COUNT (*) FROM Sailors S Why no Distinct? SELECT AVG (S. age) FROM Sailors S WHERE S. rating=10 SELECT COUNT (DISTINCT FROM Sailors S WHERE S. sname=‘Bob’ S. rating) 43

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

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

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

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

Add Group By to SQL 50
![Queries With GROUP BY and HAVING SELECT DISTINCT targetlist FROM relationlist WHERE qualification GROUP Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP](https://slidetodoc.com/presentation_image_h/0b759da7f4e3d52590e9ae0454b4ccdf/image-42.jpg)
Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification v v 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. 51
![Queries With GROUP BY and HAVING SELECT DISTINCT targetlist FROM relationlist WHERE qualification GROUP Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP](https://slidetodoc.com/presentation_image_h/0b759da7f4e3d52590e9ae0454b4ccdf/image-43.jpg)
Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification v target-list contains : (i) attribute names (ii) aggregate-op (column-name) (e. g. , MIN (S. age)). v A group is a set of tuples that each have the same value for all attributes in grouping-list. v REQUIREMENT: - target-list (i) grouping-list. - Why? - Each answer tuple of a group must have single value. 52

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 FROM Sailors S WHERE S. age >= 18 GROUP BY S. rating HAVING COUNT (*) > 1 min-age 53

Group. By --- Conceptual Evaluation 1. 2. 3. 4. 5. 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. (Group. By) 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. 54

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

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

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

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 GROUP BY B. bid v v B. color=‘red’ 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 Group. By can appear in Having, unless in aggregate operator of Having 61

Find age of the youngest sailor with age > 18, for each rating with at least 2 sailors (of any age) v 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 S 2 WHERE S. rating=S 2. rating) Find age of the youngest sailor with age > 18, for each rating with at least 2 such sailors (of age 18) HAVING COUNT(*) >1 62

Find those ratings for which the average is the minimum over all ratings SELECT S. rating FROM Sailors S WHERE S. age = (SELECT MIN (AVG (S 2. age)) FROM Sailors S 2) v WRONG : Aggregate operations cannot be nested! 63

Find those ratings for which the average is the minimum over all ratings v Correct solution (in SQL/92): SELECT Temp. rating, Temp. avg-age FROM (SELECT S. rating, AVG (S. age) AS avg-age FROM Sailors S GROUP BY S. rating) AS Temp WHERE Temp. avg-age = (SELECT MIN (avg-age) FROM (SELECT S. rating, AVG(S. age) as avg_age FROM Sailors S GROUP BY S. rating)) 65

Null Values 66

Null Values v Field values in a tuple are sometimes : § Unknown (e. g. , a rating has not been assigned) or § Inapplicable (e. g. , no maiden-name when a male person) § v SQL provides a special value null for such situations. The presence of null complicates many issues. 67

Comparisons Using Null Values § (S. rating = 8) is TRUE or FALSE ? ? What if rating has null value in tuple ? § We need a 3 -valued logic : condition can be true, false or unknown. § SQL special operators IS NULL to check if value is/is not null. § Disallow NULL value : § rating INTEGER NOT NULL 68

Comparison with NULL values v Arithmetic operations on NULL return NULL. v Comparison operators on NULL return UNKNOWN. v We can explicitly check whether a value is null or not by IS NULL and by IS NOT NULL. 69

Truth table with UNKNOWN AND TRUE = UNKNOWN OR TRUE = TRUE UNKNOWN AND FALSE = FALSE UNKNOWN OR FALSE = UNKNOWN AND UNKNOWN = UNKNOWN OR UNKNOWN = UNKNOWN NOT UNKNOWN = UNKNOWN WHERE clause is satisfied only when it evaluates to TRUE. 70

Outer Joins : Special Operators v v v Left Outer Join Right Outer Join Full Outer Join SELECT S. sid, R. bid FROM Sailors S LEFT OUTER JOIN Reserves R ON S. sid = R. sid Sailors rows (left) without a matching Reserves row (right) appear in result, but not vice versa. SELECT S. sid, R. bid FROM Sailors S NATURAL LEFT OUTER JOIN Reserves R 71

Sorting: ORDER BY clause SELECT * FROM Student WHERE s. Number >= 1 ORDER BY s. Number, s. Name (s. Number, s. Name) ( (s. Number >= 1) (Student)) 72
![SQL Queries Summary SELECT DISTINCT a 1 a 2 an FROM R SQL Queries - Summary SELECT [DISTINCT] a 1, a 2, …, an FROM R](https://slidetodoc.com/presentation_image_h/0b759da7f4e3d52590e9ae0454b4ccdf/image-60.jpg)
SQL Queries - Summary SELECT [DISTINCT] a 1, a 2, …, an FROM R 1, R 2, …, Rm [WHERE C 1] [GROUP BY g 1, g 2, …, gl [HAVING C 2]] [ORDER BY o 1, o 2, …, oj] 73

Summary v SQL was important factor in early acceptance of relational model : easy-to-understand ! v Relationally complete: in fact, significantly more expressive power than relational algebra. v Many alternative ways to write a query. So optimizer must look for most efficient evaluation plan. v In practice, users (still) need to be aware of how queries are optimized and evaluated for best results. 74