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 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 4

5 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 5

6 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); 6

7 Join -- List guests who have bookings during April 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; -- Distinct -- could use

8 Sub-Queries -- List guests who have bookings during April 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; -- Verify it! 8

9 Join -- List guests who dont have any bookings -- during April 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? 9

10 Join -- List guests who dont have any bookings -- during April 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

11 Sub-Queries -- List guests who dont have any bookings during -- April 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; -- How to verify? 11

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

13 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; 13

14 How many bookings each hotel has -- Outer Join Select name, 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, name; 14

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

16 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; -- No result for hotel without bookings 16

17 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; 17

18 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; 18

19 19 Select hotel_no "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; pause Select H.Hotel_no "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 H.Hotel_No;

20 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; 20

21 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; 21

22 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 have NOT been booked during April 2005" From Hotel H; 22

23 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; 23

24 Project PhaseTwo *. Filepart Multi-Value attribute Many-to-many relationship Copy PK as FK 24

25 Project PhaseThree –Desire2Learn –Due Monday, April 28 25


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