Untitled
Untitled
Syntax :
Reference: https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-
sql?view=sql-server-ver15
Continue..
We cannot use the following clauses in CTE:
SQL Server divides the CTE (Common Table Expressions) into two broad categories:
1. Recursive CTE
2. Non-Recursive CTE
Recursive CTE
A common table expression is known as recursive CTE that references itself.
Its concept is based on recursion, which is defined as "the application of a recursive process or definition
repeatedly." When we execute a recursive query, it repeatedly iterates over a subset of the data.
It is simply defined as a query that calls itself. There is an end condition at some point, so it does not call itself
infinitely.
A recursive CTE must have a UNION ALL statement and a second query definition that references the CTE itself in
order to be recursive.
Syntax :
Example:
OUTPUT
WITH
odd_num_cte (id, n) AS
(
SELECT 1, 1
UNION ALL
SELECT id+1, n+2 from
odd_num_cte where id < 5
)
SELECT * FROM odd_num_cte;
Sample File
CTE.sql
Non-Recursive CTE
A common table expression that doesn't reference itself is known as a non-recursive CTE.
A non-recursive CTE is simple and easier to understand because it does not use the concept of
recursion. According to the CTE Syntax, each CTE query will begin with a "With" clause followed by the
CTE name and column list, then AS with parenthesis.
• Recursive CTE members are unable to use the keyword clauses like Distinct, Group By, Having, Top,
Outer Joins.
• The CTE can only be referenced once by the Recursive member.
• We cannot use the table variables and CTEs as parameters in stored procedures.
• Since it's just a shortcut for a query or subquery, it can't be reused in another query.
• The scope of CTE is limited and thump rule. Means it will be available to use in immediate statement
only.
• The number of columns in the CTE arguments and the number of columns in the query must be the
same.
• We cannot perform UPDATE, INSERT, DELETE on a CTE in Azure SQL
Multiple CTEs
• Multiple CTEs are many CTEs in a single query. These CTE are separated by comma and commas
should not be given at the end of the last CTE.
• The WITH keyword will be mentioned only once during the start of the CTE definition. This comma-
separated multiple CTE makes sense when we must refer to the return of one CTE in another CTE
and so on.
• For multiple CTE we need to do set-based operations like UNION, UNION ALL, etc.
Nested CTEs
• Like Subqueries we can't define CTE inside CTE. But we can refer to the previously defined CTE in
subsequent CTEs in the FROM clause.
• Nested CTEs are more useful in cases where we need to compute aggregate functions on several
levels and to compare two or more grouping of rows.
Exercise
• What is the difference between Common Table Expression and Derived Table and SubQuery?
• Can you use a CTE in the WHERE clause?
• Can you DELETE the records from the CTE? Does it also affect the actual underlying base
table?
• How can you get the third-highest salary using the CTE?
• How can you check and remove the duplicate records using the CTE? Demonstrate in Azure.
• How can you apply multi-level aggregations in SQL?