Relational Algebra Query
Relational Algebra Query
TRC query to find the names of sailors who have reserved boat 103
{S | ∃R, B (R ∈ Reserve ∧ B ∈ Boat ∧ R.sid = S.sid ∧ R.bid = B.bid ∧ B.bid = 103)}
Explanation:
Write a DRC query to find the names of sailors who have reserved boat 103?
{<s.name> | ∃s, r, b (s ∈ Sailors ∧ r ∈ Reserve ∧ b ∈ Boat ∧ r.sid = s.sid ∧ r.bid = b.bid ∧
b.bid = 103)}
Explanation:
Write a query to find the names of sailors who have reserved a red boat?
To find the names of sailors who have reserved a red boat, you would need to join the Sailors,
Reserves, and Boats tables. Assuming the relevant columns in each table are sailor_name in
the Sailors table, sailor_id in the Reserves table, boat_id in the Reserves table, and color in
the Boats table, the query would look like this:
SELECT s.sailor_name
FROM Sailors s
JOIN Reserves r ON s.sailor_id = r.sailor_id
JOIN Boats b ON r.boat_id = b.boat_id
WHERE b.color = 'red';
This query joins the three tables based on their respective keys and then filters the result to
only include sailors who have reserved a red boat.
Write a query to find the names of sailors who have not reserved a red boat?
To find the names of sailors who have not reserved a red boat, you can use a subquery to
exclude the sailors who have reserved a red boat. Here's an example query:
SELECT sailor_name
FROM Sailors
WHERE sailor_id NOT IN (
SELECT r.sailor_id
FROM Reserves r
JOIN Boats b ON r.boat_id = b.boat_id
WHERE b.color = 'red'
);
This query uses a subquery to find the sailor_id of sailors who have reserved a red boat. The
outer query then selects the sailor_name from the Sailors table for sailors whose sailor_id is
not present in the subquery result, effectively giving you the names of sailors who have not
reserved a red boat.
NESTED QUERY
A nested query, also known as a subquery, is a query that is embedded within another query.
It is used to retrieve data from one or more tables based on conditions that involve data from
another table or the same table.
Write a nested query to find the names of sailors who have reserved both a red and
green boat?
Here's a nested query to find the names of sailors who have reserved both a red and green
boat: