Raghvendra Singh
What do you mean by Common Table Expression?
By Raghvendra Singh in SQL Server on Jun 22 2009
  • PEEYUSH MISHRA
    Jul, 2009 21

    Common Table Expressions(CTE)  are a new feature  in Microsoft SQL Server 2005 that offer a more readable form of the derived table that can be declared once and referenced multiple times in a query.

    WITH ProductAndCategoryNamesOverTenDollars (ProductName, CategoryName, UnitPrice)

    AS

    (
       SELECT    p.ProductName,
                         c.CategoryName,
                         p.UnitPrice
      FROM Products p  INNER JOIN Categories c ON c.CategoryID = p.CategoryID
      WHERE p.UnitPrice > 10.0
    )

    SELECT *
    FROM ProductAndCategoryNamesOverTenDollars
    ORDER BY CategoryName ASC, UnitPrice ASC, ProductName ASC 

    This query creates a CTE named ProductAndCategoryNamesOverTenRuppes that returns the name, category name, and price of those products whose unit price exceeds 10.00.

    The results of the query.

    In short, a Common Table Expression allows us to define a temporary, view-like construct. We start by (optionally) specifying the columns it returns, then define the query.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS