Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,

Similar presentations


Presentation on theme: "CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,"— Presentation transcript:

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 name and -- number of bookings of the hotel Select 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 name and -- number of bookings of the hotel Select 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 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 name and -- number of bookings of the hotel Select 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 name and -- number of bookings of the hotel Select 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 name and -- number of bookings of the hotel Select 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; -- What if count(B.Hotel_No) -- YES! 8

9 Left Outer Join -- For each hotel, display Hotel name and -- number of bookings of the hotel Select name, count(H.Hotel_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 name and -- number of bookings of the hotel, -- including hotels without any bookings. Select 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 name and -- number of bookings of the hotel, -- including hotels without any bookings. Select 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 name and -- number of bookings of the hotel. Select name, H.Hotel_No, 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! 13

14 Outer Joins and Where Clause -- For each hotel, display Hotel 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 14

15 Outer Joins and Where Clause -- For each hotel, display Hotel name and number -- of bookings of the hotel for the current year, -- including hotels without any bookings for the -- current 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 15

16 Outer Joins and Where Clause -- For each hotel, display Hotel name and number -- of bookings of the hotel for the current year, -- including hotels without any bookings for 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 16

17 Assignment8 17

18 Quiz 3 Wednesday, April 15 No Join Assignment 7 & 8 Must come to Lab 206 Use D2L 18

19 Quiz3 Remember Your Password! Try it Today! 19


Download ppt "CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,"

Similar presentations


Ads by Google