Download presentation
Presentation is loading. Please wait.
Published byAndrew Golden Modified over 8 years ago
1
CS 3630 Database Design and Implementation
2
Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype, price From Booking B Join Room R on R.Hotel_no = B.Hotel_no; -- Correct? 2
3
Joins -- For each hotel, display Hotel Number -- with name and number of bookings of -- the hotel Select H.Hotel_No, name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name; // Missing hotels without bookings // How to get all hotels? 3
4
Joins -- For each hotel, display Hotel Number with -- name and number of bookings of the hotel Select H.Hotel_No, name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name; Why Missing Hotels without bookings? For every h in Hotel For every b in Booking If h.Hotel_no = b.Hotel_no Then generating a record Group and counting 4
5
Out Joins -- For each hotel, display Hotel Number with -- name and number of bookings of the hotel How to keep hotels without bookings? For every h in Hotel joined = False For every b in Booking If h.Hotel_no = b.Hotel_no Then generating a record joined = True Keep the record if joined is False (adding nulls for booking fields) Group and counting 5
6
Left Outer Join -- For each hotel, display Hotel Number with -- name and number of bookings of the hotel Select H.Hotel_No, name, count(*) From Hotel H Left Join Booking B On H.Hotel_no = B.Hotel_no Group By H.Hotel_No, name; -- Hotels without a booking have 1 -- for count(*) 6
7
Left Outer Join -- For each hotel, display Hotel Number with -- name and number of bookings of the hotel Select H.Hotel_No, name, count(Guest_No) From Hotel H Left Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name; -- Hotels without a booking have 0 -- for count(Guest_No) 7
8
Left Outer Join -- For each hotel, display Hotel Number with -- name and number of bookings of the hotel Select H.Hotel_No, name, count(Guest_No) From Hotel H Left Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name; -- What if count(B.Hotel_No) -- YES! 8
9
Left Outer Join -- For each hotel, display Hotel Number with -- name and number of bookings of the hotel Select H.Hotel_No, name, count(Guest_No) From Hotel H Left Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name; -- What if count(H.Hotel_No) -- NO! 9
10
Left Outer Join -- For each hotel, display Hotel Number with -- name and number of bookings of the hotel, -- including hotels without any bookings. Select B.Hotel_no, name, count(B.Hotel_No) From Hotel H Left Join Booking B on H.Hotel_no = B.Hotel_no Group by B.Hotel_No, name; -- Group by B.Hotel_No instead of H.Hotel_No -- NO: Missing some hotels! -- Some records in the result have no B.Hotel_No! -- All records with the same name and a null value -- for B.Hotel_No are in one group! 10
11
Left Outer Join -- For each hotel, display Hotel Number with -- name and number of bookings of the hotel, -- including hotels without any bookings. Select H.Hotel_no, name, count(B.Hotel_No) From Hotel H Left Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name; -- Group by name only -- Group by H.Hotel_No only 11
12
Right Outer Join -- For each hotel with at least one booking, -- display Hotel number with name -- number of bookings of the hotel. Select H.Hotel_No, name, count(B.Hotel_No) From Hotel H Right join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name; -- NO: same as natural join -- since each booking must have a hotel_no 12
13
Joins -- For each booking, display Hotel name, -- hotel_no, room no and guest_no. Select name, H.Hotel_No, room_no, guest_no From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Order By H.Hotel_No, Room_No; -- No Outer Join! -- No Group By 13
14
Joins -- For each booking, display Hotel name, -- hotel_no, room no and guest_no. Select name, H.Hotel_No, room_no, guest_no From Booking B Join Hotel H on H.Hotel_no = B.Hotel_no Order By H.Hotel_No, Room_No; -- Better Join Booking with Hotel -- No Outer Join! -- No Group By 14
15
Outer Joins and Where Clause -- For each hotel, display Hotel number with name and -- number of bookings of the hotel during the -- current year, including hotels without any bookings -- during the current year. -- Assuming no booking is longer than one year. Select name, H.Hotel_No, count(B.Hotel_No) From Hotel H left join Booking B on H.Hotel_no = B.Hotel_no Where To_Char(Date_From, 'yyyy') = To_Char(sysDate, 'yyyy') Or To_Char(Date_To, 'yyyy') = To_Char(sysDate, 'yyyy') Group by H.Hotel_No, name; -- Incorrect! -- Where condition is applied after Left Join -- will miss hotels without any bookings 15
16
Outer Joins and Where Clause -- For each hotel, display Hotel number with name and -- number of bookings of the hotel during the -- current year, including hotels without any bookings -- during the current year. -- Assuming no booking is longer than one year. Select name, H.Hotel_No, count(B.Hotel_No) From Hotel H left join Booking B on H.Hotel_no = B.Hotel_no and (To_Char(Date_From, 'yyyy') = To_Char(sysDate, 'yyyy') Or To_Char(Date_To, 'yyyy') = To_Char(sysDate, 'yyyy')) Group by H.Hotel_No, name; -- YES: Checking the Current Year condition when Left Join 16
17
Outer Joins and Where Clause -- For each hotel, display Hotel number with name and -- number of bookings of the hotel during the -- current year, including hotels without any bookings -- during the current year. -- Assuming a booking could be longer than one year. Select name, H.Hotel_No, count(B.Hotel_No) From Hotel H left join Booking B on H.Hotel_no = B.Hotel_no and (To_Char(Date_From, 'yyyy') <= To_Char(sysDate, 'yyyy') and To_Char(Date_To, 'yyyy') >= To_Char(sysDate, 'yyyy')) Group by H.Hotel_No, name; -- YES: Checking the Current Year condition when Left Join 17
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.