Professional Documents
Culture Documents
-- Sailor Table
create table Boat_Reservation.Sailor
(sid int(3) primary key,
sname varchar(10),
rating int(2),
age decimal (4, 2));
-- Boat Table
create table Boat_Reservation.Boat
(bid int(3) primary key,
bname varchar(10),
color varchar(5));
-- Reserve Table
create table Boat_Reservation.Reserve
(sid int(3) not null,
bid int(3) not null,
reservationDay date,
primary key (sid, bid),
foreign key (sid) references Boat_Reservation.Sailor(sid) on delete restrict on update cascade,
foreign key (bid) references Boat_Reservation.Boat(bid) on delete restrict on update cascade);
-- Query 1
select 'Query 1' as '';
select sname, age
from Boat_Reservation.Sailor;
-- Query 2
select 'Query 2' as '';
select *
from Boat_Reservation.Sailor
where rating > 7;
-- Query 3
select 'Query 3' as '';
select S.sname
from Boat_Reservation.Sailor S, Boat_Reservation.Reserve R
where S.sid = R.sid and R.bid = 103;
-- Query 4
select 'Query 4' as '';
select distinct S.sid
from Boat_Reservation.Sailor S, Boat_Reservation.Reserve R, Boat_Reservation.Boat B
where S.sid = R.sid and B.bid = R.bid and B.color = 'Red';
-- Query 5
select 'Query 5' as '';
select distinct S.sname
from Boat_Reservation.Sailor S, Boat_Reservation.Reserve R, Boat_Reservation.Boat B
where S.sid = R.sid and B.bid = R.bid and B.color = 'Red';
-- Query 6
select 'Query 6' as '';
select distinct B.color
from Boat_Reservation.Sailor S, Boat_Reservation.Reserve R, Boat_Reservation.Boat B
where S.sid = R.sid and B.bid = R.bid and S.sname = 'Lubber';
-- Query 7
select 'Query 7' as '';
select distinct S.sname
from Boat_Reservation.Sailor S, Boat_Reservation.Reserve R
where S.sid = R.sid and R.bid is not null;
-- Query 8
select 'Query 8' as '';
select distinct S.age
from Boat_Reservation.Sailor S
where S.sname like 'B%B' and char_length(S.sname) >= 3;
-- Query 9
select 'Query 9' as '';
select distinct S.sname
from Boat_Reservation.Sailor S, Boat_Reservation.Reserve R, Boat_Reservation.Boat B
where (B.color = 'Red' OR B.color = 'Green') and S.sid = R.sid and B.bid = R.bid;
-- Query 10
select 'Query 10' as '';
select distinct S.sid
from Boat_Reservation.Sailor S, Boat_Reservation.Reserve R
where (S.rating = 10 or R.bid = 104) and S.sid = R.sid;
Output
Lab 7
Introduction to Aggregation, Group By, and Joins
Conceptual Schema Diagram
Exercise
Code
-- Query 1
select 'Query 1' as '';
select distinct G.guestname, G.guestaddress
from hotelbooking.hotel H, hotelbooking.guest G, hotelbooking.booking B
where H.city = 'London' and H.hotelno = B.hotelno and B.guestno = G.guestno
order by G.guestname asc;
-- Query 2
select 'Query 2' as '';
select H.hotelname, count(R.roomno) as no_of_rooms
from hotelbooking.hotel H, hotelbooking.room R
where H.hotelno = R.hotelno
group by H.hotelname;
-- Query 3
select 'Query 3' as '';
select H.hotelname, avg(R.price) as average_price
from hotelbooking.hotel H, hotelbooking.room R
where H.hotelno = R.hotelno and H.city = 'London'
group by H.hotelname;
-- Query 4
select 'Query 4' as '';
select R.type, R.roomno as most_expensive_room
from hotelbooking.room R
where R.price in
(select max(R.price)
from hotelbooking.room R
group by R.type);
-- Query 5
select 'Query 5' as '';
select H.hotelname, H.city, count(distinct R.type) as avaliable_room_types
from hotelbooking.room R, hotelbooking.hotel H
where H.hotelno = R.hotelno
group by H.hotelname;
-- Query 6
select 'Query 6' as '';
select distinct H.hotelname, H.city
from hotelbooking.guest G, hotelbooking.hotel H, hotelbooking.booking B
where H.hotelno = B.hotelno and G.guestno = B.guestno and G.guestaddress like '%London';
-- Query 7
select 'Query 7' as '';
select H.hotelname, H.city, count(guestno) as reservations
from hotelbooking.hotel H, hotelbooking.booking B
where H.hotelno = B.hotelno
group by H.hotelname
order by reservations desc;
-- Query 8
select 'Query 8' as '';
select distinct G.guestname
from hotelbooking.guest G, hotelbooking.booking B
where G.guestno = B.guestno and B.dateto is null;
-- Query 9
select 'Query 9' as '';
select distinct H.hotelname, R.roomno, B.guestno
from hotelbooking.hotel H, hotelbooking.room R, hotelbooking.booking B
where (B.datefrom like '2003%' or B.datefrom like '2004%') and H.hotelno = B.hotelno and R.roomno =
B.roomno;
-- Query 10
select 'Query 10' as '';
select H.hotelname, H.city
from hotelbooking.hotel H, hotelbooking.booking B
where H.hotelno = B.hotelno and B.guestno is null;
-- Query 11
select 'Query 11' as '';
select count(distinct B.guestno) as no_of_bookings
from hotelbooking.booking B
where B.datefrom < '2015-05-31';
Output