0% found this document useful (0 votes)
60 views10 pages

Database Management Systems-6

This document provides examples of queries in DRC (Domain Relational Calculus) format and explains the syntax of basic SQL queries. It includes: (1) Examples of DRC queries to find sailor names based on rating, reserved boat number, boat color, and number of reservations. (2) An explanation of the basic form of SQL queries as SELECT statements with FROM, WHERE, and DISTINCT clauses. (3) A conceptual strategy for evaluating SQL queries by computing cross products, applying qualifications, projecting columns, and eliminating duplicates.

Uploaded by

Arun Sasidharan
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)
60 views10 pages

Database Management Systems-6

This document provides examples of queries in DRC (Domain Relational Calculus) format and explains the syntax of basic SQL queries. It includes: (1) Examples of DRC queries to find sailor names based on rating, reserved boat number, boat color, and number of reservations. (2) An explanation of the basic form of SQL queries as SELECT statements with FROM, WHERE, and DISTINCT clauses. (3) A conceptual strategy for evaluating SQL queries by computing cross products, applying qualifications, projecting columns, and eliminating duplicates.

Uploaded by

Arun Sasidharan
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/ 10

any atomic formula

¬ 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.

(Q11) Find all sailors with a rating above 7.

{〈I, N, T, A〉 | 〈I, N, T, A〉  Sailors  T > 7}


This differs from the TRC version in giving each attribute a (variable) name. The

condition 〈I, N, T, A〉  Sailors ensures that the domain variables I, N , T , and

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,

rather than just S.

(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.

{〈N 〉 | I, T, A(〈I, N, T, A〉  Sailors

I, Br, D〉  Reserves  〈Br, BN,′red′〉  Boats)}


(Q7) Find the names of sailors who have reserved at least two boats.
{〈N 〉 | I, T, A(〈I, N, T, A〉  Sailors  Br1, Br2, D1, D2(〈I, Br1, D1〉 Reserves 〈I, Br2,
D2〉  Reserves  Br1 = Br2)
Notice how the repeated use of variable I ensures that the same sailor has reserved both
the boats in question.

(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) 

Dept of CSE, Unit-2 Page 21


(〈Ir, Br, D〉 Reserves(I = Ir  Br = B))))}
This query can be read as follows: “Find all values of N such that there is some tuple

〈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

pattern ‘¬(〈B, BN, C〉  Boats)∨ ’ is necessary to restrict attention to those values

that appear in tuples of Boats.

THE FORM OF A BASIC SQL QUERY

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.

Sid sname rating age sid bid day


22 Dustin 7 45.0 22 101 10/10/98
29 Brutus 1 33.0 22 102 10/10/98
31 Lubber 8 55.5 22 103 10/8/98
32 Andy 8 25.5 22 104 10/7/98
58 Rusty 10 35.0 31 102 11/10/98
64 Horatio 7 35.0 31 103 11/6/98
71 Zorba 10 16.0 31 104 11/12/98
74 Horatio 9 35.0 64 101 9/5/98
85 Art 3 25.5 64 102 9/8/98
95 Bob 3 63.5 74 103 9/8/98
Figure 5.1An Instance S3 of Sailors Figure 5.2 An Instance R2 of Reserves

bid bname color


101 Interlake blue
102 Interlake red
103 Clipper green
104 Marine red

Dept of CSE, Unit-2 Page 22


Figure 5.3 An Instance B1 of Boats

The basic form of an SQL query is as follows:

SELECT [ DISTINCT ] select-list FROM from-list WHERE qualification

Such a query intuitively corresponds to a relational algebra expression involving selec-


tions, projections, and cross-products. Every query must have a SELECT clause, which
specifies columns to be retained in the result, and a FROM clause, which specifies a
cross-product of tables. The optional WHERE clause specifies selection conditions on
the tables mentioned in the FROM clause. Let us consider a simple query.
(Q15) Find the names and ages of all sailors.

SELECT DISTINCT S.sname, S.age FROM Sailors S

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

(Q11) Find all sailors with a rating above 7.

Dept of CSE, Unit-2 Page 23


SELECT S.sid, S.sname, S.rating, S.age FROM Sailors AS S WHERE S.rating > 7
We now consider the syntax of a basic SQL query in more detail.
 The from-list in the FROM clause is a list of table names. A table name can be
followed by a range variable; a range variable is particularly useful when the same
table name appears more than once in the from-list.

 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.

 The qualification in the WHERE clause is a boolean combination (i.e., an expres-


sion using the logical connectives AND, OR, and NOT) of conditions of the form
expression op expression, where op is one of the comparison operators {<, <=, =
, <>, >=, >}.2 An expression is a column name, a constant, or an (arithmetic or
string) expression.

 The DISTINCT keyword is optional. It indicates that the table computed as an


answer to this query should not contain duplicates, that is, two copies of the same row.
The default is that duplicates are not eliminated.
the syntax of a basic SQL query, they don’t tell us the meaning of a query. The answer to
a query is itself a relation which is a multiset of rows in SQL whose contents can be
understood by considering the following conceptual evaluation strategy:
1. Compute the cross-product of the tables in the from-list.

2. Delete those rows in the cross-product that fail the qualification conditions.

3. Delete all columns that do not appear in the select-list.

4. If DISTINCT is specified, eliminate duplicate rows.

(Q1) Find the names of sailors who have reserved boat number 103.

It can be expressed in SQL as follows.SELECT S.sname FROM Sailors S, Reserves R


WHERE S.sid = R.sid AND R.bid=103Let us compute the answer to this query on the
instances R3 of Reserves and S4 of Sailors shown in Figures 5.6 and 5.7, since the

Dept of CSE, Unit-2 Page 24


computation on our usual example instances (R2 and S3) would be unnecessarily
tedious.
sid sna rati age
22 me
Dus 7ng 45.0
31 tin
Lub 8 55.5
58 ber
Rust 10 35.0
sid bid day y
22 101 10/10/96
58 103 11/12/96

Figure 5.6Instance R3 of Reserves Figure 5.7Instance S4 of Sailors

The first step is to construct the cross-product S4 × R3, which is shown in Figure 5.8.

sid sname rating age sid bid day


22 Dusti 7 45.0 22 101 10/10/96
22 n
Dusti 7 45.0 58 103 11/12/96
31 n
Lubbe 8 55.5 22 101 10/10/96
31 r
Lubbe 8 55.5 58 103 11/12/96
58 r
Rusty 10 35.0 22 101 10/10/96
58 Rusty 10 35.0 58 103 11/12/96

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

Examples of Basic SQL Queries

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.

SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid


The join of Sailors and Reserves ensures that for each selected sname, the sailor has
made some reservation. (If a sailor has not made a reservation, the second step in
the conceptual evaluation strategy would eliminate all rows in the cross-product that
involve this sailor.)
Expressions and Strings in the SELECT Command

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

Dept of CSE, Unit-2 Page 26


is any arithmetic or string expression over column names (possibly prefixed by range
variables) and constants.
(Q17) Compute increments for the ratings of persons who have sailed two different
boats on the same day.

SELECT S.sname, S.rating+1 AS rating FROM Sailors S, Reserves R1, Reserves R2


WHERE S.sid = R1.sid AND S.sid = R2.sid AND R1.day = R2.day AND R1.bid <>
R2.bid
Also, each item in a qualification can be as general as expression1 = expression2.

SELECT S1.sname AS name1, S2.sname AS name2 FROM Sailors S1, Sailors


S2 WHERE 2*S1.rating = S2.rating-1.

(Q18) Find the ages of sailors whose name begins and ends with B and has at least
three characters.

SELECT S.age FROM Sailors S WHERE S.sname LIKE ‘B %B’

The only such sailor is Bob, and his age is 63.5.


UNION, INTERSECT, AND EXCEPT
SQL provides three set-manipulation constructs that extend the basic query form pre-
sented earlier. Since the answer to a query is a multiset of rows, it is natural to consider the
use of operations such as union, intersection, and difference. SQL supports these
operations under the names UNION, INTERSECT, and EXCEPT.4 SQL also provides other set
operations: IN (to check if an element is in a given set),op ANY,op ALL(tocom-pare a
value with the elements in a given set, using comparison operator op), and EXISTS
(to check if a set is empty). IN and EXISTS can be prefixed by NOT, with the obvious
modification to their meaning. We cover UNION, INTERSECT, and EXCEPT in this section.
Consider the following query:

(Q5) Find the names of sailors who have reserved a red or a green 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’ OR B.color = ‘green’)

Dept of CSE, Unit-2 Page 27


This query is easily expressed using the OR connective in the WHERE clause. However, the
following query, which is identical except for the use of ‘and’ rather than ‘or’ in the
English version, turns out to be much more difficult :
(Q6) Find the names of sailors who have reserved both a red and 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

Dept of CSE, Unit-2 Page 28


FROM Sailors S2, Boats B2, Reserves R2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = ‘green’

(Q19) Find the sids of all sailors who have reserved red boats but not green boats.

SELECT S.sid FROM Sailors S, Reserves R, Boats B


WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’
EXCEPT SELECT S2.sid FROM Sailors S2, Reserves R2, Boats B2
WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color = ‘green’

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.

SELECT S.sid FROM Sailors S WHERE S.rating = 10


UNION
SELECT R.sid FROM Reserves R WHERE R.bid = 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 .

Introduction to Nested Queries

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.

Dept of CSE, Unit-2 Page 30

You might also like