Published on

December 14, 2007

Understanding Derived Tables in SQL Server

When working with SQL Server, you have the ability to create derived tables on the fly and then use these derived tables within your query. In simple terms, a derived table is a virtual table that is calculated on the fly from a select statement. This approach is similar to creating a temporary table and then using it in your query, but it is much simpler and more efficient.

The biggest benefit of using derived tables over temporary tables is that they require fewer steps and everything happens in memory instead of a combination of memory and disk. This results in faster performance. Let’s compare the steps involved when using a temporary table versus a derived table:

Temporary Table:

  1. Lock tempdb database
  2. Create the temporary table (write activity)
  3. Select data & insert data (read & write activity)
  4. Select data from temporary table and permanent table(s) (read activity)
  5. Drop table (write activity)
  6. Release the locks

Derived Table:

  1. Create locks, unless isolation level of “read uncommitted” is used
  2. Select data (read activity)
  3. Release the locks

As you can see, using derived tables reduces disk I/O and can significantly boost performance.

Let’s consider an example to better understand the concept of derived tables. Suppose you have been asked to generate a report that shows the total number of orders each customer placed in the year 1996. You start by writing a query that joins the Customers and Orders tables and filters the results for the year 1996. However, you notice that customers who did not place any orders in 1996 are not showing up in the results.

To include these customers in the results, you might try using an “is null” check in your query. However, this approach still does not include customers who have placed orders, but not in 1996. This is where a derived table can come in handy.

Instead of using the Orders table directly, you can create a derived table that includes only the orders from the year 1996. By joining this derived table with the Customers table, you can ensure that all customers are included in the results, regardless of whether they placed an order in 1996 or not.

Here is an example query that demonstrates the use of a derived table:

SELECT C.CustomerID, C.CompanyName, COUNT(dOrders.OrderID) AS TotalOrders
FROM Customers C
LEFT OUTER JOIN 
    (SELECT * FROM Orders WHERE YEAR(Orders.OrderDate) = 1996) AS dOrders
    ON C.CustomerID = dOrders.CustomerID
GROUP BY C.CustomerID, C.CompanyName

By using the derived table dOrders, we ensure that all customers are included in the results, even if they did not place any orders in 1996.

Derived tables can be particularly useful in scenarios where you need to create a view for a single query and then use it within another query, without the need to create a permanent view in the database. Derived tables are temporary and exist only for the duration of the query, which helps improve performance and eliminates the need for unnecessary catalog entries or temporary tables.

It is important to note that derived tables cannot be updated in SQL Server 2000. However, starting from SQL Server 2005, it is possible to update derived tables.

In conclusion, derived tables are a powerful feature in SQL Server that can greatly enhance query performance and simplify complex queries. By understanding the concept of derived tables and when to use them, you can optimize your SQL Server queries and improve overall database performance.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.