0% found this document useful (0 votes)
280 views

SQL 1 Solutions

The document describes a relational schema with three tables: SAILOR, BOAT, and RESERVATION. SAILOR stores information about sailors including ID, name, rating, and age. BOAT stores information about boats including ID, name, and color. RESERVATION stores reservations with sailor ID, boat ID, and date. The document also includes sample data and 17 SQL queries on the tables with explanations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
280 views

SQL 1 Solutions

The document describes a relational schema with three tables: SAILOR, BOAT, and RESERVATION. SAILOR stores information about sailors including ID, name, rating, and age. BOAT stores information about boats including ID, name, and color. RESERVATION stores reservations with sailor ID, boat ID, and date. The document also includes sample data and 17 SQL queries on the tables with explanations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

relational schema

SAILOR ( Sid: integer, Sname: string, Rating: integer, Age: real )


BOAT ( Bid: integer, Bname: string, Color: string )
RESERVATION ( Sid: integer, Bid: integer, Day: date )

RESERVATION.Sid and RESERVATION.Bid are foreign keys referring to SAILOR.Sid and BOAT.Bid,
respectively.

sample data

SAILOR BOAT RESERVATION


Sid Sname Rating Age Bid Bname Color Sid Bid Day
22 Dustin 7 45 101 Interlake blue 22 101 1998-10-10
29 Brutus 1 33 102 Interlake red 22 102 1998-10-10
31 Lubber 8 55.5 103 Clipper green 22 103 1998-10-08
32 Andy 8 25.5 104 Marine red 22 104 1998-10-07
58 Rusty 10 35 31 102 1998-11-10
64 Horatio 7 35 31 103 1998-11-06
71 Zorba 10 16 31 104 1998-11-12
74 Horatio 9 35 64 101 1998-09-05
85 Art 3 25.5 64 102 1998-09-09
95 Bob 3 63.5 74 103 1998-09-08
SQL queries

1. Find the names of sailors over the age of 20.

SELECT Sname
FROM SAILOR
WHERE Age > 20

2. Find the names and ratings of each sailor.

SELECT Sname,Rating
FROM SAILOR

3. Find the names of the boats.

SELECT Bname
FROM BOAT

4. Find all information about sailors over the age of 20.

SELECT *
FROM SAILOR
WHERE Age > 20

5. Find the names of sailors who have reserved boat 103.

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.

7. Find the colors of boats reserved by Lubber.

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

An alternative is to only combine those pairs of sailors who can be mentored/mentors –


SELECT *
FROM SAILOR AS S1 JOIN SAILOR AS S2 ON S2.Rating > S1.Rating AND
                                       S2.Age > S1.Age
10. Find the names of sailors who have reserved a red boat or a green boat.

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.

11. Find all sailors whose names start with A.

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 >= '1998­09­01' AND Day <= '1998­09­30'

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'

15. Determine the age each sailor will be next year.

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

You might also like