eecs 484

Upload: charlie-yan

Post on 04-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 EECS 484

    1/38

    2/6/2013 EECS 484 1

    SQL Queries

    Chapter 5

    Sample Database used in these

    notes: sailors.sql(Posted on Ctools)

  • 7/29/2019 EECS 484

    2/38

    2/6/2013 EECS 484 2

    SQL Query Language

    Implements relational algebra Select, Project, Join, Set operators (we will see

    these in the next lecture)

    And so much more

    Correlated subqueries

    Ordering of results

    Aggregate queries (e.g., SUM, MAX, AVG)

    Three-valued logic for NULL values

    Etc.

  • 7/29/2019 EECS 484

    3/38

    2/6/2013 EECS 484 3

    Basic SQL Query

    SELECT [DISTINCT] attr-list

    FROM relation-list

    WHERE qualification

    Optional

    List of relations

    Attr1 op Attr2

    OPS: , =, =,

    Combine using AND, OR, NOT

    Attributes frominput relations

    (Conceptual)Evaluation:

    1. Take cross-product of relation-list2. Select rows satisfying qualification

    3. Project columns in attr-list

    (eliminate duplicates only if DISTINCT)

    Optimizerchoosesefficient

    plan!!

  • 7/29/2019 EECS 484

    4/38

    2/6/2013 EECS 484 4

    Example of Basic Query

    Schema: Sailors (sid, sname, rating, age) Boats (bid, bname, color)

    Reserves (sid, bid, rday)

    Find the names of sailors who havereserved boat #103

    SELECT S.snameFROM Sailors S, Reserves RWHERE S.sid = R.sid AND R.bid = 103;

  • 7/29/2019 EECS 484

    5/38

    2/6/2013 EECS 484 5

    Example of Basic Query

    sid bid rday

    22 101 10/10

    58 103 11/12

    Reserves

    sid sname rating age

    22 Dustin 7 45

    58 Rusty 10 35

    31 Lubber 8 55

    Sailors

    558Lubber3110/1010122

    457Dustin2211/1210358

    3510Rusty5811/1210358

    58

    22

    22

    sid

    103

    101

    101

    bid

    11/12

    10/10

    10/10

    rday

    3510Rusty58

    8

    7

    rating

    55Lubber31

    45Dustin22

    agesnamesid

    Reserves x Sailors

  • 7/29/2019 EECS 484

    6/38

    2/6/2013 EECS 484 6

    Example of Basic Query

    SELECT DISTINCT snameFROM Sailors S, Reserves RWHERE S.sid = R.sid AND R.bid = 103

    What

    s the effect of adding DISTINCT?

  • 7/29/2019 EECS 484

    7/382/6/2013 EECS 484 7

    Another Example

    Schema: Sailors (sid, sname, rating, age) Boats (bid, bname, color)

    Reserves (sid, bid, day)

    Find the colors of boats reserved by asailor named rusty

    SELECT B.colorFROM Sailors S, Reserves R, Boats BWHERE S.sid = R.sid AND R.bid = B.bid AND

    S.sname = 'Rusty';

  • 7/29/2019 EECS 484

    8/382/6/2013 EECS 484 8

    Note on Range Variables

    Needed when same relation appears twicein FROM clause

    SELECT S1.sname, S2.snameFROM Sailors S1, Sailors S2WHERE S1.age > S2.age;

    What does thisQuery compute?

    Good style to always use range variables anyway

  • 7/29/2019 EECS 484

    9/382/6/2013 EECS 484 9

    ORDER BY clause

    Helps sort the result for presentation

    SELECT S.sname, S.ageFROM Sailors SORDER BY S.age [ASC]

    SELECT S.sname, S.ageFROM Sailors SORDER BY S.age DESC

    Find the names and agesof all sailors, in increasingorder of age

    Find the names and agesof all sailors, in decreasingorder of age

    Attribute(s) in ORDER BY clause must be in SELECT list.

  • 7/29/2019 EECS 484

    10/382/6/2013 EECS 484 10

    ORDER BY clause

    SELECT S.sname, S.age, S.ratingFROM Sailors SORDER 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 ofrating.

    What does this query compute?

  • 7/29/2019 EECS 484

    11/382/6/2013 EECS 484 11

    Union

    Find names of sailors who have reserved ared or a green boat

    SELECT S.snameFROM Sailors S, Reserves R, Boats B

    WHERE S.sid = R.sid AND R.bid = B.bidAND (B.color = 'red' OR B.color = 'green');

    SELECT S.snameFROM Sailors S, Reserves R, Boats B

    WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red'UNIONSELECT S.snameFROM Sailors S, Reserves R, Boats B

    WHERE S.sid = R.sid and R.bid = B.bid AND B.color = 'green';

  • 7/29/2019 EECS 484

    12/382/6/2013 EECS 484 12

    Intersect

    Find sids of sailors who have reserved ared and a green boat

    What is wrong with the above query?

    SELECT S.snameFROM Sailors S, Reserves R, Boats B

    WHERE S.sid = R.sid AND R.bid = B.bidAND (B.color = 'red' AND B.color = 'green');

  • 7/29/2019 EECS 484

    13/382/6/2013 EECS 484 13

    Intersect

    Find sids of sailors who have reserved ared and a green boat

    SELECT S.sidFROM Sailors S, Reserves R, Boats B

    WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red'INTERSECTSELECT S.sidFROM 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)?

  • 7/29/2019 EECS 484

    14/38

    2/6/2013 EECS 484 14

    Intersect on Non-Key

    Find the names of sailors who havereserved a red and a green boat

    SELECT S.snameFROM Sailors S, Reserves R, Boats B

    WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red'INTERSECTSELECT S.snameFROM Sailors S, Reserves R, Boats B

    WHERE S.sid = R.sid and R.bid = B.bid AND B.color = 'green'

    What is wrong with the above query?

  • 7/29/2019 EECS 484

    15/38

    2/6/2013 EECS 484 15

    Intersect on non-key

    Find the names of sailors who havereserved a red and a green boat

    CREATE VIEW RedGreenSailors ASSELECT S.sidFROM Sailors S, Reserves R, Boats B

    WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red'INTERSECTSELECT S.sidFROM Sailors S, Reserves R, Boats BWHERE S.sid = R.sid and R.bid = B.bid AND B.color = 'green';

    SELECT S.sname FROM

    Sailors S, RedGreenSailors R WHERE

    S.sid = R.sid;

    DROP VIEW RedGreenSailors;

  • 7/29/2019 EECS 484

    16/38

    2/6/2013 EECS 484 16

    Intersect with non-key

    Find the names of sailors who havereserved a red and a green boat. Get rid ofthe VIEW.

    SELECT S.sname FROM

    Sailors S,

    (SELECT S.sidFROM Sailors S, Reserves R, Boats BWHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red'INTERSECTSELECT S.sidFROM Sailors S, Reserves R, Boats B

    WHERE S.sid = R.sid and R.bid = B.bid AND B.color = 'green')RedGreenSailors WHERES.sid = RedGreenSailors.sid;

    Another sol:See Q8 inCh. 5, p. 150

  • 7/29/2019 EECS 484

    17/38

    2/6/2013 EECS 484 17

    Nested Queries

    Query with another query embedded insideSELECT S.snameFROM Sailors SWHERE S.sid IN (SELECT R.sid

    FROM Reserves RWHERE R.bid=103)

    What does thisCompute?

    Conceptual evaluation: For each row of Sailors,evaluate the subquery over reserves

    To find sailors who have not reserved 103, use NOT IN

  • 7/29/2019 EECS 484

    18/38

    2/6/2013 EECS 484 18

    More Set Operators

    Also EXCEPT (set difference) (Painful) SQL oddities

    For UNION, default is to eliminate duplicates!

    To keep duplicates, must specify UNION ALL

  • 7/29/2019 EECS 484

    19/38

    2/6/2013 EECS 484 19

    Over-Use of Nesting

    Common error by novice SQLprogrammers.

    Query optimizers not as good at optimizingqueries across nesting boundaries.

    Try hard first to write non-nested.

    SELECT DISTINCT S.sname

    FROM Sailors S, Reserves RWHERE S.sid = R.sid AND R.bid = 103;

  • 7/29/2019 EECS 484

    20/38

    2/6/2013 EECS 484 20

    More Set Comparison Operators

    Weve already seen IN, EXISTS. Can also use NOT IN,NOT EXISTS.

    Also available: opANY, opALL, op is , , =,

    Find sailors whose rating is greater than that ofall

    sailors called Horatio:SELECT *FROM Sailors SWHERE S.rating >ALL (SELECT S2.rating

    FROM Sailors S2WHERE S2.sname='Horatio')

    What if we replace ALL with ANY?

  • 7/29/2019 EECS 484

    21/38

    2/6/2013 EECS 484 21

    Aggregate OperatorsCOUNT (*)COUNT ( [DISTINCT] A)SUM ( [DISTINCT] A)

    AVG ( [DISTINCT] A)MAX (A) Can use DistinctMIN (A) Can use Distinct

    SELECT AVG (S.age)FROM Sailors SWHERE S.rating=10

    SELECT COUNT (*) FROM Sailors S

    SELECT AVG ( DISTINCT S.age)FROM Sailors SWHERE S.rating=10

    SELECT S.sname FROM Sailors SWHERE S.rating= (SELECT MAX(S2.rating) FROM Sailors S2)

    single column

    SELECT COUNT (DISTINCT S.name)FROM Sailors S

  • 7/29/2019 EECS 484

    22/38

    2/6/2013 EECS 484 22

    Aggregate Query - Example

    Find name and age of oldest sailor(s)SELECT S.sname, MAX (S.age)FROM Sailors S

    SELECT S.sname, S.ageFROM Sailors SWHERE S.age =

    (SELECT MAX (S2.age)

    FROM Sailors S2)

    How many tuplesin the result?

  • 7/29/2019 EECS 484

    23/38

    2/6/2013 EECS 484 23

    GROUP BY

    Conceptual evaluation Partition data into groups according to somecriterion

    Evaluate the aggregate for each group

    Example: For each rating level, find the ageof the youngest sailor

    SELECT MIN (S.age), S.ratingFROM Sailors SGROUP BY S.rating

    How many tuplesin the result?

  • 7/29/2019 EECS 484

    24/38

    2/6/2013 EECS 484 24

    GROUP BY and HAVING

    SELECT [DISTINCT] target-listFROM relation-listWHERE qualificationGROUP BY grouping-listHAVING group-qualification

    Conceptual Evaluation:

    1. Eliminate tuples that dont satisfy qualification

    2. Partition remaining data into groups3. Eliminate groups according to group-qualification

    4. Evaluate aggregate operation(s) for each group

    Target-list contains:Attribute names (subsetof grouping-list)Aggregate operations(e.g., min(age))

  • 7/29/2019 EECS 484

    25/38

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

    SELECT S.rating, MIN (S.age)FROM Sailors SWHERE S.age >= 18GROUP BY S.ratingHAVING COUNT (*) >= 2

    sid sname rating age

    22 dustin 7 45.031 lubber 8 55.5

    71 zorba 10 16.0

    64 horatio 7 35.0

    29 brutus 1 33.058 rusty 10 35.0

    rating age

    1 33.0

    7 45.0

    7 35.0

    8 55.5

    10 35.0

    rating

    7 35.0

    Answer relation

    rating

  • 7/29/2019 EECS 484

    26/38

    2/6/2013 EECS 484 26

    NULL Values in SQL NULL represents unknown

    or inapplicable Query evaluation

    complications Q: Is (rating > 10) true when

    rating is NULL?

    A: Condition evaluates tounknown (not T or F)

    What about AND, ORconnectives?

    Need 3-valued logic WHERE clause eliminates

    rows that dont evaluate totrue

    p q p AND q p OR q

    T T T T

    T F F T

    T U U T

    F T F T

    F F F F

    F U F U

    U T U T

    U F F U

    U U U U

  • 7/29/2019 EECS 484

    27/38

    2/6/2013 EECS 484 27

    NULL Values Example

    sid sname rating age

    22 dustin 7 45

    58 rusty 10 NULL

    31 lubber 8 55

    sailorsWhat does this queryreturn?

    SELECT snameFROM sailors

    WHERE age > 45OR age

  • 7/29/2019 EECS 484

    28/38

    2/6/2013 EECS 484 28

    NULL Values in Aggregates

    NULL values generally ignored whencomputing aggregates

    Modulo some special cases (see textbook)

    SELECT AVG(age)FROM sailors sid sname rating age

    22 dustin 7 45

    58 rusty 10 NULL31 lubber 8 55

    sailors

    Returns 50!

  • 7/29/2019 EECS 484

    29/38

    2/6/2013 EECS 484 29

    Outer Join*

    sid bid day

    22 101 10/10/99

    Reservessid sname rating age

    22 dustin 7 45.0

    58 rusty 10 35.0

    Sailors

    Select S.sid, R.sidFrom Sailors S NATURAL LEFT OUTER JOIN Reserves R

    sid bid

    22 101

    58 null

    ResultSimilarly:

    RIGHT OUTER JOIN FULL OUTER JOIN

    For each red boat find the number of

  • 7/29/2019 EECS 484

    30/38

    2/6/2013 EECS 484 30

    For each red boat, find the number ofreservations for this boat*

    SELECT B.bid, COUNT (*) AS scountFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sidAND R.bid=B.bidAND B.color='red'

    GROUP BY B.bid

    SELECT B.bid, COUNT (*) AS scountFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sidAND R.bid=B.bidGROUP BY B.bid -- Would this work?HAVING B.color = 'red' --note: one color per bid

  • 7/29/2019 EECS 484

    31/38

    Subtle Errors

    Find the sid of sailors who have reservedexactly one boat.

    SELECT S1.sid

    FROM Sailors S1EXCEPT

    SELECT R1.sid

    FROM Reserves R1, Boats B1, Reserves R2, Boats B2 WHERE R1.sid=R2.sid ANDR1.bid=B1.bid

    AND R2.bid=B2.bid AND R1.bidR2.bid;

    There is a subtle error in the above.

    2/6/2013 EECS 484 31

  • 7/29/2019 EECS 484

    32/38

    Error fixed

    Find the sid of sailors who have reservedexactly one boat.

    SELECT R3.sid

    FROM Reserves R3MINUS

    SELECT R1.sid

    FROM Reserves R1, Boats B1, Reserves R2, Boats B2 WHERE R1.sid=R2.sid ANDR1.bid=B1.bid

    AND R2.bid=B2.bid AND R1.bid R2.bid;

    2/6/2013 EECS 484 32

  • 7/29/2019 EECS 484

    33/38

    Another Solution

    Find the sid of sailors who have reservedexactly one boat.

    SELECT S.sid FROM Sailors S, Boats B, Reserves R

    WHERES.sid = R.sid AND B.bid = R.bid

    GROUP BY S.sid

    HAVING COUNT(*) = 1;

    2/6/2013 EECS 484 33

    Fi d h f h il i h 18

  • 7/29/2019 EECS 484

    34/38

    2/6/2013 EECS 484 34

    Find the age of the youngest sailor with age>18,for each rating with at least 2 sailors (of any age)

    Subquery in the HAVING clause

    Compare this with the query where we consideredonly ratings with 2 sailors over 18!

    SELECT S.rating, MIN (S.age) AS MINAGEFROM Sailors SWHERE S.age > 18GROUP BY S.rating

    HAVING 1 < (SELECT COUNT (*) FROM Sailors S2WHERE S2.rating=S.rating)

    i d i f hi h h i h

  • 7/29/2019 EECS 484

    35/38

    2/6/2013 EECS 484 35

    Find ratings for which the average age is theminimum of the average age over all ratings*

    Aggregate operations cannot be nested! WRONG:SELECT S.ratingFROM Sailors SWHERE AVG(S.age) =

    (SELECT MIN (AVG (S2.age)) FROM Sailors S2)

    SELECT T.rating, T.avgageFROM (SELECT S.rating,AVG (S.age) AS avgage FROM Sailors S

    GROUP BY S.rating) TWHERE T.avgage = (SELECT MIN (T.avgage) FROM T);

    Correct solution (in SQL/92 may fail in practice):

    If above does not work, one solution is to define T as a view .

  • 7/29/2019 EECS 484

    36/38

    Solution Using Views

    2/6/2013 EECS 484 36

    CREATE VIEW AVG_AGE_BY_RATING ASSELECT S.rating, AVG(S.age) AS avgageFROM Sailors S;

    SELECT T.rating, T.avgage FROMAVG_AGE_BY_RATING TWHERE T.avgage= (SELECT MIN(A.avgage) FROM

    AVG_AGE_BY_RATING A);

  • 7/29/2019 EECS 484

    37/38

    Triggers, general ICs

    Review the end of lecture 4Avoid triggers whenever possible

    Why?

    2/6/2013 EECS 484 37

  • 7/29/2019 EECS 484

    38/38

    Suggested Review

    Exercises 5.1, 5.3, 5.7