? Can we use a LEFT JOIN with Reservation but throw out the matched row? videoIdstoreId"> ? Can we use a LEFT JOIN with Reservation but throw out the matched row? videoIdstoreId">
Download presentation
Presentation is loading. Please wait.
1
Another Join? Find videoIds and storeIds for all copies of Annie Hall which aren’t reserved. We can do a join of movie and video on movieId where title is Annie Hall to get videoIds of all the Annie Hall videos. Then we can do an outer join with reservation to get those that aren’t reserved? Or do we try a non-equal join to get those that aren’t reserved?
2
Account NumberVideo IdDate Reserved 1011111/30/2000 11:12:42 PM 101775641/30/2000 11:12:41 PM Video videoIddateAcquiredmovieIdstoreId 1011/25/19981013 1112/5/19971233 11212/31/19951235 1134/5/19981235 1144/5/19981895 1233/25/19861233 1455/12/19951455 775644/29/19911893 909873/25/19994503 9978710/10/19979875 movieIdtitlegenrelengthrating 101The Thirty-Nine Stepsmystery101R 123Annie Hallromantic comedy110R 145Lady and the Trampanimated comedy93PG 189Animal Housecomedy87PG-13 450Elizabethcostume drama123PG-13 553Stagecoachwestern130R 987Duck Soupcomedy99PG-13 Movie Reservation
3
INNER JOIN SELECT v.videoId, v.storeId FROM video v INNER JOIN movie m ON v.movieId=m.movieId WHERE m.title="Annie Hall"; That gives us all Annie Hall videos. Now we just need to get rid of those that are on reserve (video 111). Can we use an INNER JOIN with <>? Can we use a LEFT JOIN with Reservation but throw out the matched row? videoIdstoreId 1113 1125 1135 1233
4
LEFT JOIN SELECT v.videoId, v.storeId FROM (video v INNER JOIN movie m ON v.movieId=m.movieId) LEFT JOIN Reservation r ON v.videoId=r.videoId WHERE m.title="Annie Hall" AND ISNULL (r.videoId); How the heck does this work? videoIdstoreId 1125 1135 1233
5
Not the way I’d approach it I’d use NOT IN. SELECT v.videoId, v.storeId FROM video v INNER JOIN movie m ON v.movieId=m.movieId WHERE m.title="Annie Hall" AND v.videoId NOT IN (SELECT videoId FROM Reservation); videoIdstoreId 1125 1135 1233
6
SQL Functions Two basic types of functions: Aggregate (or group) functions which operate on a set of rows. These include COUNT, SUM, AVG, MIN, MAX, FIRST and LAST functions. Also TOP (PERCENT), BOTTOM, DISTINCT, DISTINCTROW. Row level functions which operate on single rows at a time. These include numeric ones such as addition (+) subtraction (-), etc, ROUND, and string and date functions.
7
Aggregate Functions 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 SELECT COUNT(cost), AVG(cost), MAX(cost), MIN(cost) FROM PreviousRental; Expr1000Expr1001Expr1002Expr1003 8$2.72$3.99$0.99
8
Aggregate with selection NUMAVGMAXMIN 6$3.13$3.99$2.49 SELECT COUNT(cost) AS NUM, AVG(cost) AS AVG, MAX(cost) AS MAX, MIN(cost) AS MIN FROM PreviousRental WHERE cost>2;
9
Top Let’s find the hourly employee that worked most recently (just the date). Return both first name and last name We’ll need to use COUNT, TOP 1, and do a join. SELECT TOP 1 e.firstName, e.lastname FROM employee e INNER JOIN timecard t ON e.ssn=t.ssn ORDER BY t.date DESC; firstNamelastname AyishaThreat
10
ssnlastNamefirstName 145-09-0967UnoJane 245-11-4554ToulouseJie 376-77-0099ThreatAyisha 479-98-0098FortuneJulian 579-98-8778FivozinskyBruce Employee ssndatestartTimeendTimestoreIdpaid 145-09-09671/14/20028:15:00 AM12:00:00 PM3Yes 145-09-09671/16/20028:15:00 AM12:00:00 PM3Yes 245-11-45541/14/20028:15:00 AM12:00:00 PM3Yes 376-77-00991/3/200210:00:00 AM2:00:00 PM5Yes 376-77-00991/3/20023:00:00 PM7:00:00 PM3Yes 376-77-00992/23/20022:00:00 PM10:00:00 PM5Yes 376-77-00993/21/20023:00:00 PM7:00:00 PM5Yes Timecard HourlyEmployee ssnhourlyRate 145-09-0967$6.05 245-11-4554$5.50 376-77-0099$10.75 479-98-0098$9.50 579-98-8778$5.50
11
TOP with ties What if we have a tie with TOP (or BOTTOM). Let’s find the hourly employee that worked first. SELECT TOP 1 e.firstName, e.lastname FROM employee e INNER JOIN timecard t ON e.ssn=t.ssn ORDER BY t.date ASC; firstNamelastname AyishaThreat AyishaThreat
12
DISTINCT Let’s find all hourly employees who have been paid. SELECT DISTINCT e.firstName, e.lastname FROM employee e INNER JOIN timecard t ON e.ssn=t.ssn; firstNamelastname AyishaThreat JaneUno JieToulouse
13
DISTINCT or DISTINCTROW SELECT e.firstName, e.lastname, t.storeID FROM employee e INNER JOIN timecard t ON e.ssn=t.ssn; firstNamelastnamestoreID JaneUno3 JaneUno3 JieToulouse3 AyishaThreat5 AyishaThreat5 AyishaThreat5 AyishaThreat3
14
SELECT DISTINCT e.firstName, e.lastname, t.storeID FROM employee e INNER JOIN timecard t ON e.ssn=t.ssn; firstNamelastnamestoreID AyishaThreat3 AyishaThreat5 JaneUno3 JieToulouse3 SELECT DISTINCTROW e.firstName, e.lastname, t.storeID FROM employee e INNER JOIN timecard t ON e.ssn=t.ssn; firstNamelastnamestoreID JaneUno3 JaneUno3 JieToulouse3 AyishaThreat5 AyishaThreat5 AyishaThreat5 AyishaThreat3
15
Row-level functions These functions operate on single rows at a time. Arithmetic functions + - * / ROUND and NZ (specific to Access) String functions: concatenation: + or & extractors: MID, LEFT, RIGHT, LEN, etc. Date functions (specific to Access) YEAR, MONTH, DATEDIFF, etc
16
DATEDIFF and arithmetic Let’s calculate the amount earned for each hourly employee’s timecard. Include in output firstname, lastname, date, $earned SELECT e.firstName & ' ' & lastName AS Employee, t.date, Round(DateDiff("n",t.startTime,t.endTime)/60*h.hourlyRate,2) AS [pay] FROM Employee AS e INNER JOIN (HourlyEmployee AS h INNER JOIN Timecard AS t ON h.ssn = t.ssn) ON e.ssn = t.ssn ORDER BY t.date;
17
Employeedatepay Ayisha Threat1/3/200243 Ayisha Threat1/3/200243 Jie Toulouse1/14/200220.62 Jane Uno1/14/200222.69 Jane Uno1/16/200222.69 Ayisha Threat2/23/200286 Ayisha Threat3/21/200243 SELECT e.firstName & ' ' & lastName AS Employee, t.date, Round(DateDiff("n",t.startTime,t.endTime)/60*h.hourlyRate,2) AS [pay] FROM Employee AS e INNER JOIN (HourlyEmployee AS h INNER JOIN Timecard AS t ON h.ssn = t.ssn) ON e.ssn = t.ssn ORDER BY t.date;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.