cs 3630 database design and implementation. where clause and aggregate functions -- list all rooms...

25
CS 3630 Database Design and Implementation

Upload: bria-pelham

Post on 31-Mar-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

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

CS 3630 Database Design and Implementation

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Which Approach to Use?

• Outer Join

• Sub-Query

15

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

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

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

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

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

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

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

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;

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

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

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

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

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

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

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

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

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

Project

• PhaseTwo

*. Filepart

• Multi-Value attribute

• Many-to-many relationship

• Copy PK as FK

24

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

Project

• PhaseThree– Desire2Learn

– Due Monday, April 28

25