WITH (Common Table Expressions

SQL SQL

Posted by admin on 2023-01-29 09:57:38 |

Share: Facebook | Twitter | Whatsapp | Linkedin Visits: 639


WITH (Common Table Expressions

A common table expression (CTE) is a defined short result set that endures within the range of a particular statement and that can be called later within that statement, perhaps on many occasions. The following query is describing the CTE:

Syntax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
WITH all_emp
AS
(
SELECT empId, BossId, FirstName, LastName
FROM Emp
WHERE BossId is NULL
 
UNION ALL
 
SELECT e.empId, e.BossId, e.FirstName, e.LastName
FROM Emp e INNER JOIN all_emp r
ON e.BossId = r.Id
)
SELECT * FROM all_emp

Leave a Comment: