SQL (iii) Asst. Prof. Lipyeow Lim InformaFon & Computer Science Department University of Hawaii at Manoa 1 Lipyeow Lim -‐-‐ University of Hawaii at Manoa
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)
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
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
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 )
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
• [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
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:
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 )
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?
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:
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
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);
• 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’;
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 );