Notes
Chapter 20: Recursive Functions and CTEs

Recursive functions and Common Table Expressions (CTEs) are powerful tools in Microsoft SQL Server for working with hierarchical or recursive data structures. In this chapter, we will explore the concept of recursive queries, how to use CTEs to create them, and real-world examples.

1. Recursive Queries

A recursive query is a query that refers to itself in its definition. It is often used to work with hierarchical data structures like organizational charts, file systems, or bill of materials.

Basic Structure of Recursive Query:

Example :

Recursive query to retrieve employees and their managers WITH RecursiveQuery
AS (
SELECT
EmployeeID, FirstName, LastName, ManagerID
FROM Employees
ManagerID IS NULL Anchor member(s)
UNION ALL SELECT e.EmployeeID, e.FirstName, e.LastName, e.ManagerID
FROM Employees AS e
JOIN RecursiveQuery AS r ON e.ManagerID = r.EmployeeID Recursive member(s)
) SELECT * FROM RecursiveQuery;

In this example, the recursive query retrieves employees and their managers.

2. Common Table Expressions (CTEs)

A Common Table Expression (CTE) is a named temporary result set that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement. CTEs are commonly used in conjunction with recursive queries to simplify their structure.

Creating a CTE:

Example :

Creating a CTE for a recursive query WITH RecursiveCTE
AS (
SELECT
EmployeeID, FirstName, LastName, ManagerID
FROM Employees
WHERE ManagerID IS NULL
UNION ALL SELECT
e.EmployeeID, e.FirstName, e.LastName, e.ManagerID
FROM Employees AS e
JOIN RecursiveCTE AS r ON e.ManagerID = r.EmployeeID
) SELECT * FROM RecursiveCTE;

In this example, the recursive query retrieves employees and their managers.

3. Real-world Examples of Recursion

Organizational Chart:
  • Employee Hierarchy: Use recursion to retrieve a hierarchical list of employees and their managers in a large organization.
  • Reporting Structures: Determine the direct and indirect reports for a given manager.
File Systems:
  • Folder Structure: Navigate and search through a complex folder structure using recursive queries to find specific files or directories.
Bill of Materials:
  • Product Assembly: Analyze product structures, determine component requirements, and calculate costs in manufacturing.

4. Key Takeaways

  • Recursive queries in SQL Server are used to work with hierarchical data structures.
  • Common Table Expressions (CTEs) simplify the structure of recursive queries.
  • Real-world applications of recursion include organizational charts, file systems, and bill of materials.

4. Key Takeaways

  • Recursive queries in SQL Server are used to work with hierarchical data structures.
  • Common Table Expressions (CTEs) simplify the structure of recursive queries.
  • Real-world applications of recursion include organizational charts, file systems, and bill of materials.
Cross Apply and Subqueries

In Microsoft SQL Server, subqueries are powerful tools that allow you to nest one query within another. This chapter explores subqueries, their use cases, and the CROSS APPLY operator for real-world scenarios.

Read More
Conditional Logic and UNION

Conditional logic and the UNION operator are essential tools in Microsoft SQL Server for manipulating and combining data from multiple sources. In this chapter, we'll explore the usage of CASE statements, UNION, and real-world examples of conditional logic in SQL.

Read More