-- Valid
WITH EmpCTE
AS
( SELECT * FROM HumanResources.Employee
WHERE ManagerID = 10
UNION
SELECT * FROM HumanResources.Employee
WHERE DepartmentID = 3
UNION ALL
SELECT E.*
FROM HumanResources.Employee AS E
JOIN EmpCTE AS M
ON E.ManagerID = M.EmployeeID )
SELECT * FROM EmpCTE;
-- Invalid
WITH EmpCTE
AS
( SELECT * FROM HumanResources.Employee
WHERE ManagerID = 10
UNION ALL
SELECT E.*
FROM HumanResources.Employee AS E
JOIN EmpCTE AS M
ON E.ManagerID = M.EmployeeID
UNION
SELECT * FROM HumanResources.Employee
WHERE DepartmentID = 3 )
SELECT * FROM EmpCTE;