Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SQL 3

Lipyeow
August 17, 2015

SQL 3

SQL

Lipyeow

August 17, 2015
Tweet

More Decks by Lipyeow

Other Decks in Technology

Transcript

  1. ICS  321  Data  Storage  &  Retrieval   The  Database  Language

     SQL  (iii)   Asst.  Prof.    Lipyeow  Lim   InformaFon  &  Computer  Science  Department   University  of  Hawaii  at  Manoa   1   Lipyeow  Lim  -­‐-­‐  University  of  Hawaii  at  Manoa  
  2. Bag  SemanFcs  in  SQL   •  SELECT-­‐FROM-­‐WHERE  statements  preserve  

    duplicates  by  default,  unless  DISTINCT  is  given.   •  Set  operators  UNION,  INTERSECT,  EXCEPT  use  set   semanFcs  by  default!   •  To  use  bag  semanFcs:  UNION  ALL,  INTERSECT   ALL,  EXCEPT  ALL.   Lipyeow  Lim  -­‐-­‐  University  of  Hawaii  at  Manoa   2   (SELECT title, year FROM Movies) UNION ALL (SELECT movieTitle AS title, movieYear AS year FROM StarsIn)
  3. Aggregate  Operators   •  SQL  supports  5  aggregaFon  operators  on

     a   column,  say  A,   1.  COUNT  (  *  ),  COUNT  (  [DISTINCT]  A  )   2.  SUM  (  [DISTINCT]  A  )   3.  AVG  (  [DISTINCT]  A  )   4.  MAX  (  A  )   5.  MIN  (  A  )     Lipyeow  Lim  -­‐-­‐  University  of  Hawaii  at  Manoa   3  
  4. AggregaFon  Queries   •  Q25:  Find  the  average  age  of

     all  sailors   Lipyeow  Lim  -­‐-­‐  University  of  Hawaii  at  Manoa   4   SELECT AVG(S.age) FROM Sailors S •  Q28:  Count  the  number  of  sailors   SELECT COUNT (*) FROM Sailors S •  Find  the  age  of  the  oldest  sailor   SELECT MAX (S.age) FROM Sailors S
  5. Q27:  Find  the  name  and  age  of  the   oldest

     sailor   •  If  there  is  an  aggregaFon  operator  in  the  SELECT   clause,  then  it  can  only  have  aggregaFon  operators   unless  the  query  has  a  GROUP  BY  clause    -­‐-­‐  first  query   is  illegal.   Lipyeow  Lim  -­‐-­‐  University  of  Hawaii  at  Manoa   5   SELECT S.sname, MAX (S.age) FROM Sailors S SELECT S.sname, S.age FROM Sailors S WHERE S.age = ( SELECT MAX(S2.age) FROM Sailors S2 )
  6. Queries  with  GROUP  BY  and  HAVING   •  The  target-­‐list

     contains  (i)  acribute  names    (ii)  terms   with  aggregate  operaFons  (e.g.,  MIN  (S.age)).   –  The  list  of  acribute  names  in  (i)  must  be  a  subset  of   grouping-­‐list.       –  IntuiFvely,  each  answer  tuple  corresponds  to  a  group,  and   these  acributes  must  have  a  single  value  per  group.       –  A  group  is  a  set  of  tuples  that  have  the  same  value  for  all   acributes  in  grouping-­‐list.   Lipyeow  Lim  -­‐-­‐  University  of  Hawaii  at  Manoa   6   SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification
  7. Conceptual  EvaluaFon  Strategy  with   GROUP  BY  and  HAVING  

    •  [Same  as  before]  The  cross-­‐product  of  rela2on-­‐list  is   computed,  tuples  that  fail  qualifica2on  are  discarded,   `unnecessary’  fields  are  deleted   •  The  remaining  tuples  are  parFFoned  into  groups  by  the   value  of  acributes  in  grouping-­‐list.       •  The  group-­‐qualifica2on  is  then  applied  to  eliminate  some   groups.    Expressions  in  group-­‐qualifica2on  must  have  a   single  value  per  group!   –  In  effect,  an  acribute  in  group-­‐qualifica2on  that  is  not  an   argument  of  an  aggregate  op  also  appears  in  grouping-­‐list.    (SQL   does  not  exploit  primary  key  semanFcs  here!)   •  AggregaFons  in  target-­‐list  are  computed  for  each  group   •  One  answer  tuple  is  generated  per  qualifying  group   Lipyeow  Lim  -­‐-­‐  University  of  Hawaii  at  Manoa   7  
  8. Q32:  Find  age  of  the  youngest  sailor  with  age  >=

      18,  for  each  raFng  with  at  least  2  such  sailors   Lipyeow  Lim  -­‐-­‐  University  of  Hawaii  at  Manoa   8   SELECT S.rating, MIN(S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 Answer relation: Sailors instance:
  9. Conceptual  EvaluaFon  for  Q32   Lipyeow  Lim  -­‐-­‐  University  of

     Hawaii  at  Manoa   9   Partition or GROUP BY Eliminate groups Using HAVING clause Perform aggregation on each group
  10. EVERY  and  ANY  in  HAVING  clauses   •  EVERY:  every

     row  in  the  group  must  saFsfy  the   acached  condiFon   •  ANY:  at  least  one  row  in  the  group  need  to   saFsfy  the  condiFon   Lipyeow  Lim  -­‐-­‐  University  of  Hawaii  at  Manoa   10   SELECT S.rating, MIN(S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 AND EVERY ( S.age <=60 )
  11. Conceptual  EvaluaFon  with  EVERY   Lipyeow  Lim  -­‐-­‐  University  of

     Hawaii  at  Manoa   11   Partition or GROUP BY Eliminate groups Using HAVING clause Perform aggregation on each group HAVING COUNT (*) > 1 AND EVERY (S.age <=60) What  is  the  result  of     changing  EVERY  to  ANY?  
  12. Find  age  of  the  youngest  sailor  for  each  raFng  with

     at   least  2  sailors  between  18  and  60   Lipyeow  Lim  -­‐-­‐  University  of  Hawaii  at  Manoa   12   SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 AND S.age <= 60 GROUP BY S.rating HAVING COUNT (*) > 1 Answer relation: Sailors instance:
  13. Outer  Joins   •  Regular  join  on  sid:  Sailor  Lubber

     gets  dropped.   •  Outer  join:  Sailor  rows  without  a  matching  Reserves   row  appear  exactly  once  in  the  result,  with  the   columns  inherited  from  Reserves  taking  null  values.   •  Le;  Outer  Join  :  Sailor  rows  w/o  matching  reservaFons   appear  in  the  result,  but  not  vice  versa   •  Right  Outer  Join:  ReservaFons  w/o  matching   reservaFons  appear  in  the  result,  but  not  vice  versa   Lipyeow  Lim  -­‐-­‐  University  of  Hawaii  at  Manoa   13   sid   sname   ra?ng   age   22   DusFn   7   45.0   31   Lubber   8   55.0   58   Rusty   10   35.0   sid   bid   day   22   101   10/10/96   58   103   11/12/96   S1 R1
  14. Example  of  outer  join   Lipyeow  Lim  -­‐-­‐  University  of

     Hawaii  at  Manoa   14   sid   sname   ra?ng   age   22   DusFn   7   45.0   31   Lubber   8   55.5   58   Rusty   10   35.0   sid   bid   day   22   101   10/10/96   58   103   11/12/96   S1 R1 SELECT S1.*, R1.* FROM Sailors S1 NATURAL OUTER JOIN Reserves R1 sid   sname   ra?ng   age   sid   bid   day   22   DusFn   7   45   22   101   10/10/96   31   Lubber   8   55.5   NULL   NULL   NULL   58   Rusty   10   35.0   58   103   11/12/96   Result •  Note  the  nulls  
  15. InserFon   •  If  inserFng  results  from  a  query,  query

     must   be  evaluated  prior  to  actual  inserFon   Lipyeow  Lim  -­‐-­‐  University  of  Hawaii  at  Manoa   15   INSERT INTO R(A1, A2, ...) VALUES (v1, v2, ...); INSERT INTO Studio(name) SELECT DISTINCT studioname FROM Movies WHERE studioname NOT IN (SELECT name FROM Studio);
  16. DeleFon   •  DeleFon  specified  using  a  where  clause.  

    •  To  delete  a  specific  tuple,  you  need  to  use  the   primary  key  or  candidate  keys.   Lipyeow  Lim  -­‐-­‐  University  of  Hawaii  at  Manoa   16   DELETE FROM R WHERE <condition>; DELETE FROM StarsIn WHERE movieTitle = ‘The Maltese Falcon’ AND MovieYear = 1942 AND starName=‘Sydney Greenstreet’;
  17. Updates   •  Tuples  to  be  updated  are  specified  using

     a  where   clause.   •  To  update  a  specific  tuple,  you  need  to  use  the   primary  key  or  candidate  keys.   Lipyeow  Lim  -­‐-­‐  University  of  Hawaii  at  Manoa   17   UPDATE R SET <new value assignments> WHERE <condition>; UPDATE MovieExec SET name=‘Pres. ‘ || name WHERE cert# IN ( SELECT presC# FROM Studio );