0% found this document useful (0 votes)
19 views3 pages

Practice Set 1

The document outlines the database schema for a sailing club, detailing the Sailor, Boats, and Reserves tables along with their primary and foreign keys. It provides SQL definitions for creating these tables and includes several SQL queries to retrieve specific information about sailors and their boat reservations. The queries cover various scenarios, such as finding sailors who reserved specific boats, counting reservations, and filtering by boat color.

Uploaded by

ujjwalkandpal9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Practice Set 1

The document outlines the database schema for a sailing club, detailing the Sailor, Boats, and Reserves tables along with their primary and foreign keys. It provides SQL definitions for creating these tables and includes several SQL queries to retrieve specific information about sailors and their boat reservations. The queries cover various scenarios, such as finding sailors who reserved specific boats, counting reservations, and filtering by boat color.

Uploaded by

ujjwalkandpal9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Name : Aryan singh rajput

Practice set – 1

Consider the database of a sailing club with the following three tables:

Sailor (sid: integer, sname: string, rating: integer, age: integer)

Boats (bid: integer, bname: string, colour: string)

Reserves (sid: integer, bid: integer, day: date)

Identify primary keys and foreign keys in above relations.

Sailor : Primary key is sid

Boats : Primary key is bid

Reserves : Primary key is sid and bid together

Reserves : sid is foreign key references Sailor

Bid is a foreign key references Boats

A. Write SQL Definition for the above relational Schema with necessary constraints

Answer:

CREATE TABLE Sailor (

sid INTEGER PRIMARY KEY,

sname VARCHAR(50) NOT NULL,

rating INTEGER,

age FLOAT

);

CREATE TABLE Boats (

bid INTEGER PRIMARY KEY,

bname VARCHAR(50) NOT NULL,

colour VARCHAR(30)

);

CREATE TABLE Reserves (


sid INTEGER,

bid INTEGER,

day DATE,

PRIMARY KEY (sid, bid, day),

FOREIGN KEY (sid) REFERENCES Sailor(sid),

FOREIGN KEY (bid) REFERENCES Boats(bid)

);

B. Write the following queries using SQL subqueries.

1. Find names of sailors who’ve reserved boat #103

Answer: SELECT S.sname FROM Sailor S JOIN Reserves R ON S.sid = R.sid WHERE R.bid = 103;

2. Find the names of sailors who’ve reserved a red or a green boat

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);

4. Find the names of boats reserved by Dustin


Answer: SELECT DISTINCT B.bname FROM Boats B JOIN Reserves R ON B.bid = R.bid JOIN Sailor S
ON R.sid = S.sid WHERE S.sname = 'Dustin';

5. Find number of boats reserved by Dustin

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy