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

Assignment5

The document outlines the creation of a database named 'Boating' with three tables: Sailors, Boats, and Reserves. It includes SQL commands for creating these tables, inserting data, and executing various queries to retrieve specific information about sailors and their boat reservations. Additionally, it demonstrates how to perform operations such as counting distinct names and calculating average ages based on ratings.

Uploaded by

Caliberito Awsm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Assignment5

The document outlines the creation of a database named 'Boating' with three tables: Sailors, Boats, and Reserves. It includes SQL commands for creating these tables, inserting data, and executing various queries to retrieve specific information about sailors and their boat reservations. Additionally, it demonstrates how to perform operations such as counting distinct names and calculating average ages based on ratings.

Uploaded by

Caliberito Awsm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ASSIGNMENT 5

CREATE DATABASE BOATING;

USE BOATING;

-- 1. Create the Sailors table

CREATE TABLE Sailors (

sid INT PRIMARY KEY,

sname VARCHAR(50),

rating INT,

age INT

);

-- Insert data into Sailors table

INSERT INTO Sailors (sid, sname, rating, age) VALUES

(1, 'Bob', 5, 25),

(2, 'Alice', 8, 30),

(3, 'Charlie', 7, 22),

(4, 'David', 6, 29),

(5, 'Eve', 7, 27),

(6, 'Tom', 8, 21);

SELECT * FROM Sailors;

-- Output
-- 2. Create the Boats table

CREATE TABLE Boats (

bid INT PRIMARY KEY,

bname VARCHAR(50),

color VARCHAR(20)

);

-- Insert data into Boats table

INSERT INTO Boats (bid, bname, color) VALUES

(101, 'Voyager', 'Red'),

(102, 'Explorer', 'Green'),

(103, 'Adventure', 'Blue'),

(104, 'Odyssey', 'Red'),

(105, 'Pioneer', 'Green');

SELECT * FROM Boats;

-- Output

-- 3. Create the Reserves table

CREATE TABLE Reserves (

sid INT,

bid INT,

day DATE,

PRIMARY KEY (sid, bid, day),

FOREIGN KEY (sid) REFERENCES Sailors(sid),

FOREIGN KEY (bid) REFERENCES Boats(bid)

);
-- Insert data into Reserves table

INSERT INTO Reserves (sid, bid, day) VALUES

(1, 101, '2024-12-01'),

(1, 103, '2024-12-02'),

(2, 102, '2024-12-01'),

(3, 101, '2024-12-03'),

(3, 104, '2024-12-03'),

(4, 105, '2024-12-02'),

(5, 103, '2024-12-04'),

(5, 101, '2024-12-04');

SELECT * FROM Reserves;

-- Output

-- Queries
-- 1. Find all information of sailors who have reserved boat number 101.

SELECT s.* FROM Sailors AS s

JOIN Reserves AS r ON s.sid = r.sid

WHERE r.bid = 101;

-- Output
-- 2. Find the name of boat reserved by Bob.

SELECT bname FROM Boats WHERE bid IN (

SELECT bid FROM Reserves WHERE sid IN (

SELECT sid FROM Sailors WHERE sname = 'Bob'

));

-- Output

-- 3. Find the names of sailors who have reserved a red boat, and list in the order of age.

SELECT sname, age FROM Sailors WHERE sid IN (

SELECT sid FROM Reserves WHERE bid IN (

SELECT bid FROM Boats WHERE color = 'red'

)) ORDER BY age;

-- Output

-- 4. Find the names of sailors who have reserved at least one boat.

SELECT DISTINCT s.sname

FROM Sailors AS s JOIN Reserves AS r

ON s.sid = r.sid;y

-- Output
-- 5. Find the ids and names of sailors who have reserved two different boats on the same day.

SELECT DISTINCT S.sid, S.sname

FROM Sailors S

JOIN Reserves R1 ON S.sid = R1.sid

JOIN Reserves R2 ON S.sid = R2.sid

WHERE R1.day = R2.day

AND R1.bid <> R2.bid;

-- Output

-- 6. Find the ids of sailors who have reserved a red boat or a green boat.

SELECT sid FROM Sailors WHERE sid IN (

SELECT sid FROM Reserves WHERE bid IN (

SELECT bid FROM Boats WHERE color IN ('red', 'green')

));

-- Output

-- 7. Find the name and the age of the youngest sailor.

SELECT sname, age

FROM Sailors

WHERE age = (SELECT MIN(age) FROM Sailors);

-- Output
-- 8. Count the number of different sailor names.

SELECT count(DISTINCT sname) FROM Sailors;

-- Output

-- 9. Find the average age of sailors for each rating level.

SELECT rating, AVG(age) FROM Sailors GROUP BY rating;

-- Output

-- 10. Find the average age of sailors for each rating level that has at least two sailors.

SELECT rating, AVG(age)

FROM Sailors GROUP BY rating

HAVING COUNT(sname) >= 2;

-- Output

You might also like