5.8 Table Product in SQL 87
STNO
----
2415
5544
Neither SQL Server nor MySQL support the minus operation.
5.8 Table Product in SQL
A select phrase that lists several distinct table names after the reserved word
from computes the product of these tables.
Example 5.8.1 To examine all possible pairs of students/instructors we could
write the following select:
select STUDENTS.name, INSTRUCTORS.name
from STUDENTS, INSTRUCTORS;
Since our database is in a state that contains 9 students and five instructors,
this will result in 45 rows retrieved:
NAME NAME
---------------------------------
Edwards P. David Evans Robert
Grogan A. Mary Evans Robert
Mixon Leatha Evans Robert
.
.
.
Pierce Richard Will Samuel
Prior Lorraine Will Samuel
Rawlings Jerry Will Samuel
Lewis Jerry Will Samuel
Observe that the tables are not “linked” by any where condition; as expected
in the definition of the product, all combinations of rows are considered. Af-
ter computing the product, a projection eliminates all attributes except STUDENTS.
name and INSTRUCTORS.name.
Also, note that we use qualified attributes as required by the definition of
table product (see Definition 4.1.7).
The result produced by the query shown in Example 5.8.1 does not differ-
entiate between the attributes STUDENTS.name and INSTRUCTORS.name and
this may confuse the user. Therefore, it is preferable to rename the columns of
the result using the option as:
select STUDENTS.name as stname, INSTRUCTORS.name as instname
from STUDENTS, INSTRUCTORS;
This will generate:
88 SQL — The Relational Language
STNAME INSTNAME
---------------------------------
Edwards P. David Evans Robert
Grogan A. Mary Evans Robert
Mixon Leatha Evans Robert
.
.
.
Pierce Richard Will Samuel
Prior Lorraine Will Samuel
Rawlings Jerry Will Samuel
Lewis Jerry Will Samuel
SQL allows for computations of products of several copies of the same table
through the creation of aliases; the solution proceeds using the logic discussed
in Example 4.1.18. To create an alias S of a table named T we write the name
of the alias after the name of the table in the list of table, making sure that at
least one space (and no comma) exists between the name of the table and its
alias. For example, in the select phrase of Example 5.8.2 we create the alias I
by writing
INSTRUCTORS I
Table aliases are also known as correlation names of tables.
Example 5.8.2 Let us solve the query shown in Example 4.1.18: finding all