Practice Set 1
Practice Set 1
Practice set – 1
Consider the database of a sailing club with the following three tables:
A. Write SQL Definition for the above relational Schema with necessary constraints
Answer:
rating INTEGER,
age FLOAT
);
colour VARCHAR(30)
);
bid INTEGER,
day DATE,
);
Answer: SELECT S.sname FROM Sailor S JOIN Reserves R ON S.sid = R.sid WHERE R.bid = 103;
Answer: SELECT DISTINCT S.sname FROM Sailor S JOIN Reserves R ON S.sid = R.sid JOIN Boats B ON
R.bid = B.bid WHERE B.colour IN ('red', 'green');
3. Find the names of sailors who have reserved maximum number of boats
Answer: SELECT S.sname FROM Sailor S JOIN Reserves R ON S.sid = R.sid GROUP BY S.sid, S.sname
HAVING COUNT(*) = (SELECT MAX(boat_count) FROM (SELECT COUNT(*) AS boat_count FROM
Reserves GROUP BY sid) AS counts);
Answer: SELECT COUNT(DISTINCT R.bid) FROM Reserves R JOIN Sailor S ON R.sid = S.sid WHERE
S.sname = 'Dustin';
6. Find the names of sailors who have reserved less than 3 boats
Answer: SELECT S.sname FROM Sailor S JOIN Reserves R ON S.sid = R.sid GROUP BY S.sid, S.sname
HAVING COUNT(DISTINCT R.bid) < 3;
7. Find the rating and the age of the sailors who reserved a red boat.
Answer: SELECT DISTINCT S.rating, S.age FROM Sailor S JOIN Reserves R ON S.sid = R.sid JOIN Boats
B ON R.bid = B.bid WHERE B.colour = 'red';
8. Find the IDs of those sailors who reserved the boat ‘Clipper’ and who reserved a green boat.