100% found this document useful (1 vote)
212 views

CTE (Common Table Expression) & Recursive CTE

Common table expressions (CTEs) allow for temporary named result sets that can be referenced within the same statement. CTEs are defined with a WITH clause and can be queried like a view or table. Recursive CTEs allow for repeating queries through self-joins until a termination condition is met, useful for hierarchical or recursive queries.

Uploaded by

Jay Nare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
212 views

CTE (Common Table Expression) & Recursive CTE

Common table expressions (CTEs) allow for temporary named result sets that can be referenced within the same statement. CTEs are defined with a WITH clause and can be queried like a view or table. Recursive CTEs allow for repeating queries through self-joins until a termination condition is met, useful for hierarchical or recursive queries.

Uploaded by

Jay Nare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CTE

(Common Table Expression)


&
Recursive CTE
CTEs
• A common table expression (CTE) is a similar concept to a derived
table in the sense that it’s a named table expression that is visible
only to the statement that defines it.
• Like a query against a derived table, a query against a CTE involves
three main parts:
• ■ The inner query
• ■ The name you assign to the query and its columns
• ■ The outer query
Syntax :

CTEs are table expressions, meaning they return a temporary result that can be
used in the scope of an SELECT, INSERT, UPDATE, DELETE, or APPLY statement.

WITH <CTE_name>
AS
(
<inner_query>
)
<outer_query>;
Example
;WITH C AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY categoryid
ORDER BY unitprice, productid) AS rownum,
categoryid, productid, productname, unitprice
FROM Production.Products
)
SELECT categoryid, productid, productname, unitprice
FROM C
WHERE rownum <= 2;
• Notice that when we define the CTE we give the result a name as well
its columns.  In this way a CTE acts like a VIEW.  

•  The name and columns are defined.  These columns correspond to


the columns returned from the inner query.
Multiple CTEs
• You don’t nest CTEs like you do derived tables. If you need to define multiple CTEs, you simply
separate them by commas.

WITH C1 AS
(
SELECT ...
FROM T1
WHERE ...
),
C2
(
SELECT
FROM C1
WHERE ...
)
SELECT ...
FROM C2
WHERE ...;
Recursive CTE
• Recursive CTEs are use repeated procedural loops.
• The recursive query call themselves until the query satisfied the
condition. In a recursive CTE we should provide a where condition to
terminate the recursion.:

• We will see how to create a simple Recursive query to display the Row
Number from 1 to 10 using a CTE.
Example
Declare @RowNo int =1;
;with ROWCTE as  
   (  
      SELECT @RowNo as ROWNO    
UNION ALL  
      SELECT  ROWNO+1  
  FROM  ROWCTE  
  WHERE RowNo < 10
    )  
 
SELECT * FROM ROWCTE
• A CTE must be followed by a single SELECT, INSERT, UPDATE, or DELETE statement
that references some or all the CTE columns.

• Specifying more than one WITH clause in a CTE is not allowed. For example, if
a CTE_query_definition contains a subquery, that subquery cannot contain a nested
WITH clause that defines another CTE.

The following clauses cannot be used in the CTE_query_definition:

• ORDER BY (except when a TOP clause is specified)


• INTO

• When a CTE is used in a statement that is part of a batch, the statement before it must be
followed by a semicolon.
CTE versus Derived Table
• Derived tables are table results defined in the FROM clause.  Given that derived tables return a table
expression, it should be no surprise that you can use CTEs in their place.

SELECT Quota.TerritoryID,
Quota.TerritoryQuota,
Sales.TerritorySales,
Sales.TerritorySales - Quota.TerritoryQuota
FROM (SELECT TerritoryID,
SUM(SalesQuota) AS TerritoryQuota
FROM Sales.SalesPerson
GROUP BY TerritoryID) AS Quota
INNER JOIN
(SELECT SOH.TerritoryID,
SUM(SOH.TotalDue) AS TerritorySales
FROM Sales.SalesOrderHeader AS SOH
GROUP BY SOH.TerritoryID) AS Sales
ON Quota.TerritoryID = Sales.TerritoryID
Using CTEs instead of derived tables
;WITH Quota (territoryid, quota)
AS (SELECT territoryid,
Sum(salesquota) AS TerritoryQuota
FROM sales.salesperson
GROUP BY territoryid),

Sales (territoryid, sales)


AS (SELECT SOH.territoryid,
Sum(SOH.totaldue) AS TerritorySales
FROM sales.salesorderheader AS SOH
GROUP BY SOH.territoryid)
SELECT Quota.territoryid,
Quota.quota,
Sales.sales,
Sales.sales - Quota.quota
FROM Quota
INNER JOIN
Sales
ON Quota.territoryid = Sales.territoryid;
CTEs versus Subqueries

• There are some differences between subqueries and CTEs, notably:

• A subquery is defined within an outer query. A CTE is defined before


calling it from within the query.
• A CTE can reference itself, a subquery cannot.
• A CTE can reference other CTEs within the same WITH clause (Nest). A
subquery cannot reference other subqueries.
• A CTE can be referenced multiple times from a calling query. A
subquery cannot be referenced.

You might also like