Download presentation
Presentation is loading. Please wait.
1
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.
2
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.
3
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
4
accountIdlastNamefirstNamestreetcitystatezipcodebalance 101BlockJane345 Randolph CircleApopkaFL30458-$0.00 102HamiltonCherry3230 Dade St.Dade CityFL30555-$3.47 103HarrisonKatherine103 Landis HallBrattFL30457-$30.57 104BreauxCarroll76 Main St.ApopkaFL30458-$34.58 106MorehouseAnita9501 Lafayette St.HoumaLA44099-$0.00 111DoeJane123 Main St.ApopkaFL30458-$0.00 201GreavesJoseph14325 N. Bankside St.GodfreyIL43580-$0.00 444DoeJaneCawthon Dorm, room 642TallahasseeFL32306-$0.00 445RiccardiGreg101 Thanet St.LondonFL33333-$0.00 446RiccardiGreg101 Thanet St.LondonFL33333-$0.00 447RiccardiGreg101 Thanet St.LondonFL33333-$0.00 448MylopoulosJanet4402 Elm St.ApopkaFL33455-$0.00 449MylopoulosJanet4402 Elm St.ApopkaFL33455-$0.00 450MylopoulosJanet4402 Elm St.ApopkaFL33455-$0.00 451O'ConnellJanet4402 Elm St.ApopkaFL33455-$0.00 452O'ConnellJanet4402 Elm St.ApopkaFL33455-$0.00 Customer ssnlastNamefirstName 145-09-0967UnoJane 245-11-4554ToulouseJie 376-77-0099ThreatAyisha 479-98-0098FortuneJulian 579-98-8778FivozinskyBruce Employee
5
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
6
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;
7
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
8
accountIdvideoIddateRenteddateDuecost 1031011/3/20021/4/2002$1.59 1011114/24/20025/2/2002$3.99 1011124/24/20024/30/2002$1.99 1011132/22/20022/25/2002$3.00 1011142/22/20022/25/2002$3.00 10312312/1/200112/31/2001$10.99 1011452/14/20022/16/2002$1.99 101775644/24/2002 101909871/1/20021/8/2002$2.99 101997871/1/20021/4/2002$3.49 Rental accountIdvideoIddateRenteddateReturnedcost 101 12/9/200112/10/2001$2.49 1011121/13/20011/4/2001$1.99 1011131/15/2002 $0.99 10211312/1/200112/3/2001$2.49 11110112/4/200112/6/2001$2.49 111997871/1/20021/4/2002$3.95 20111312/9/200112/14/2001$3.99 201775641/14/20021/24/2002$3.35 PreviousRental
9
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
10
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
11
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
12
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);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.