Derived tables allow you to nest queries within a FROM clause. With derived tables, you can perform grouping of groups, or you can construct a join with a group, without having to create a view.
In the following example, the inner SELECT statement (enclosed in parentheses) creates a derived table, grouped by customer ID values. The outer SELECT statement assigns this table the correlation name sales_order_counts and joins it to the Customers table using a join condition.
SELECT Surname, GivenName, number_of_orders FROM Customers JOIN ( SELECT CustomerID, count(*) FROM SalesOrders GROUP BY CustomerID ) AS sales_order_counts (CustomerID, number_of_orders) ON (Customers.ID = sales_order_counts.CustomerID) WHERE number_of_orders > 3;
The result is a table of the names of those customers who have placed more than three orders, including the number of orders each has placed.
For an explanation of key joins of derived tables, see Key joins of views and derived tables.
For an explanation of natural joins of derived tables, see Natural joins of views and derived tables.
For an explanation of outer joins of derived tables, see Outer joins of views and derived tables.