Recursive CTE in SQL Server
Last Updated :
16 Jan, 2024
CTE which is the abbreviation for Common Table Expression is an SQL Server tool that returns a temporary data set that can be used by another query. In this article, we are going to learn about the Implementation of Recursive CTE in SQL servers. We will understand how recursive common table expressions work step by step and understand their workflow through various examples. Make sure you are familiar with Common table Expressions in SQL before diving into the Recursive CTE's.
Introduction to Recursive CTE in SQL Server
The Common Table Expressions (CTE) were introduced in SQL Server back in 2005. It is a temporary named result set that can make complex queries simple to write and understand by breaking them into smaller parts. The CTEs can be referenced with INSERT, UPDATE, DELETE, or SELECT statements in SQL. CTE returns a temporary result set that is referenced by another query. As the result set is temporary, it is not stored anywhere in the memory and thus it makes CTE an effective tool that can be used for reference to any other table.
A recursive CTE references itself during the execution. It returns a subset of the result, then it recursively references itself and terminates when it has returned all the results.
Recursive CTEs enable the user to process hierarchical data and allow to join all levels of hierarchy present in the database.
Syntax:
WITH RECURSIVE
cte_name [(col1, col2, ...)]
AS ( subquery )
Select col1, col2, .. from cte_name;
Explanation of Syntax:
- cte_name: Name given to recursive subquery written in subquery block.
- col1, col2, ...colN: The name given to columns generated by subquery.
- Subquery: A MySql query that refer to itself using cte_name as its own name.
Workflow of Recursive CTE
Workflow of recursive CTEThe workflow of RECURSIVE CTE is majorly consists of these 5 steps:
- Anchor Member Execution: The CTE starts it's execution with the anchor member which is a non recursive query. It initializes the initial rows of the CTE that serves as the starting point of the recursion.
- Recursive Member Execution(Iterations): The recursive member mainly consists of the SELECT statement that references the CTE itself. Each iteration uses the results that were obtained in the previous iteration or the initial anchor member.This process is repeated until a termination condition is met.
- Termination Condition Check: The termination condition is essential for the termination of the recursive query in the CTE or else our query can get stuck in an infinite loop. This mainly consists of WHERE clause that filters the rows. When the termination condition is met the recursive member halts the execution and returns the result set.
- Union Result Sets: The UNION ALL operator combines the results from the anchor member as well as the results obtained from all of the iterations of the recursive member. At this step the final result set is prepared which will be returned to the user.
- Return Final Result: Finally the result set formed in the previous steps is returned to the user as the output generated by the recursive CTE.
Examples of Recursive CTE in SQL server
Let us consider the following table Organization on which we want to find the Hierarchical relationships between the employees and managers of a respective organization.
Table-OrganizationExample 1: Find Hierarchical Relationship of All Levels
Query:
WITH RecursiveOrganizationCTE AS
(
SELECT EmployeeID, FirstName, LastName, Department,ManagerID
FROM Organization
WHERE ManagerID IS NULL
UNION ALL
SELECT e.EmployeeID, e.FirstName, e.LastName, e.department,e.ManagerID
FROM Organization e
JOIN RecursiveOrganizationCTE r ON e.ManagerID = r.EmployeeID
)
--Show the records stored inside the CTE we created above
SELECT *
FROM RecursiveOrganizationCTE;
Output:
OutputExplanation: The following query uses a recursive CTE to extract hierarchical structure of a table based on the organizational relationships within the table Organization. The query shows the managers who are also employees within the organization at the top level as they have managerID as NULL , then it shows employees who reports to managers as well as act as managers themselves. At last the employees who are not managers who belongs to the last level of the hierarchical structure is shown in the result set.
Example 2: Simple Recursive CTE to Find Out Days in a Week
In the following example we will see how we can use the Recursive CTE in SQL server to extract all the days in a week easily.
Query:
WITH DaysofWeek(x, WeekDays)
AS (
SELECT 0, DATENAME(DW, 0)
UNION ALL
SELECT x + 1, DATENAME(DW, x + 1)
FROM DaysofWeek WHERE x < 6
)
SELECT WeekDays FROM DaysofWeek;
Output:
OutputExplanation: The following query extracts all the days in a week with the help of Recursive CTE and DATENAME() function. The DATENAME() function returns the name of the weekday based on the weekday number. The anchor member is set as 0 and returns the Monday. The recursive member returns the next day of the week starting from tuesday till Sunday based on the value of x which is incremented by 1 recursively after each call.
Example 3: Simple Recursive CTE to Find Out All the Months in a Year
In the following example we will see how we can use the Recursive CTE in SQL server to extract all the months in a year .
Query:
WITH RecursiveMonths AS (
SELECT
1 AS MonthNumber,
DATENAME(MONTH, CAST('2024-01-01' AS DATE)) AS MonthName
UNION ALL
SELECT
MonthNumber + 1,
DATENAME(MONTH, DATEADD(MONTH, MonthNumber, '2024-01-01'))
FROM RecursiveMonths
WHERE MonthNumber < 12
)
SELECT * FROM RecursiveMonths;
Output:
OutputExplanation: The following query is used to extract all the months in a year with the help of Recursive CTE and DATENAME() function. The DATENAME() function returns the name of the month based on the month number. The anchor member is set as 1 and returns the January month. The recursive member returns the next month of the year starting from February to Decemeber based on the value of monthnumber which is incremented by 1 recursively after each call.
Example 4: Find Relationship Between Data Present in the Different Tables Through Recursive CTE.
In the following example we will see how we can use the Recursive CTE to extract meaningful data from multiple tables. Let us consider two tables CityData and CityRoutes on which we will perform our operations.
After inserting some data into the CityData Table, The Table Looks:
Table- CityDataAfter inserting some data into the CityRoutes Table, The Table Looks:
Table-City RoutesQuery:
WITH Destinations AS (
SELECT
RouteID,
SourceCityID,
DestinationCityID,
CAST(CityName AS VARCHAR(MAX)) AS Route,
Distance
FROM CityRoutes CR
INNER JOIN CityData CD ON CR.SourceCityID = CD.CityID
UNION ALL
SELECT
CR.RouteID,
CR.SourceCityID,
CR.DestinationCityID,
CAST(R.Route + ' -> ' + CD.CityName AS VARCHAR(MAX)),
R.Distance + CR.Distance
FROM Destinations R
INNER JOIN CityRoutes CR ON R.DestinationCityID = CR.SourceCityID
INNER JOIN CityData CD ON CR.DestinationCityID = CD.CityID
)
SELECT Route, Distance
FROM Destinations
WHERE SourceCityID = 3;
Output:
OutputExplanation: The following query uses Recursive CTE as well as multiple JOINS to find out the best route and it's total distance from the id of the source city provided by the user in the query. The anchor member SELECT selects the routes from the CityRoutes table joining it with the CityData table. The recursive member UNION ALL selects additional routes by joining the CityRoutes table with the CTE itself.
Conclusion
In the following article, we have learned how Recursive common table expressions work in SQL server. We have learned the workflow of Recursive CTE along with different examples. We have learned how we can use recursive CTEs to find out the hierarchical relationships within a table and we have also learned how we can use recursive CTE along with two different tables as well. We hope this article has helped you to understand Recursive CTE in SQL Servers.
Similar Reads
SQL Server REPLACE() Function
SQL Server is a strong relational database management system (RDBMS) developed to manage large data efficiently. In SQL Server, the REPLACE() function is used to modify or replace a substring within a given string. Taking about the real word uses, the REPLACE() function is vastly used in data proces
4 min read
Recursive Join in SQL
In SQL, a recursive join is a powerful technique used to handle hierarchical data relationships, such as managing employee-manager relationships, family trees, or any data with a self-referential structure. This type of join enables us to combine data from the same table repeatedly, accumulating rec
3 min read
SQL Server TRY PARSE() Function
SQL Server is a Relational Database Management System(RDBMS), which is used to handle, manage and utilize the data of organizations and so on. It provides various effective functions to manage things efficiently and gives exceptional output. In this article, we will understand one of the important f
8 min read
SQL Server CASE Expression
The CASE expression is used to show another column which can be evaluated based on the conditions depending on the existing columns. The CASE expression consists of WHEN and THEN statements. WHEN is used when the condition satisfies and THEN executes the operation or assignment of value of that colu
6 min read
Find Duplicates in MS SQL Server
Finding duplicate values in a database is a common task when managing data integrity. In SQL, several methods can be employed to identify and handle duplicate entries. In this article, We will explore two effective techniques for locating duplicates using SQL queries: the GROUP BY clause and the ROW
4 min read
CREATE TABLE in SQL Server
SQL Server provides a variety of data management tools such as querying, indexing, and transaction processing. It supports multiple programming languages and platforms, making it a versatile RDBMS for various applications. With its robust features and reliability, SQL Server is a popular choice for
4 min read
SQL Server FULL OUTER JOIN
Joins in SQL are used to retrieve data from multiple tables based on a related column (or common column) between them. In this article, we will learn how to use FULL OUTER JOIN, which returns all rows from both tables being joined. It combines the results of both LEFT OUTER JOIN and RIGHT OUTER JOIN
6 min read
Reverse PIVOT Table in SQL Server
In SQL Server, the PIVOT operation is a powerful feature that allows you to transform rows into columns, providing a convenient way to structure and analyze data. However, there are situations where we may need to reverse this operation, converting columns back into rows. This process is commonly kn
7 min read
SQL Server COALESCE() Function
The COALESCE() function in SQL Server is a powerful tool designed to handle NULL values effectively. It evaluates a list of expressions in a specified order and returns the first non-null value encountered. In this article, We will learn about the SQL Server COALESCE() by understanding various examp
4 min read
GROUPING ID Function in SQL Server
SQL Server is a Relational Database Management System that is used to create and manipulate the database. It provides advanced security measures like encryption, access control, and auditing to protect sensitive data from unauthorized access. It Supports a wide range of data types, including structu
6 min read