It’s a headache for developers to write or read a complex SQL query using a number of
When dealing with sub-queries, it is often required that you need to select a part of the data from a sub query or even join data from a query with some other tables. In that case, either you have an option to name your sub-queries with an alias or to use it directly. Gradually your requirement is getting more and more complex and your query would look unmaintainable at any time. CTE allows you to define the subquery at once, name it using an alias and later call the same data using the alias just like what you do with a normal table. CTE is standard ANSI SQL standard.
The query looks really a mess. Even if I need to write something that wraps around the entire query, it would gradually become unreadable. CTE allows you to generate Tables beforehand and use it later when we actually bind the data into the output.
According to the syntax, the CTE starts with a
Join
s.
Complex SQL statements can be made easier to understand and
maintainable in the form of CTE or Common Table expressions. In this
post, I will try to cover some of the benefits that we get when working
with CTE. When dealing with sub-queries, it is often required that you need to select a part of the data from a sub query or even join data from a query with some other tables. In that case, either you have an option to name your sub-queries with an alias or to use it directly. Gradually your requirement is getting more and more complex and your query would look unmaintainable at any time. CTE allows you to define the subquery at once, name it using an alias and later call the same data using the alias just like what you do with a normal table. CTE is standard ANSI SQL standard.
SELECT * FROM (
SELECT A.Address, E.Name, E.Age From Address A
Inner join Employee E on E.EID = A.EID) T
WHERE T.Age > 50
ORDER BY T.NAME
The query looks really a mess. Even if I need to write something that wraps around the entire query, it would gradually become unreadable. CTE allows you to generate Tables beforehand and use it later when we actually bind the data into the output.
With T(Address, Name, Age) --Column names for Temporary table
AS
(
SELECT A.Address, E.Name, E.Age from Address A
INNER JOIN EMP E ON E.EID = A.EID
)
SELECT * FROM T --SELECT or USE CTE temporary Table
WHERE T.Age > 50
ORDER BY T.NAME
Yes as you can see, the second query is much more readable using CTE.
You can specify as many query expressions as you want and the final
query which will output the data to the external environment will
eventually get reference to all of them.
With T1(Address, Name, Age) --Column names for Temporary table
AS
(
SELECT A.Address, E.Name, E.Age from Address A
INNER JOIN EMP E ON E.EID = A.EID
),
T2(Name, Desig)
AS
(
SELECT NAME, DESIG FROM Designation)
SELECT T1.*, T2.Desig FROM T1 --SELECT or USE CTE temporary Table
WHERE T1.Age > 50 AND T1.Name = T2.Name
ORDER BY T1.NAME
So the queries are separated using commas. So basically you can pass
as many queries as you want and these queries will act as a subqueries,
getting you the data and name it as a temporary table in the query. According to the syntax, the CTE starts with a
With
clause. You can specify the column names in braces, but it is not mandatory.SQL SERVER – Simple Example of Recursive CTE
Recursive is the process in which the
query executes itself. It is used to get results based on the output of
base query. We can use CTE as Recursive CTE (Common Table Expression)
Here, the result of CTE is repeatedly
used to get the final resultset. The following example will explain in
detail where I am using AdventureWorks database and try to find
hierarchy of Managers and Employees.
USE AdventureWorks
GO
WITH Emp_CTE AS (
SELECT EmployeeID, ContactID, LoginID, ManagerID, Title, BirthDate
FROM HumanResources.Employee
WHERE ManagerID IS NULL
UNION ALL
SELECT e.EmployeeID, e.ContactID, e.LoginID, e.ManagerID, e.Title, e.BirthDate
FROM HumanResources.Employee e
INNER JOIN Emp_CTE ecte ON ecte.EmployeeID = e.ManagerID
)
SELECT *
FROM Emp_CTE
GO
In the above example Emp_CTE is a Common
Expression Table, the base record for the CTE is derived by the first
sql query before UNION ALL. The result of the query gives you the
EmployeeID which don’t have ManagerID.
Second query after UNION ALL is executed
repeatedly to get results and it will continue until it returns no rows.
For above e.g. Result will have EmployeeIDs which have ManagerID (ie,
EmployeeID of the first result). This is obtained by joining CTE result
with Employee table on columns EmployeeID of CTE with ManagerID of
table Employee.
This process is recursive and will continue till there is no ManagerID who doesn’t have EmployeeID.
No comments:
Post a Comment