You are on page 1of 12

Lab 6

Writing Basic SQL Queries


Queries in Relational Algebra Notation
1. π sname , age (sailor )
2. σ rating>7 (sailor )
3. π sname (σ bid=103 ( Sailor∗Reserve))
4. π sid (σ ¿' ¿ ' ¿( ( Sailor∗Reserve )∗Boat ))
5. π sname (σ ¿' ¿ ' ¿( ( Sailor∗Reserve )∗Boat ))
6. π ¿(σ ( ( Sailor∗Reserve )∗Boat ))¿
sname=' Lubber '

7. π sname (σ bid≠NULL ( Sailor∗Reserve))


8. π age (σ sname LIKE ' B %B ' ∧CHAR (sname)≥3 ( Sailor))
LENGTH

9. π sname (σ ¿' ¿ ' ∨' ¿ ' ¿ ' ¿( ( Sailor∗Reserve )∗Boat ))


10. π sid (σ rating=10∨bid=104 (Sailor∗Reserve))

Queries in SQL Notation


-- Lab 6

-- Drop Previous Schema


drop schema Boat_Reservation;

-- Create New Schema


create schema Boat_Reservation;

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

-- Populating Sailor Table


insert into Boat_Reservation.Sailor
values
(22, 'Dustin', 7, 45.0),
(29, 'Brutus', 1, 33.0),
(31, 'Lubber', 8, 55.5),
(32, 'Andy', 8, 25.5),
(58, 'Rusty', 10, 35.0),
(64, 'Horatio', 7, 35.0),
(71, 'Zorba', 10, 16.0),
(74, 'Horatio', 9, 35.0),
(85, 'Art', 3, 25.5),
(95, 'Bob', 3, 63.5);

-- Populating Boat Table


insert into Boat_Reservation.Boat
values
(101, 'Interlake', 'Blue'),
(102, 'Interlake', 'Red'),
(103, 'Clipper', 'Green'),
(104, 'Marine', 'Red');

-- Populating Reserve Table


insert into Boat_Reservation.Reserve
values
(22, 101, date'1998-10-10'),
(22, 102, date'1998-10-10'),
(22, 103, date'1998-08-10'),
(22, 104, date'1998-07-10'),
(31, 102, date'1998-10-11'),
(31, 103, date'1998-06-11'),
(31, 104, date'1998-12-11'),
(64, 101, date'1998-05-09'),
(64, 102, date'1998-08-09'),
(74, 103, date'1998-08-09');

-- Printing the Three Tables


select 'Sailor Table' as '';
select * from Boat_Reservation.Sailor;
select 'Boat Table' as '';
select * from Boat_Reservation.Boat;
select 'Reserve Table' as '';
select * from Boat_Reservation.Reserve;

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

You might also like