Subqueries in the FROM clause
This topic describes subqueries that occur as nested SELECT statements in the FROM clause of an outer SELECT statement. Such subqueries are sometimes called derived tables or table expressions because the outer query uses the results of the subquery as a data source.
SELECT * FROM (SELECT address.* FROM employee);
address ROW(102 Ruby, Belmont, CA, 49932, 1000)
address ROW(133 First, San Jose, CA, 85744, 4900)
address ROW(152 Topaz, Willits, CA, 69445, 1000))
⋮
This illustrates how to specify a derived table, but it is a trivial example of this syntax, because the outer query does not manipulate any values in the table expression that the subquery in the FROM clause returns. (See Figure 1 for a simple query that returns the same results.)
SELECT LIMIT 1 * FROM
(SELECT c.customer_num, c.lname, c.company,
c.phone, u.call_dtime, u.call_descr
FROM customer c, cust_calls u
WHERE c.customer_num = u.customer_num
ORDER BY u.call_dtime DESC);
customer_num 106
lname Watson
company Watson & Son
phone 415-389-8789
call_dtime 1998-06-12 08:20
call_descr Order was received, but two of the cans of
ANZ tennis balls within the case were empty
In the preceding example, the subquery includes an ORDER BY clause that specifies a column that appears in Projection list of the subquery, but the query would also be valid if the Projection list had omitted the u.call_dtime column. The FROM clause is the only context in which a subquery can specify the ORDER BY clause.