0% found this document useful (0 votes)
15 views4 pages

Relational Algebra Query

The document discusses relational algebra queries to find sailors who have reserved a specific boat and correlated nested queries. It provides the TRC and DRC queries to find sailors who have reserved boat 103 and explanations. It also discusses using correlated nested queries to find sailors who have reserved red boats or not reserved red boats and provides examples.

Uploaded by

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

Relational Algebra Query

The document discusses relational algebra queries to find sailors who have reserved a specific boat and correlated nested queries. It provides the TRC and DRC queries to find sailors who have reserved boat 103 and explanations. It also discusses using correlated nested queries to find sailors who have reserved red boats or not reserved red boats and provides examples.

Uploaded by

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

UNIT-3

PRACTICES ON RELATIONAL ALGEBRA


Write a TRC query to find the names of sailors who have reserved boat 103?

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:

S represents the Sailors relation.


R represents the Reserve relation.
B represents the Boat relation.
The query selects sailors (S) for whom there exists a tuple (R) in the Reserve relation and a
tuple (B) in the Boat relation such that the sailor's ID (S.sid) matches the reserved ID (R.sid),
the reserved boat ID (R.bid) matches the boat ID (B.bid), and the boat ID is 101 (B.bid =
103).

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:

s represents the Sailors relation.


r represents the Reserve relation.
b represents the Boat relation.
The query selects the name attribute (<s.name>) of sailors (s) for whom there exists a tuple
(r) in the Reserve relation and a tuple (b) in the Boat relation such that the sailor's ID (r.sid)
matches the sailor's ID (s.sid), the reserved boat ID (r.bid) matches the boat ID (b.bid), and
the boat ID is 101 (b.bid = 103).
CORRELATED NESTED QUERIES
Correlated nested queries, also known as correlated subqueries, are SQL queries where the
subquery references a column from the outer query. The subquery is executed for each row of
the outer query, allowing for conditional filtering or calculations based on values from the
outer query. The result of the subquery is used in the evaluation of the outer query.

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:

SELECT DISTINCT s.name


FROM sailors s
WHERE EXISTS (
SELECT *
FROM reserves r1
JOIN boats b1 ON r1.bid = b1.bid
WHERE r1.sid = s.sid AND b1.color = 'red'
) AND EXISTS (
SELECT *
FROM reserves r2
JOIN boats b2 ON r2.bid = b2.bid
WHERE r2.sid = s.sid AND b2.color = 'green'
);
In this query, we use two nested subqueries. The first subquery checks if there exists a
reservation where the sailor has reserved a red boat, and the second subquery checks if there
exists a reservation where the sailor has reserved a green boat. By using the EXISTS keyword
and correlating the subqueries with the main query using the sailor ID (sid), we can find the
sailors who have reserved both a red and green boat
Write a nested query to find the names of sailors who have reserved all boats?
SELECT s.name
FROM sailors s
WHERE NOT EXISTS (
SELECT b.bid
FROM boats b
WHERE NOT EXISTS (
SELECT *
FROM reserves r
WHERE r.sid = s.sid AND r.bid = b.bid
)
);
In this query, we use two levels of nested subqueries. The innermost subquery selects all boat
IDs (bid) for which there is no reservation by the current sailor (s.sid). The middle subquery
checks if there is any boat that has not been excluded by the inner subquery, meaning the
sailor has reserved it. Finally, the outermost query selects the sailor names where there is no
boat remaining that they haven't reserved. This effectively finds the sailors who have reserved
all boats.

You might also like