Database Management Systems-6
Database Management Systems-6
¬ p, p V q, p q, or p q
X(p(X)), where X is a domain variable
X(p(X)), where X is a domain variable
Examples of DRC Queries
We now illustrate DRC through several examples. The reader is invited to compare
these with the TRC versions.
Aarerestricted to be fields of the same tuple. In comparison with the TRC query, we can
say T > 7 instead of S.rating > 7, but we must specify the tuple 〈I, N, T, A 〉in the result,
(Q1) Find the names of sailors who have reserved boat 103 .
{〈N 〉 | I, T, A(〈I, N, T, A〉 Sailors
Ir, Br, D(〈Ir, Br, D〉 Reserves Ir = I Br = 103))}
(Q2) Find the names of sailors who have reserved a red boat.
(Q9) Find the names of sailors who have reserved all boats.
{〈N 〉 | I, T, A(〈I, N, T, A〉 Sailors
B, BN, C(¬(〈B, BN, C〉 Boats)
〈I, N,T,A〉 in Sailors satisfying the following condition: for every 〈B, BN,C〉, either
this is not a tuple in Boats or there is some tuple 〈Ir, Br, D 〉 in Reserves that proves
that Sailor I has reserved boat B.” The ∀ quantifier allows the domain variables B,
BN, and C to range over all values in their respective attribute domains, and the
This section presents the syntax of a simple SQL query and explains its meaning
through a conceptual evaluation strategy. A conceptual evaluation strategy is a way to
evaluate the query that is intended to be easy to understand, rather than efficient. A
DBMS would typically execute a query in a different and more efficient way.
The answer is a set of rows, each of which is a pair 〈sname, age〉. If two or more sailors
have the same name and age, the answer still contains just one pair with that name and age.
This query is equivalent to applying the projection operator of relational algebra.
The answer to this query with and without the keyword DISTINCT on instance S3 of
Sailors is shown in Figures 5.4 and 5.5. The only difference is that the tuple for
Horatio appears twice if DISTINCT is omitted; this is because there are two sailors
called Horatio and age 35.
Sname age
sname age Dustin 45.0
Dustin 45.0 Brutus 33.0
Brutus 33.0 Lubber 55.5
Lubber 55.5 Andy 25.5
Andy 25.5 Rusty 35.0
Rusty 35.0 Horatio 35.0
Horatio 35.0 Zorba 16.0
Zorba 16.0 Horatio 35.0
Art 25.5 Art 25.5
Bob 63.5 Bob 63.5
Figure 5.4Answer to Q15 Figure 5.5Answer to Q15 without DISTINCT
The select-list is a list of (expressions involving) column names of tables named in the
from-list. Column names can be prefixed by a range variable.
2. Delete those rows in the cross-product that fail the qualification conditions.
(Q1) Find the names of sailors who have reserved boat number 103.
The first step is to construct the cross-product S4 × R3, which is shown in Figure 5.8.
Figure 5.8 S4 × R3
The second step is to apply the qualification S.sid = R.sid AND R.bid=103. (Note that
the first part of this qualification requires a join operation.) This step eliminates all
but the last row from the instance shown in Figure 5.8. The third step is to eliminate
unwanted columns; only sname appears in the SELECT clause. This step leaves us with
the result shown in Figure 5.9, which is a table with a single column and, as it happens,
just one row.
sname
rusty
Figure 5.9 Answer to Query Q1 on R3 and S4
Query Q1, which we discussed in the previous section, can also be expressed as follows:
SELECT sname FROM Sailors S, Reserves R WHERE S.sid = R.sid AND bid=103Only the
occurrences of sid have to be qualified, since this column appears in both the Sailors and
Reserves tables. An equivalent way to write this query is:
Dept of CSE, Unit-2 Page 25
SELECT sname FROM Sailors, Reserves WHERE Sailors.sid = Reserves.sid AND bid=103
(Q16) Find the sids of sailors who have reserved a red boat.
SELECT R.sid FROM Boats B, Reserves R WHERE B.bid = R.bid AND B.color = ‘red’
This query contains a join of two tables, followed by a selection on the color of boats.We
can think of B and R as rows in the corresponding tables that ‘prove’ that a sailor with sid
R.sid reserved a red boat B.bid. On our example instances R2 and S3.
(Q2) Find the names of sailors who have reserved a red boat.
SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid
AND R.bid = B.bid AND B.color = ‘red’
This query contains a join of three tables followed by a selection on the color of boats. The
join with Sailors allows us to find the name of the sailor who, according to Reserves tuple R,
has reserved a red boat described by tuple B.
(Q3) Find the colors of boats reserved by Lubber.
SELECT B.color FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid
AND R.bid = B.bid AND S.sname = ‘Lubber’
This query is very similar to the previous one. Notice that in general there may be
more than one sailor called Lubber (since sname is not a key for Sailors); this query is still
correct in that it will return the colors of boats reserved by some Lubber, if there are
several sailors called Lubber
(Q4) Find the names of sailors who have reserved at least one boat.
SQL supports a more general version of the select-list than just a list of columns. Each
item in a select-list can be of the form expression AS column name, where expression
(Q18) Find the ages of sailors whose name begins and ends with B and has at least
three characters.
(Q5) Find the names of sailors who have reserved a red or a green boat.
If we were to just replace the use of OR in the previous query by AND, in analogy to
the English statements of the two queries, we would retrieve the names of sailors who
have reserved a boat that is both red and green. The integrity constraint that bid is a
key for Boats tells us that the same boat cannot have two colors, and so the variant of the
previous query with AND in place of OR will always return an empty answer set. A correct
statement of Query Q6 using AND is the following:
SELECT S.sname FROM Sailors S, Reserves R1, Boats B1, Reserves R2, Boats
B2 WHERE S.sid = R1.sid AND R1.bid = B1.bid AND S.sid = R2.sid AND
R2.bid = B2.bid AND B1.color=‘red’ AND B2.color = ‘green’
We can think of R1 and B1 as rows that prove that sailor S.sid has reserved a red boat.
R2 and B2 similarly prove that the same sailor has reserved a green boat. S.sname is
not included in the result unless five such rows S, R1, B1, R2, and B2 are found.The OR
query (Query Q5) can be rewritten as follows:
SELECT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’
UNION
SELECT S2.sname
FROM Sailors S2, Boats B2, Reserves R2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = ‘green’
This query says that we want the union of the set of sailors who have reserved red
boats and the set of sailors who have reserved green boats. In complete symmetry, the
AND query (Query Q6) can be rewritten as follows:
SELECT S.sname
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’
INTERSECT
SELECT S2.sname
(Q19) Find the sids of all sailors who have reserved red boats but not green boats.
Sailors 22, 64, and 31 have reserved red boats. Sailors 22, 74, and 31 have reserved
green boats. Thus, the answer contains just the sid 64.
Indeed, since the Reserves relation contains sid information, there is no need to look at
the Sailors relation, and we can use the following simpler query:
SELECT R.sid FROM Boats B, Reserves R
WHERE R.bid = B.bid AND B.color = ‘red’
EXCEPT SELECT R2.sid FROM Boats B2,
Reserves R2 WHERE R2.bid = B2.bid AND
B2.color = ‘green’
Note that UNION, INTERSECT, and EXCEPT can be used on any two tables that are
union-compatible, that is, have the same number of columns and the columns, taken
in order, have the same types. For example, we can write the following query:
(Q20) Find all sids of sailors who have a rating of 10 or have reserved boat 104.
The first part of the union returns the sids 58 and 71. The second part returns 22
and 31. The answer is, therefore, the set of sids 22, 31, 58, and 71. A final point
to note about UNION, INTERSECT, and EXCEPT follows. In contrast to the default that
duplicates are not eliminated unless DISTINCT is specified in the basic query form, the
default for UNION queries is that duplicates are eliminated! To retain duplicates, UNION
ALL must be used; if so, the number of copies of a row in the result is m + n, where
m and n are the numbers of times that the row appears in the two parts of the union.
Similarly, one version of INTERSECT retains duplicates the number of copies of a row
in the result is min(m, n)—and one version of EXCEPT also retains duplicates—the
Dept of CSE, Unit-2 Page 29
number of copies of a row in the result is m − n, where m corresponds to the first
relation.
NESTED QUERIES
A nested query is a querythat has another query embedded within it; the embedded
query is called a subquery .
As an example, let us rewrite the following query, which we discussed earlier, using a nested
subquery:
(Q1) Find the names of sailors who have reserved boat 103.
SELECT S.sname
FROM Sailors S
WHERE S.sid IN ( SELECT R.sid
FROM Reserves R
WHERE R.bid = 103 )
The nested subquery computes the (multi)set of sids for sailors who have reserved boat
103 (the set contains 22, 31, and 74 on instances R2 and S3), and the top-level query
retrieves the names of sailors whose sid is in this set. The IN operator allows us to
test whether a value is in a given set of elements; an SQL query is used to generate
the set to be tested. Notice that it is very easy to modify this query to find all sailors
who have not reserved boat 103—we can just replace IN by NOT IN!
(Q2) Find the names of sailors who have reserved a red boat.
SELECT S.sname
FROM Sailors S
WHERE S.sid IN ( SELECT R.sid
FROM Reserves R
WHERE R.bid IN ( SELECT B.bid
FROM Boats B
WHERE B.color = ‘red’ )
The innermost subquery finds the set of bids of red boats (102 and 104 on instance
B1). The subquery one level above finds the set of sids of sailors who have reserved
one of these boats. On instances B1, R2, and S3, this set of sids contains 22, 31, and64.