Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price.

Similar presentations


Presentation on theme: "CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price."— Presentation transcript:

1 CS 3630 Database Design and Implementation

2 Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price. Select * From Room Where Price > Avg(Price); ORA-00934: group function is not allowed here Where clause condition is applied to each record of the table 2

3 Sub-Queries -- List all rooms whose price is greater than -- the average room price. Select * From Room Where Price > (Select Avg(Price) From Room); -- Could use any other operator --, >= -- When only one value is returned. -- Like a C++ function! 3

4 Verifying the Query -- List all rooms whose price is greater than -- the average room price. Select Avg(Price) From Room; Select * From Room Order By Price; 4

5 Sub-Queries -- List all rooms whose price is the lowest. Select * From Room Where Price <= All (Select Price From Room); -- Returns a set of values Select * From Room Where Price = (Select Min(Price) From Room); -- Returns a single value -- Could use IN 5

6 Where Clause of Sub-Queries -- List all rooms whose price is not the lowest. Select * From Room Where Price > Any (Select Price From Room); Select * From Room Where Price > (Select Min(Price) From Room); Select * From Room R1 Where Exists (Select * From Room R2 Where R1.price > R2.price); -- Where condition on R1 and R2 6

7 Sorting the Result -- List all rooms whose price is not the lowest. Select * From Room Where Price > Any (Select Price From Room) Order By Hotel_No, Room_No; Select * From Room Where Price > (Select Min(Price) From Room) Order By Hotel_No, Room_No; Select * From Room R1 Where Exists (Select * From Room R2 Where R1.price > R2.price) Order By Hotel_No, Room_No; 7

8 Where Clause of Sub-Queries -- List all rooms whose price is greater than the -- average room price of their hotels. Select * From Room R1 Where Price > (Select Avg(Price) From Room R2 Where R1.Hotel_no = R2.Hotel_no); 8

9 Join -- List guests who have bookings during April 2005. Select Unique G.* From guest G Join booking B on G.guest_no = B.guest_no and date_from <= '30-Apr-05' and date_to >= '01-Apr-05' order by G.guest_no; 9

10 Could Use 4 Digits for Year -- List guests who have bookings during April 2005. -- could use Distinct and 2005 Select Distinct G.* From guest G Join booking B on G.guest_no = B.guest_no and date_from <= '30-Apr-2005' and date_to >= '01-Apr-2005' order by G.guest_no; 10

11 Where Clause Executed After Join -- List guests who have bookings during April 2005. -- could use Distinct and 2005 Select Distinct G.* From guest G Join booking B on G.guest_no = B.guest_no Where date_from <= '30-Apr-2005' and date_to >= '01-Apr-2005' order by G.guest_no; 11

12 Applying Conditions Before Join -- List guests who have bookings during April 2005. -- could use Distinct and 2005 Select Distinct G.* From guest G Join (Select * From booking Where date_from <= '30-Apr-2005' and date_to >= '01-Apr-2005') B on G.guest_no = B.guest_no order by G.guest_no; 12

13 Sub-Queries -- List guests who have bookings during April 2005. Select * From guest Where guest_no IN (Select Distinct guest_no From booking Where date_from <= '30-Apr-05' and date_to >= '01-Apr-05') Order by Guest_no; 13

14 Verifying the Query Select * from booking order by guest_no, date_from; 14

15 Join -- List guests who don’t have any bookings -- during April 2005. Select Distinct G.* From guest G Join booking B on G.guest_no = B.guest_no and (date_from > '30-Apr-05' Or date_to < '01-Apr-05'); -- Correct? 15

16 Join -- List guests who don’t have any bookings -- during April 2005. Select Distinct G.* From guest G Join booking B on G.guest_no = B.guest_no and (date_from > '30-Apr-05' Or date_to < '01-Apr-05'); -- Incorrect! -- Guests who have bookings before or after -- April 2005. 16

17 Sub-Queries -- List guests who don’t have any bookings during -- April 2005. Select * From guest Where guest_no Not IN (Select distinct guest_no From booking Where date_from <= '30-Apr-05' and date_to >= '01-Apr-05') Order by Guest_no; 17

18 Verifying the Query -- List guests who don’t have any bookings during -- April 2005. Select * From guest Order By Guest_No; Select distinct guest_no From booking Where date_from <= '30-Apr-05' and date_to >= '01-Apr-05' Order by Guest_no; 18

19 How many bookings each hotel has? -- Simple Query Select hotel_no, Count(*) From Booking Group By hotel_no; -- No result for hotels without booking 19

20 How many bookings each hotel has -- Sub-Query -- Including hotels without booking Select Hotel_no, (select Count(*) from booking B where h.hotel_no = b.hotel_no) "Count" from Hotel H; 20

21 How many bookings each hotel has -- Outer Join Select H.Hotel_No, count(B.Hotel_No) From Hotel H Left join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No; 21

22 Which Approach to Use? Outer Join Sub-Query 22

23 How many rooms of each hotel have been booked at least once Select Hotel_no, count(distinct room_no) From Booking Group By hotel_no Order By Hotel_No; -- No result for hotel without bookings 23

24 How many rooms of each hotel have been booked at least once Select hotel_no, (Select Count(unique b.room_no) From booking B Where h.hotel_no = b.hotel_no) "No. Rooms have been booked" From Hotel H Order By Hotel_No; 24

25 How many rooms of each hotel have been booked at least once -- Outer Join Select H.Hotel_no, count(unique B.room_no) "No. Rooms have been booked" From Hotel H Left Join Booking B on H.Hotel_No = B.hotel_no Group By H.Hotel_No Order By Hotel_No; 25

26 How many rooms of each hotel have NOT been booked -- Outer Join -- Missing some hotels Select R.Hotel_no, count(*) From Room R Left Join Booking B on R.Hotel_No = B.hotel_no and R.Room_No = B.Room_No Where B.Room_no is Null Group By R.Hotel_No Order By R.Hotel_No; 26

27 How many rooms of each hotel have NOT been booked at least once Select hotel_no, (Select count (*) From Room R Where R.hotel_no = H.Hotel_No) - (select Count(unique b.room_no) from booking B where h.hotel_no = b.hotel_no) "No. Rooms have NOT been booked" From Hotel H; 27

28 How many rooms of each hotel do NOT have any booking during April 2005 Select Hotel_no, (Select count (*) From Room R Where R.hotel_no = H.Hotel_No) - (select Count(unique b.room_no) from booking B where h.hotel_no = b.hotel_no and date_from <= '30-Apr-05' and date_to >= '01-Apr-05') "No. Rooms NOT booked Apr 2005" From Hotel H; 28

29 Select Hotel_no, (Select count (*) From Room R Where R.hotel_no = H.Hotel_No) "Total No. of Rooms", (select Count(unique b.room_no) from booking B where h.hotel_no = b.hotel_no and date_from <= '30-Apr-05' and date_to >= '01-Apr-05') "No. Rooms with bookings", (Select count (*) From Room R Where R.hotel_no = H.Hotel_No) - (select Count(unique b.room_no) from booking B where h.hotel_no = b.hotel_no and date_from <= '30-Apr-05' and date_to >= '01-Apr-05') "No. Rooms without bookings" From Hotel H; 29


Download ppt "CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price."

Similar presentations


Ads by Google