POTD 3: SQL Join

Due 20-Feb-2024, 12pm/noon EST

Purpose:

You may make a copy of a worksheet and complete this activity, or simply type your answers in any text editor.

You may work alone or with at most 2 other students (max size=3) in this course.


Import alldbs.sql into your database server (if you have not done so). Write SQL queries to solve the following problems. You will need to consider tables:  BoatsSailorsReserves.

  1. Find the names of sailors who have reserved boat 103
    SELECT sname 
    FROM Sailors NATURAL JOIN Reserves 
    WHERE bid = 103;  
  2. Find the colors of the boats reserved by Guppy.
    SELECT DISTINCT color 
    FROM Boats NATURAL JOIN Reserves NATURAL JOIN Sailors 
    WHERE sname = "Guppy";   
  3. Find the names of sailors who have reserved a red boat
    SELECT DISTINCT sname 
    FROM Sailors NATURAL JOIN Reserves NATURAL JOIN Boats
    WHERE color = "Red";  
  4. Find the names of the sailors who have not reserved a boat
    SELECT sname 
    FROM Sailors LEFT JOIN Reserves ON Sailors.sid = Reserves.sid
    WHERE bid IS NULL;
    
    -- or --
    
    SELECT sname 
    FROM Sailors LEFT OUTER JOIN Reserves ON Sailors.sid = Reserves.sid
    WHERE bid IS NULL; 
  5. Find the sid's of the sailors who have reserved a boat whose name (the boat's name) begins with the letter "M"
    SELECT DISTINCT Sailors.sid
    FROM Sailors NATURAL JOIN Reserves NATURAL JOIN Boats
    WHERE bname LIKE "M%";   
  6. Find the number of times each sailor (sid) reserved a boat whose name (the boat's name) begins with the letter "M"
    SELECT Sailors.sid, COUNT(Sailors.sid)
    FROM Sailors NATURAL JOIN Reserves NATURAL JOIN Boats
    WHERE bname LIKE "M%"
    GROUP BY Sailors.sid; 


Grading rubric

[Total: 10 points]: Done (or provide evidence of your attempt, full or reasonable effort)

(-2.5 points) for 24 hours late (submitted after 20-Feb-2024 12pm EST, by 21-Feb-2024 12pm EST)
(-5 points) for 48 hours late (submitted after 21-Feb-2024 12pm EST, by 22-Feb-2024 12pm EST)


Submission


Copyright © 2024 Upsorn Praphamontripong
Released under the Creative Commons License CC-BY-NC-SA 4.0 license.
Last updated 2024-02-17 22:31