cs 3630 database design and implementation. where clause and aggregate functions -- list all rooms...
TRANSCRIPT
CS 3630 Database Design and Implementation
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
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
Sub-Queries-- List all rooms whose price is the lowest.
Select * From RoomWhere Price <= All (Select Price From Room);-- Returns a set of values
Select * From RoomWhere Price = (Select Min(Price) From Room);-- Returns a single value-- Could use IN
4
Where Clause of Sub-Queries-- List all rooms whose price is not the lowest. Select * From RoomWhere Price > Any (Select Price From Room);
Select * From RoomWhere Price > (Select Min(Price) From Room);
Select * From Room R1Where Exists (Select * From Room R2 Where R1.price > R2.price);-- Where condition on R1 and R2
5
Where Clause of Sub-Queries
-- List all rooms whose price is greater than the -- average room price of their hotels.
Select *From Room R1Where Price > (Select Avg(Price) From Room R2 Where R1.Hotel_no = R2.Hotel_no);
6
Join-- List guests who have bookings during April 2005.
Select Unique G.*From guest GJoin 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 2005
7
Sub-Queries
-- List guests who have bookings during April 2005.
Select *From guestWhere 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
Join
-- List guests who don’t have any bookings -- during April 2005.
Select Distinct G.*From guest GJoin booking B on G.guest_no = B.guest_no and (date_from > '30-Apr-05' Or date_to < '01-Apr-05');
-- Correct?
9
Join
-- List guests who don’t have any bookings -- during April 2005.
Select Distinct G.*From guest GJoin 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.
10
Sub-Queries
-- List guests who don’t have any bookings during-- April 2005.
Select *From guestWhere 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
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
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
How many bookings each hotel has
-- Outer Join
Select name, H.Hotel_No, count(B.Hotel_No)From Hotel HLeft join Booking B on H.Hotel_no = B.Hotel_noGroup by H.Hotel_No, name;
14
Which Approach to Use?
• Outer Join
• Sub-Query
15
How many rooms of each hotel have been booked at least once
Select Hotel_no, count(distinct room_no)From BookingGroup By hotel_no;-- No result for hotel without bookings
16
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
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 HLeft Join Booking B on H.Hotel_No = B.hotel_noGroup By H.Hotel_No;
18
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 HOrder by Hotel_No;
pause
Select H.Hotel_no "Hotel No", count(unique B.room_no) "No. Rooms have been booked"From Hotel HLeft Join Booking B on H.Hotel_No = B.hotel_noGroup By H.Hotel_NoOrder By H.Hotel_No;
How many rooms of each hotel have NOT been booked
-- Outer Join-- Missing some hotels
Select R.Hotel_no, count(*)From Room RLeft Join Booking B on R.Hotel_No = B.hotel_no and R.Room_No = B.Room_NoWhere B.Room_no is NullGroup By R.Hotel_No;
20
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
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
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
Project
• PhaseTwo
*. Filepart
• Multi-Value attribute
• Many-to-many relationship
• Copy PK as FK
24
Project
• PhaseThree– Desire2Learn
– Due Monday, April 28
25