I this post, I will explain Why we need CTE (Common Table Expression)?
A CTE stands for Common Table Expression. A CTE is a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. A CTE always returns a result set.
There are several reasons why we may want to use a CTE over other
methods. Some of them include:
- Readability– CTE’s promote readability. Rather than lump all you query logic into one large query, create several CTE’s, which is then combined later in the statement. This lets you get the chunks of data you need and combine them in a final SELECT.
- Substitute for a View– You can substitute a CTE for a view. This is handy if you don’t have permissions to create a view object or you don’t want to create one as it is only used in this one query.
- Recursion– Use CTE’s do create recursive queries, that is queries that can call themselves. This is handy when you need to work on hierarchical data such as organization charts.
- Limitations– Overcome SELECT statement limitations, such as referencing itself (recursion), or performing GROUP BY using non-deterministic functions.
- Ranking– Whenever you want to use a ranking function such as ROW_NUMBER(), RANK(), NTILE() etc.
0 comments:
Post a Comment
Please do not enter any spam link in the message box.