SQL 1 Solutions
SQL 1 Solutions
RESERVATION.Sid and RESERVATION.Bid are foreign keys referring to SAILOR.Sid and BOAT.Bid,
respectively.
sample data
SELECT Sname
FROM SAILOR
WHERE Age > 20
SELECT Sname,Rating
FROM SAILOR
SELECT Bname
FROM BOAT
SELECT *
FROM SAILOR
WHERE Age > 20
SELECT DISTINCT Sname
FROM SAILOR NATURAL JOIN RESERVATION
WHERE Bid=103
There is an important subtlety here – sailor names are not unique, so what should the
results contain if boat 103 has been reserved by two different sailors with the same name?
Without DISTINCT, even the same sailor may appear in the results multiple times if they
have reserved boat 103 multiple times – probably not what the problem intended. However,
with DISTINCT, a name would only be listed once even if there are multiple sailors with that
name – which does satisfy the problem (all of the names belonging to sailors who have
reserved boat 103 are included), but doesn't fully represent all the individuals who have
reserved boat 103.
To have a list of the individuals who have reserved boat 103, sailor IDs need to be included
in order to uniquely identify sailors.
SELECT DISTINCT Sname,Sid
FROM SAILOR NATURAL JOIN RESERVATION
WHERE Bid=103
6. Find the names of sailors who have reserved a red boat.
SELECT DISTINCT Sname
FROM ( SAILOR NATURAL JOIN RESERVATION ) NATURAL JOIN BOAT
WHERE Color='red'
This has the same issue as #5 with regards to the (non-)uniqueness of sailor names.
SELECT DISTINCT Color
FROM ( SAILOR NATURAL JOIN RESERVATION ) NATURAL JOIN BOAT
WHERE Sname='Lubber'
8. Find the names of sailors who have reserved at least one boat.
The observation here is that RESERVATION lists only those sailors who have reserved at
least one boat – if a sailor hasn't reserved a boat, there won't be any reservations. So the
task is really just to extract sailor names from the reservations. There is the same issue as
#5 with regards to the (non-)uniqueness of sailor names.
SELECT DISTINCT Sname
FROM SAILOR NATURAL JOIN RESERVATION
9. Sailors can be mentored by someone who has a higher rating and is older. Find all possible
mentoring pairs.
Generate all pairs of sailors, then pick out just those pairs that satisfy the
mentored/mentoring definition –
SELECT *
FROM SAILOR AS S1, SAILOR AS S2
WHERE S2.Rating > S1.Rating AND S2.Age > S1.Age
SELECT DISTINCT Sname
FROM ( SAILOR NATURAL JOIN RESERVATION ) NATURAL JOIN BOAT
WHERE Color='red' OR Color='green'
This has the same issue as #5 with regards to the (non-)uniqueness of sailor names.
SELECT *
FROM SAILOR
WHERE Sname LIKE 'A%'
12. Find all sailors whose names are five letters long.
SELECT *
FROM SAILOR
WHERE Sname LIKE '_____'
13. Find all reservations (date, sailor sids and names, and boat bids and names) for
reservations made during September 1998.
SELECT Day,Sid,Sname,Bid,Bname
FROM ( SAILOR NATURAL JOIN RESERVATION ) NATURAL JOIN BOAT
WHERE Day >= '19980901' AND Day <= '19980930'
14. Find all reservations (date, sailor sids and names, and boat bids and names) for
reservations made on the 10th of (any) month during the 1990s.
SELECT Day,Sid,Sname,Bid,Bname
FROM ( SAILOR NATURAL JOIN RESERVATION ) NATURAL JOIN BOAT
WHERE Day LIKE '199___10'
SELECT Sname,Sid,Age+1 AS NextAge
FROM SAILOR
16. For each sailor, determine the ratio of the sailor's rating to his/her age.
SELECT Sname,Sid,Rating/Age AS Ratio
FROM SAILOR
17. List the sid, name, and rating of each sailor along with the bids and names of the boats
each has reserved, sorted in descending order by rating and, within each rating, in
ascending order by boat name.
DISTINCT is used because a sailor may have reserved the same boat more than once.
SELECT DISTINCT Sid, Sname, Rating, Bid, Bname
FROM SAILOR NATURAL JOIN RESERVATION NATURAL JOIN
ORDER BY Rating DESC, Bname ASC