INF 280 Database Systems SQL:Join D. Christozov INF 280 Database Systems SQL:Join
The two NULL values do not match! SQL: join Used for the following purposes: to combine data from two or more tables to select data from one table based on a search condition involving columns from from an other table; to reintegrate data, separated during normalization to combine information about separate entities to explore relationships between tables. Matching the related values in columns of the two tables is based on the foreign key mechanism or the rule of having columns from the two tables with the same names. The two NULL values do not match! D. Christozov INF 280 Database Systems SQL:Join
INF 280 Database Systems SQL:Join Syntax: FROM A [type] [JOIN] B [condition] Equijoins: the join condition is based on comparison for equality and the join columns are included in the final result Natural join – only one of the corresponding join columns is included in the result. FROM A [type] B ON A.b = B.d (the corresponding columns have different names) FROM A [type] B USING b (the corresponding columns have equal names b) D. Christozov INF 280 Database Systems SQL:Join
Using foreign key constrains SQL: join Using foreign key constrains FROM A [type] B (the corresponding columns are defined as foreign key reference) Theta joins any join in which the condition is given in the form: qualifier_1.column_1 relational_operator qualifier_2.column_2 where relational_operator can be any of the following: =, <>, <, >, <=, >= Equijoin is a special case of theta join D. Christozov INF 280 Database Systems SQL:Join
SQL: join – categories of natural/equijoins Built in specified in FROM clause CROSS a Cartesian product: all combinations of rows INNER the default join type: only matched rows are retained LEFT(OUTER) all rows from table A, plus the matched values from table B RIGHT(OUTER) all rows from table B, plus the matched values from table A FULL(OUTER) all rows from both tables, merged where matches were found,or filled with NULLs in the columns from other table UNION opposite of the INNER: retains only no matched rows Defined specified as filter-condition in WHERE clause D. Christozov INF 280 Database Systems SQL:Join
INF 280 Database Systems SQL:Join SQL: cross join A A cross join B a b 1334 CCC 1399 GGG 1208 MMM A.a A.b B.b B.c 1334 CCC 8554 1399 GGG 1208 MMM 5223 9111 WWW 7894 B b c CCC 8554 GGG 5223 9111 WWW 7894 D. Christozov INF 280 Database Systems SQL:Join
INF 280 Database Systems SQL:Join SQL: inner join A a b 1334 CCC 1399 GGG 1208 MMM A inner join B (A.b = B.b) A.a A.b B.c 1334 CCC 8554 1399 GGG 5223 9111 B b c CCC 8554 GGG 5223 9111 WWW 7894 D. Christozov INF 280 Database Systems SQL:Join
INF 280 Database Systems SQL:Join SQL: left outer join A a b 1334 CCC 1399 GGG 1208 MMM A left outer join B (A.b = B.b) A.a B.b B.c 1334 CCC 8554 1399 GGG 5223 9111 1208 MMM NULL B b c CCC 8554 GGG 5223 9111 WWW 7894 D. Christozov INF 280 Database Systems SQL:Join
INF 280 Database Systems SQL:Join SQL: right outer join A a b 1334 CCC 1399 GGG 1208 MMM A right outer join B (A.b = B.b) A.a A.b B.c 1334 CCC 8554 1399 GGG 5223 9111 NULL WWW 7894 B b c CCC 8554 GGG 5223 9111 WWW 7894 D. Christozov INF 280 Database Systems SQL:Join
INF 280 Database Systems SQL:Join SQL: full outer join A a b 1334 CCC 1399 GGG 1208 MMM A full outer join B (A.b = B.b) A.a A.b B.c 1334 CCC 8554 1399 GGG 5223 9111 NULL WWW 7894 1208 MMM B b c CCC 8554 GGG 5223 9111 WWW 7894 D. Christozov INF 280 Database Systems SQL:Join
INF 280 Database Systems SQL:Join SQL: union join A a b 1334 CCC 1399 GGG 1208 MMM A union join B (A.b = B.b) A.a A.b B.c 1208 MMM NULL WWW 7894 B b c CCC 8554 GGG 5223 9111 WWW 7894 D. Christozov INF 280 Database Systems SQL:Join
INF 280 Database Systems SQL:Join Exercises D. Christozov INF 280 Database Systems SQL:Join
INF 280 Database Systems SQL:Join Booking Guests HotelNo RoomNo GuestNo DateFrom DateTo H01 R01 G01 20050305 20050310 R02 G02 20050328 G03 20050311 20050426 H02 G04 G05 20050601 20050830 R03 G06 20050228 20050502 H03 G07 20050410 20050430 H04 G08 20050412 20050508 GuestNo Name Address G01 Peter USA G02 John Scapto2 G03 Alice UK G04 Barry BG G05 Vern AUBG G06 Simeon Skapto1 G07 Mark Kchuka G08 Julia Sofia Hotel HotelNo Name Address H01 Lessvenor San Francisco H02 Grosvenor New York H03 Alen Mak Blagoevgrad H04 Sheraton Sofia Rooms RoomNo HotelNo Type Price R01 H01 Single 67.00 R02 H02 Double 76.00 R03 H03 Apartment 102.00 H04 72.00 75.00 92.00 88.00 134.00 SELECT Room_No, Type, Price FROM Room INNER JOIN Booking USING (HotelNo, Room_No) UNION JOIN Guest USING GuestNo; D. Christozov INF 280 Database Systems SQL:Join
INF 280 Database Systems SQL:Join Booking Guests HotelNo RoomNo GuestNo DateFrom DateTo H01 R01 G01 20050305 20050310 R02 G02 20050328 G03 20050311 20050426 H02 G04 G05 20050601 20050830 R03 G06 20050228 20050502 H03 G07 20050410 20050430 H04 G08 20050412 20050508 GuestNo Name Address G01 Peter USA G02 John Scapto2 G03 Alice UK G04 Barry BG G05 Vern AUBG G06 Simeon Skapto1 G07 Mark Kchuka G08 Julia Sofia Hotel HotelNo Name Address H01 Lessvenor San Francisco H02 Grosvenor New York H03 Alen Mak Blagoevgrad H04 Sheraton Sofia Rooms RoomNo HotelNo Type Price R01 H01 Single 67.00 R02 H02 Double 76.00 R03 H03 Apartment 102.00 H04 72.00 75.00 92.00 88.00 134.00 SELECT RoomNo, Type, GuestNo FROM Room Left Outer Join Booking Using (RoomNo, HotelNo) WHERE Date_from <= 03/24/2003 AND Date_to >= 03/24/2003; D. Christozov INF 280 Database Systems SQL:Join
INF 280 Database Systems SQL:Join Booking Guests HotelNo RoomNo GuestNo DateFrom DateTo H01 R01 G01 20050305 20050310 R02 G02 20050328 G03 20050311 20050426 H02 G04 G05 20050601 20050830 R03 G06 20050228 20050502 H03 G07 20050410 20050430 H04 G08 20050412 20050508 GuestNo Name Address G01 Peter USA G02 John Scapto2 G03 Alice UK G04 Barry BG G05 Vern AUBG G06 Simeon Skapto1 G07 Mark Kchuka G08 Julia Sofia Hotel Rooms HotelNo Name Address H01 Lessvenor San Francisco H02 Grosvenor New York H03 Alen Mak Blagoevgrad H04 Sheraton Sofia RoomNo HotelNo Type Price R01 H01 Single 67.00 R02 H02 Double 76.00 R03 H03 Apartment 102.00 H04 72.00 75.00 92.00 88.00 134.00 SELECT GuestNo, Guest.Name, Hotel.Name FROM Guest, Booking, Hotel WHERE DateFrom <= ’03/25/2003’ and DateTo >= ‘03/25/2003’; D. Christozov INF 280 Database Systems SQL:Join
INF 280 Database Systems SQL:Join Booking Guests HotelNo RoomNo GuestNo DateFrom DateTo H01 R01 G01 20050305 20050310 R02 G02 20050328 G03 20050311 20050426 H02 G04 G05 20050601 20050830 R03 G06 20050228 20050502 H03 G07 20050410 20050430 H04 G08 20050412 20050508 GuestNo Name Address G01 Peter USA G02 John Scapto2 G03 Alice UK G04 Barry BG G05 Vern AUBG G06 Simeon Skapto1 G07 Mark Kchuka G08 Julia Sofia Hotel Rooms HotelNo Name Address H01 Lessvenor San Francisco H02 Grosvenor New York H03 Alen Mak Blagoevgrad H04 Sheraton Sofia RoomNo HotelNo Type Price R01 H01 Single 67.00 R02 H02 Double 76.00 R03 H03 Apartment 102.00 H04 72.00 75.00 92.00 88.00 134.00 SELECT HotelNo, Name FROM Hotel Left Outer Join Booking Using HotelNo; D. Christozov INF 280 Database Systems SQL:Join
INF 280 Database Systems SQL:Join Booking Guests HotelNo RoomNo GuestNo DateFrom DateTo H01 R01 G01 20050305 20050310 R02 G02 20050328 G03 20050311 20050426 H02 G04 G05 20050601 20050830 R03 G06 20050228 20050502 H03 G07 20050410 20050430 H04 G08 20050412 20050508 GuestNo Name Address G01 Peter USA G02 John Scapto2 G03 Alice UK G04 Barry BG G05 Vern AUBG G06 Simeon Skapto1 G07 Mark Kchuka G08 Julia Sofia Hotel Rooms HotelNo Name Address H01 Lessvenor San Francisco H02 Grosvenor New York H03 Alen Mak Blagoevgrad H04 Sheraton Sofia RoomNo HotelNo Type Price R01 H01 Single 67.00 R02 H02 Double 76.00 R03 H03 Apartment 102.00 H04 72.00 75.00 92.00 88.00 134.00 What is the mistake? SELECT Type, SUM(Price) FROM Room, Booking, Guests WHERE DateFrom <= ’03/25/2003’ and DateTo >= ‘03/25/2003’; D. Christozov INF 280 Database Systems SQL:Join
INF 280 Database Systems SQL:Join Write query Consider ‘Hotel’s Chain Data Base Booking: HotelNo; RoomNo; GuestNo; DateFrom;DateTo Guests: GuestNo; Name; Address Hotel: HotelNo; Name; Address Rooms: RoomNo; HotelNo; Type; Price Write a query to list all guests (name) currently (March 24, 2003) stay in Grosvenor Hotel. (Tip: Grosvenor is the hotel name) Write a query to calculate the today’s (March 24, 2003) income (sum of prices of occupied rooms) of all bookings for Grosvenor Hotel. Write a query to list today’s (March 24, 2003) unoccupied rooms (Room_No, Type, Price) in Grosvenor Hotel. Write a query to count American (Tip: address like ‘%USA%’) guests booked single rooms for May for Grosvenor Hotel. 1. SELECT Guests.* FROM ((Guests Inner Join Bookings Using GuestNo) Inner Join Hotel Using HotelNo) WHERE (DateFrom <= March 24 and DateTo >= March 24) and Hotel.Name = ‘Grosvenor’; Note: Hotel.Name – must qualified 2. SELECT ‘Income for March 23’, SUM(Price) AS ‘Money’ FROM ((Booking Inner Join Rooms ON (Booking.RoomNo=Rooms.RoomNo AND Booking.HotelNo=Rooms.HotelNo) ) Inner Join Hotel ON (Booking.HotelNo = Hotel.HotelNo)) Note: In the second join cannot use ‘USING’ 3. SELECT Rooms.* FROM ((Booking Right Outer Join Rooms ON (Booking.RoomNo=Rooms.RoomNo AND Booking.HotelNo=Rooms.HotelNo) ) Note: all rooms, including those which were never booked Inner Join Hotel ON (Booking.HotelNo = Hotel.HotelNo)) WHERE ((DateFrom >= March 24 OR DateTo <= March 24) OR DateFrom IS Null) and Hotel.Name = ‘Grosvenor’; 4. SELECT ‘Number of USA guests on March 24’, Count(*) FROM FROM ((Guests Inner Join Bookings Using GuestNo) Inner Join Hotel Using HotelNo) WHERE (((DateFrom <= May 31 and DateTo >= May 1) and Hotel.Name = ‘Grosvenor’) and Guest.Address Like ‘USA’; Note: comparing intervals – repeating from ‘group by’ D. Christozov INF 280 Database Systems SQL:Join