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

DBMS Practice

The document discusses relational algebra expressions to solve various queries on relations representing sailors, boats and reservations. It provides the queries, explains the relational algebra used to solve each query and shows the output.

Uploaded by

Upakul Kalita
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)
8 views

DBMS Practice

The document discusses relational algebra expressions to solve various queries on relations representing sailors, boats and reservations. It provides the queries, explains the relational algebra used to solve each query and shows the output.

Uploaded by

Upakul Kalita
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/ 18

DBMS – SQL

Himanish Shekhar Das


Assistant Professor
Department of Computer Science and Engineering
Email: [email protected]
Contact: 9435427610
SAILORS RESERVES
SID SNAME RATING AGE SID BID DAY
22 A 7 45 22 101 10/10/98
29 B 1 33 22 102 10/10/98
31 C 8 55 22 103 10/8/98
32 D 8 25 22 104 10/7/98
58 E 10 35 31 102 11/10/98
64 F 7 35 31 103 11/6/98
71 G 10 16 31 104 11/12/98
74 F 9 35 64 101 9/5/98
85 I 3 25 64 102 9/8/98
95 J 3 63 74 103 9/8/98
BOATS
BID BNAME COLOR
101 INTERLAKE BLUE
102 INTERLAKE RED
103 CLIPPER GREEN
104 MARINE RED
Questions – Using Relational Algebra
Expressions
1. Find the names of sailors who have reserved boat 103.
2. Find the names of sailors who have reserved a red boat.
3. Find the colors of boats reserved by C.
4. Find the names of sailors who have reserved at least one boat.
5. Find the names of sailors who have reserved a red or a green boat.
6. Find the names of sailors who have reserved a red and a green boat.
7. Find the names of sailors who have reserved at least two boats.
8. Find the sids of sailors with age over 20 who have not reserved a red
boat.
9. Find the names of sailors who have reserved all boats.
10. Find the names of sailors who have reserved all boats called Interlake.
Questions – Using Relational Algebra
Expressions
1. Find the names of sailors who have reserved boat 103.

Answer:
sname: A, C, F
Questions – Using Relational Algebra
Expressions
2. Find the names of sailors who have reserved a red boat.

This query involves a series of two joins. First we choose (tuples


describing) red boats. Then we join this set with Reserves (natural
join, with equality specified on the bid column) to identify
reservations of red boats. Next we join the resulting intermediate
relation with Sailors (natural join, with equality specified on the sid
column) to retrievethe names of sailors who have made reservations
of red boats. Finally, we project the sailors’ names.
Questions – Using Relational Algebra
Expressions
2. Find the names of sailors who have reserved a red boat.

Answer:
Sname: A, C, F
Questions – Using Relational Algebra
Expressions
3. Find the colors of boats reserved by C.

Answer:
Color: green and red
Questions – Using Relational Algebra
Expressions
4. Find the names of sailors who have reserved at least one boat.

A Sailors tuple appears in (some tuple of) this intermediate


relation only if at least one Reserves tuple has the same sid
value, that is, the sailor has made some reservation.

Answer: sname: A,C,F


Questions – Using Relational Algebra
Expressions
5. Find the names of sailors who have reserved a red or a green boat.

We identify the set of all boats that are either red or green (Tempboats, which
contains boats with the bids 102, 103, and 104 on instances B, R, and S). Then
we join with Reserves to identify sids of sailors who have reserved one of these
boats; this gives us sids 22, 31, 64, and 74 over our example instances. Finally,
we join (an intermediate relation containing this set of sids) with Sailors to find
the names of Sailors with these sids. This gives us the names A, C, and F on the
instances B, R, and S.
Questions – Using Relational Algebra
Expressions
5. Find the names of sailors who have reserved a red or a green boat.
Questions – Using Relational Algebra
Expressions
6. Find the names of sailors who have reserved a red and a green boat.

It tries to compute sailors who have reserved a boat that is both red and
green. (Since bid is a key for Boats, a boat can be only one color; this
query will always return an empty answer set.)
Questions – Using Relational Algebra
Expressions
6. Find the names of sailors who have reserved a red and a green boat.

The correct approach is to find sailors who have reserved a red boat,
then sailors who have reserved a green boat, and then take the
intersection of these two sets:

Answer: sname: A,C


Questions – Using Relational Algebra
Expressions
7. Find the names of sailors who have reserved at least two boats.

Answer: sname: A,C,F


Questions – Using Relational Algebra
Expressions
7. Find the names of sailors who have reserved at least two boats.
First we compute tuples of the form sid,sname,bid, where sailor sid has made
a reservation for boat bid; this set of tuples is the temporary relation
Reservations. Next we find all pairs of Reservations tuples where the same
sailor has made both reservations and the boats involved are distinct. Here is
the central idea: In order to show that a sailor has reserved two boats, we
must find two Reservations tuples involving the same sailor but distinct boats.
Over instances B, R, and S, the sailors with sids 22, 31, and 64 have each
reserved at least two boats. Finally, we project the names of such sailors to
obtain the answer, containing the names A, C and F.
Questions – Using Relational Algebra
Expressions
8. Find the sids of sailors with age over 20 who have not reserved a red
boat.

This query illustrates the use of the set-difference operator. Again, we use the
fact that sid is the key for Sailors. We first identify sailors aged over 20 (over
instances B, R, and S, sids 22, 29, 31, 32, 58, 64, 74, 85, and 95) and then
discard those who have reserved a red boat (sids 22, 31, and 64), to obtain
the answer (sids 29, 32, 58, 74, 85, and 95). If we want to compute the
names of such sailors, we must first compute their sids (as shown above),
and then join with Sailors and project the sname values.
Questions – Using Relational Algebra
Expressions
9. Find the names of sailors who have reserved all boats.

The intermediate relation Tempsids is defined using division, and


computes the set of sids of sailors who have reserved every boat (over
instances B, R, and S, this is just sid 22). Division then returns all sids
such that there is a tuple hsid,bidi in the first relation for each bid in the
second. Joining Tempsids with Sailors is necessary to associate names
with the selected sids; for sailor 22, the name is A.
Questions – Using Relational Algebra
Expressions
10. Find the names of sailors who have reserved all boats called
Interlake.

Now we apply a selection to Boats, to ensure that we compute only


bids of boats named Interlake in defining the second argument to the
division operator. Over instances B, R, and S, Tempsids evaluates to
sids 22 and 64, and the answer contains their names, A and F.

You might also like