Lab 8 Complex SQL Queries: Code
Lab 8 Complex SQL Queries: Code
use hotelBooking1;
-- Hotel Table
create Table hotel
(hotelno varchar(10) primary key,
hotelname varchar(20),
city varchar(20));
-- Room Table
create table room(
roomno numeric(5),
hotelno varchar(10),
type varchar(10),
price decimal(5,2),
primary key (roomno, hotelno),
foreign key (hotelno) references hotel(hotelno) on delete restrict on update cascade);
-- Guest Table
create table guest(
guestno numeric(5) primary key,
guestname varchar(20),
guestaddress varchar(50));
-- Booking Table
create table booking(
hotelno varchar(10),
guestno numeric(5),
datefrom datetime,
dateto datetime,
roomno numeric(5),
primary key (hotelno, guestno, datefrom),
foreign key (roomno, hotelno) references room(roomno, hotelno) on delete restrict on update cascade,
foreign key (guestno) references guest(guestno) on delete restrict on update cascade);
-- Query 1
select 'Query 1' as '';
select B.datefrom, sum(price) as total_revenue_per_night
from booking B natural join room R
where R.type = 'double'
group by B.datefrom;
-- Query 2
select 'Query 2' as '';
select count(distinct guestno) as no_of_Aug_guests
from booking B natural join guest G
where month(dateto) = 8 or month(datefrom) = 8;
-- Query 3
select 'Query 3' as '';
select R.type, R.price
from hotel H natural join room R
where H.hotelname = 'Avari';
-- Query 4
select 'Query 4' as '';
select G.guestno, G.guestname
from booking B natural join guest G natural join hotel H
where H.hotelname = 'Marriott' and (B.dateto >= date(sysdate()) or B.datefrom = date(sysdate()));
-- B.datefrom = sysdate() is written for those entries who did not confirm the last date of their
-- booking. This query is written with the assumption that no client can make any reservation for future
-- without staying at the hotel.
-- Query 5
select 'Query 5' as '';
select sum(R.price) as 'today\'s_earning_of_Hotel_Inn'
from booking B natural join hotel H natural join room R
where H.hotelname = 'Hotel Inn' and B.datefrom = date(sysdate());
-- Query 6
select 'Query 6' as '';
select R.roomno as 'unoccupied_rooms_in_Hotel_Inn'
from room R natural left join booking B natural join hotel H
where H.hotelname = 'Hotel Inn' and B.guestno is null;
-- Query 7
select 'Query 7' as '';
select sum(R.price) as 'lost_income_of_Hotel_Inn'
from room R natural left join booking B natural join hotel H
where H.hotelname = 'Hotel Inn' and B.guestno is null;
-- Query 8
select 'Query 8' as '';
select sum(price) as 'today\'s_lost_income_of_all_hotels'
from room R natural left join booking B
where
B.dateto <= date(sysdate()) -- bbokings with null dateto are considered to be still occupied
or (roomno, hotelno) IN
(select roomno, hotelno
from room R natural left join booking B natural join hotel H
where B.guestno is null);
-- Query 9
select 'Query 9' as '';
select sum(price) as 'lost_income_of_hotels_having_two_different_types_of_rooms'
from room R natural left join booking B
where B.guestno is null
and hotelno IN
(select hotelno
from room
group by hotelno
having count(distinct type) > 2);
-- Query 10
select 'Query 10' as '';
update room
set price = price * 0.02;
select *
from room;
Output:
Lab 9
Views and Triggers
Code:
select @@sql_mode;
set sql_mode = '';
use hotelbooking1;
-- Task 1
create view view1
as select R.roomno, R.type, R.price, G.guestname
from room R natural join booking B natural join hotel H natural join guest G
where H.hotelname = 'Marriott';
-- Task 2
create view view2
as select G.guestname, G.guestaddress
from guest G
where G.guestaddress like '%London'
order by G.guestname;
-- Task 4
insert into view2
values('Taylor Swift', 'Kemp House, 152 City Road, London');
select 'Yes, New Data has updated the original table as well.' as '';
-- Task 5
update view1
set price = price * 0.02;
select 'Yes, update operation is performed on the original database as well.' as '';
-- Task 6
create table employee(
ssn varchar(9) primary key,
fname varchar(10),
lname varchar(10),
bdate date,
address varchar(50),
gender char(1),
salary int(6));
update employee
set lname = 'Cena'
where ssn = '123456789';