Using multiple tables in a query page 2 |
|||||||||||||||||||
Example 5.6.1 Find the model and the maker of PC priced below $600:
As a result each model of the same maker occurs in the result set only once:
Sometimes the table in the FROM clause need to be pointed more than once. In this case renaming is indispensable. Example 5.6.2 Find the model pairs with equal price:
Here the condition A.model < B.model is to issue one of similar pairs that is only distinguished by rearrangement, for example: {1232, 1233} and {1233, 1232}. The DISTINCT keyword is only used to eliminate duplicate rows because equal models with the same price are in the PC table. As a result, we get the following table:
Renaming is also needed in case the FROM clause uses a subquery. So, the first example can be rewritten as follows:
Note that in this case the Product qualifier may not be already used in other clauses of the SELECT statement. This is because the Product table is just out of use. Instead of this name the Prod alias is used. Moreover, references are only possible to those Product table columns listed in the subquery. Behind a alias of derived table expression in brackets there can be a list of names of columns which will be used instead of columns names of table expression. The order of names should naturally be corresponding to the list of columns of table expression (in our case - to the list in SELECT clause). Thus we can avoid ambiguity of names and, as consequence, necessities of their specification. The previous example now can be rewritten as:
|