Upcoming Work Homework #3 due today by 4:30 Homework #4 due Wed Nov 22. Midterm #2 on Wed Nov 29. It will be open book/note (no computers) If you get a better score on the second than the first then that score will replace your first midterm score. Final Project Due Dec 7 SQL chapters 7-10 this week Mike Stafford guest lecture Mon Nov 20.
SQL Set operations Remember union, intersection, and difference from relational algebra? We can do those same set operations with SQL. The results are unique sets of rows. With Access, we have a union (and union all) but not intersect or except (difference). We can create intersections and differences with IN and NOT IN predicates. Tables must be union compatible to be eligible for set operations. must have same number of columns must have compatible data types (number types, text types) You can do this by using whole tables or taking subsets of tables.
Union vs. Join What’s the difference between a union and a join? all columns compatible instead of just join column number columns must be equal with union duplicates removed with union Union is used to implement FULL OUTER JOIN in Access SELECT with left outer join UNION SELECT with right outer join
accountIdlastNamefirstNamestreetcitystatezipcodebalance 101BlockJane345 Randolph CircleApopkaFL30458-$ HamiltonCherry3230 Dade St.Dade CityFL30555-$ HarrisonKatherine103 Landis HallBrattFL30457-$ BreauxCarroll76 Main St.ApopkaFL30458-$ MorehouseAnita9501 Lafayette St.HoumaLA44099-$ DoeJane123 Main St.ApopkaFL30458-$ GreavesJoseph14325 N. Bankside St.GodfreyIL43580-$ DoeJaneCawthon Dorm, room 642TallahasseeFL32306-$ RiccardiGreg101 Thanet St.LondonFL33333-$ RiccardiGreg101 Thanet St.LondonFL33333-$ RiccardiGreg101 Thanet St.LondonFL33333-$ MylopoulosJanet4402 Elm St.ApopkaFL33455-$ MylopoulosJanet4402 Elm St.ApopkaFL33455-$ MylopoulosJanet4402 Elm St.ApopkaFL33455-$ O'ConnellJanet4402 Elm St.ApopkaFL33455-$ O'ConnellJanet4402 Elm St.ApopkaFL33455-$0.00 Customer ssnlastNamefirstName UnoJane ToulouseJie ThreatAyisha FortuneJulian FivozinskyBruce Employee
Union SELECT c.firstname, c.lastname FROM Customer c union SELECT e.firstName, e.lastName FROM Employee e; firstnamelastname AnitaMorehouse AyishaThreat BruceFivozinsky CarrollBreaux CherryHamilton GregRiccardi JaneBlock JaneDoe JaneUno JanetMylopoulos JanetO'Connell JieToulouse JosephGreaves JulianFortune KatherineHarrison
Union All firstnamelastname JaneBlock CherryHamilton KatherineHarrison CarrollBreaux AnitaMorehouse JaneDoe JosephGreaves JaneDoe GregRiccardi GregRiccardi GregRiccardi JanetMylopoulos JanetMylopoulos JanetMylopoulos JanetO'Connell JanetO'Connell JaneUno JieToulouse AyishaThreat JulianFortune BruceFivozinsky Duplicates not removed. SELECT c.firstname, c.lastname FROM Customer c union all SELECT e.firstName, e.lastName FROM Employee e;
INTERSECTION & DIFFERENCE Access doesn’t have SQL EXCEPT (DIFFERENCE) or INTERSECT so we have to use IN and NOT IN. Two sets A and B. Intersection of AB? SELECT FROM A WHERE also IN B A-B? SELECT FROM A WHERE NOT IN B
accountIdvideoIddateRenteddateDuecost /3/20021/4/2002$ /24/20025/2/2002$ /24/20024/30/2002$ /22/20022/25/2002$ /22/20022/25/2002$ /1/200112/31/2001$ /14/20022/16/2002$ /24/ /1/20021/8/2002$ /1/20021/4/2002$3.49 Rental accountIdvideoIddateRenteddateReturnedcost /9/200112/10/2001$ /13/20011/4/2001$ /15/2002 $ /1/200112/3/2001$ /4/200112/6/2001$ /1/20021/4/2002$ /9/200112/14/2001$ /14/20021/24/2002$3.35 PreviousRental
In predicate Find Customers (accountId, lastname and firstname) who have previously rented video number 101 using In predicate. SELECT c.accountId, c.lastName, c.firstName FROM Customer c WHERE c.accountId IN (SELECT p.accountId FROM previousRental p WHERE videoId=101); accountIdlastNamefirstName 101BlockJane 111DoeJane
NOT IN Find the customers who have not previously rented video 101. SELECT c.accountId, c.lastName, c.firstName FROM Customer c WHERE c.accountId NOT IN (SELECT p.accountId FROM previousRental p WHERE videoId=101); accountIdlastNamefirstName 102HamiltonCherry 103HarrisonKatherine 104BreauxCarroll 106MorehouseAnita 201GreavesJoseph 444DoeJane 445RiccardiGreg 446RiccardiGreg 447RiccardiGreg 448MylopoulosJanet 449MylopoulosJanet 450MylopoulosJanet 451O'ConnellJanet 452O'ConnellJanet
A more complicated query Find customers who have previously or are currently renting video 101. SELECT u.accountId, lastName, firstName FROM ((SELECT p.accountId, p.videoId FROM previousRental p UNION SELECT r.accountId, r.videoId FROM rental r) u INNER JOIN Customer c ON u.accountId=c.accountId) WHERE videoId=101; accountIdlastNamefirstName 101BlockJane 103HarrisonKatherine 111DoeJane
Same query with IN SELECT c.accountId, c.lastName, c.firstName FROM Customer c WHERE c.accountId IN (SELECT r.accountId FROM Rental r WHERE r.videoId=101) OR c.accountId IN (SELECT p.accountId FROM PreviousRental p WHERE p.videoId=101);