0% found this document useful (0 votes)
209 views3 pages

Dbms Boat

The document describes a database schema with tables for sailors, boats, and reservations. It includes sample data and SQL queries that: 1) Find sailors who have made at least one boat reservation. 2) Find names of sailors who reserved a red or green boat in March. 3) Find names of sailors who have reserved both a red and green boat.

Uploaded by

Medha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
209 views3 pages

Dbms Boat

The document describes a database schema with tables for sailors, boats, and reservations. It includes sample data and SQL queries that: 1) Find sailors who have made at least one boat reservation. 2) Find names of sailors who reserved a red or green boat in March. 3) Find names of sailors who have reserved both a red and green boat.

Uploaded by

Medha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

QUESTION 1

Consider the following relational schema:


SAILORS (sid, sname, rating, date_of_birth)
BOATS (bid, bname, color)
RESERVES (sid, bid, date, time slot)

CREATE TABLE SAILORS (


sid INT PRIMARY KEY NOT NULL,
sname VARCHAR(255) NOT NULL,
rating INT,
date_of_birth DATE,
);

CREATE TABLE BOATS (


bid INT PRIMARY KEY NOT NULL,
bname VARCHAR(255) NOT NULL,
color VARCHAR(255),
PRIMARY KEY (bid)
);

CREATE TABLE RESERVES (


sid INT NOT NULL,
bid INT NOT NULL,
date DATE,
time_slot TIME,
PRIMARY KEY (sid, bid, date, time_slot),
FOREIGN KEY (sid) REFERENCES SAILORS(sid),
FOREIGN KEY (bid) REFERENCES BOATS(bid)
);

INSERT INTO boats (bid, bname, color) VALUES


(201, 'Mercedes', 'Blue'),
(202, 'Ferrari', 'Red'),
(203, 'Red Bull Racing', 'Yellow'),
(204, 'McLaren', 'Orange'),
(205, 'Alpine', 'Black');

INSERT INTO reserves (sid, bid, rdate, time_slot)


VALUES
(101, 201, '2023-03-04', '09:00:00'),
(102, 202, '2023-03-05', '10:30:00'),
(104, 203, '2023-03-06', '12:00:00'),
(105, 204, '2023-03-07', '13:30:00');

INSERT INTO sailors (sid, sname, rating, dob)


VALUES
(101, 'Lewis Hamilton', 10, '2003-03-20'),
(102, 'Max Verstappen', 9, '2005-07-26'),
(104, 'Charles Leclerc', 7, '2001-02-16'),
(105, 'Valtteri Bottas', 6, '2002-07-07'),
(106, 'Lando Norris', 8, '2002-10-04'),
(107, 'Sergio Perez', 5, '2002-04-04');

a) Find sailors who’ve reserved at least one boat

SELECT DISTINCT sid, sname


FROM SAILORS
WHERE sid IN (
SELECT sid
FROM RESERVES
);

b) Find names of sailors who’ve reserved a red or a green boat in the month of March.

SELECT DISTINCT S.sname


FROM SAILORS S, RESERVES R, BOATS B
WHERE S.sid = R.sid AND R.bid = B.bid
AND B.color IN ('Red', 'Green')
AND MONTH(R.rdate) = 3;

c) Find names of sailors who’ve reserved a red and a green boat

SELECT S.sname
FROM SAILORS S, RESERVES R1, RESERVES R2, BOATS B1, BOATS B2
WHERE S.sid = R1.sid AND S.sid = R2.sid
AND R1.bid = B1.bid AND B1.color = 'Red'
AND R2.bid = B2.bid AND B2.color = 'Green';

d) Find sid of sailors who have not reserved a boat after Jan 2018.

SELECT S.sid
FROM SAILORS S
WHERE NOT EXISTS (
SELECT *
FROM RESERVES R
WHERE S.sid = R.sid AND R.rdate > '2018-01-31'
);
e) Find sailors whose rating is greater than that of all the sailors named “John”

SELECT S1.sname
FROM SAILORS S1
WHERE S1.rating > ALL (
SELECT S2.rating
FROM SAILORS S2
WHERE S2.sname = 'John'
);

f) Find sailors who’ve reserved all boats

SELECT R.sid, S.sname


FROM SAILORS S, RESERVES R
WHERE S.sid = R.sid
GROUP BY R.sid, S.sname
HAVING COUNT(DISTINCT R.bid) = (
SELECT COUNT(*)
FROM BOATS
);

g) Find name and age of the oldest sailor(s)

SELECT sname, YEAR(CURDATE()) - YEAR(dob) AS age


FROM SAILORS
WHERE dob = (
SELECT MAX(dob)
FROM SAILORS
);

h) Find the age of the youngest sailor for each rating with at least 2 such sailors

SELECT rating, MIN(YEAR(CURDATE()) - YEAR(dob)) AS min_age


FROM SAILORS
WHERE rating IN (
SELECT rating
FROM SAILORS
GROUP BY rating
HAVING COUNT(*) >= 2
)
GROUP BY rating;

You might also like