🧠Second Brain
Search
Common Table Expression (CTE)
A Common Table Expression (CTE) is a temporary, named result set, which can be referenced within SELECT, INSERT, UPDATE, or DELETE statements. CTEs are also usable in Views.
|
|
# Types of CTEs: Recursive and Non-Recursive
# Non-Recursive CTE
There are two main types of CTEs: Recursive and Non-Recursive.
Non-recursive CTEs are simpler, used to reduce SQL duplication by replacing repeated SQL statements with a reference name.
Example:
|
|
# Recursive CTE
Recursive CTEs incorporate repeated procedural loops, hence the term “recursive.” These CTEs call themselves until a specified condition is met. In a recursive CTE, a termination condition is crucial to avoid infinite recursion.
Recursive CTEs are particularly useful for querying hierarchical data, such as organizational charts (where employees report to managers) or multi-level bills of materials (where products comprise multiple components, each of which may also consist of other components).
|
|
For more details, visit 5 Practical SQL CTE Examples | LearnSQL.com.
Origin:
5 Practical SQL CTE Examples | LearnSQL.com.
References:
Created 2023-10-31