SQL (i) Asst. Prof. Lipyeow Lim InformaFon & Computer Science Department University of Hawaii at Manoa 1 Lipyeow Lim -‐-‐ University of Hawaii at Manoa
relaFon names (possibly with a range-‐variable a[er each name). • target-‐list A list of a\ributes of relaFons in rela%on-‐list • qualifica%on Comparisons (A\r op const or A\r1 op A\r2, where op is one of <, >, ≤, ≥, =, ≠) combined using AND, OR and NOT. • DISTINCT is an opFonal keyword indicaFng that the answer should not contain duplicates. Default is that duplicates are not eliminated! Lipyeow Lim -‐-‐ University of Hawaii at Manoa 3 SELECT [ DISTINCT ] target-list FROM relation-list WHERE qualification
the same relaFon appears twice in the FROM clause. • Good style to always use range variables Lipyeow Lim -‐-‐ University of Hawaii at Manoa 4 SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND bid=103 SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103 Without range variables
defined in terms of the following conceptual evaluaFon strategy: 1. Compute the cross-‐product of rela%on-‐list. 2. Discard resulFng tuples if they fail qualifica%ons. 3. Delete a\ributes that are not in target-‐list. 4. If DISTINCT is specified, eliminate duplicate rows. • This strategy is probably the least efficient way to compute a query! An opFmizer will find more efficient strategies to compute the same answers. Lipyeow Lim -‐-‐ University of Hawaii at Manoa 5
• Would adding DISTINCT to this query make a difference? • What is the effect of replacing S.sid by S.sname in the SELECT clause? Would adding DISTINCT to this variant of the query make a difference? Lipyeow Lim -‐-‐ University of Hawaii at Manoa 7 sid bid day 22 101 10/10/96 58 103 11/12/96 sid sname ra,ng age 22 DusFn 7 45.0 31 Lubber 8 55.5 58 Rusty 10 35.0 R1 S1 SELECT S1.sid FROM Sailors S1, Reserves R1 WHERE S1.sid=R1.sid
Lipyeow Lim -‐-‐ University of Hawaii at Manoa 8 sid bid day 22 101 10/10/96 58 103 11/12/96 sid sname ra,ng age 22 DusFn 7 45.0 31 Lubber 8 55.5 58 Rusty 10 35.0 R1 S1 SELECT B1.color FROM Sailors S1, Reserves R1, Boats B1 WHERE S1.sid=R1.sid AND R1.bid=B1.bid AND S1.sname=‘Lubber’ bid bname color 101 Interlake Blue 102 Interlake Red 103 Clipper green 104 Marine Red B1
can also contain arithmeFc or string expressions over the column names • Example: compute a new ``age adjusted’’ raFng for each sailor whose raFng saFsfies a special formula Lipyeow Lim -‐-‐ University of Hawaii at Manoa 9 SELECT S1.sname, S1.rating * S1.age / 100 AS NewRating FROM Sailors S1 WHERE S1.rating – 5.0 > S1.age / 12.0 sid sname ra,ng age 22 DusFn 7 45.0 31 Lubber 8 55.5 58 Rusty 10 35.0 S1
involving a NULL is always NULL • The result of any comparison operator like =,>,< is always UNKNOWN Lipyeow Lim -‐-‐ University of Hawaii at Manoa 10 SELECT S1.sname, FROM Sailors S1 WHERE S1.rating – 5.0 > 0 sid sname ra,ng age 22 DusFn NULL 45.0 31 Lubber 8 55.5 58 Rusty 10 35.0 S1
= 0, UNKNOWN=0.5 – AND : min, OR : max, NOT : 1-‐v Lipyeow Lim -‐-‐ University of Hawaii at Manoa 11 X Y T T T U T F U T U U U F F T F U F F X AND Y T U F U U F F F F X OR Y T T T T U U T U F NOT X F F F U U U T T T
comparisons operators ( <, >, =, etc), but take note of collaFons – i.e. determines the ordering. Lexicographic, languages etc • SQL supports pa\ern matching via the LIKE operator and wildcards – ``%’’ : zero or more arbitrary chars – ``_’’ : any one char Lipyeow Lim -‐-‐ University of Hawaii at Manoa 12 SELECT S1.sname, S1.rating FROM Sailors S1 WHERE S1.sname LIKE `L_%’ sid sname ra,ng age 22 DusFn 7 45.0 31 Lubber 8 55.5 58 Rusty 10 35.0 S1
specified using strings and “cast” into the date/Fme datatypes using funcFons. Lipyeow Lim -‐-‐ University of Hawaii at Manoa 13 SELECT R* FROM Reserves R WHERE R.day = DATE ‘2010-10-02’ TIME ’15:00:02.5’ TIMESTAMP ‘2010-10-02 15:00:02’ Cast Date string
result of the SQL query according to the given column(s). Lipyeow Lim -‐-‐ University of Hawaii at Manoa 14 SELECT S1.sname, S1.rating FROM Sailors S1 ORDER BY S1.rating DESC sid sname ra,ng age 22 DusFn 7 45.0 31 Lubber 8 55.5 58 Rusty 10 35.0 S1 sname ra,ng Rusty 10 Lubber 8 DusFn 7