With문
WITH CTE_name AS (
-- CTE 정의
SELECT column1, column2
FROM table_name
WHERE condition
)
-- CTE를 사용하는 쿼리
SELECT *
FROM CTE_name
WHERE some_column = some_value;
예제
WITH EmployeeManager AS (
SELECT e.id AS employee_id, e.name AS employee_name, m.name AS manager_name
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id
)
SELECT *
FROM EmployeeManager;