Write for Us
Derived Tables
Using a derived table is in effect like using a temporary table, without the hassle of specifically creating and referring to one. I have re-coded the above BOL query to use derived tables in the example below:SELECT Ord.OrderID, Ord.OrderDate, maxUnitPriceFROM Northwind.dbo.Orders AS Ord INNER JOIN(SELECT orderID,MAX(UnitPrice) AS maxUnitPriceFROM Northwind.dbo.[Order Details]GROUP BY OrderID) AS OrdDetON ordDet.orderID = Ord.orderIDORDER BY Ord.OrderID DESC, Ord.OrderDate, maxUnitPriceThe code in red causes the SQL Server optimizer to generate a notional (or derived) table called OrdDet for the duration of the query. The derived table notionally takes up much less space than the original [order details] table, because it contains only two columns and only one detail row for each order. Because of this, my ‘derived table’ version of the query should run even faster than the INNER JOIN version. When I checked the execution plan for the derived table version against that of the "join" version to see what sort of improvement I got, the results came out ... exactly the same!