WITH
Use WITH
to specify a Common Table Expression.
What is a Common Table Expression (CTE)?
A Common Table Expression (CTE) is a temporary result set that you can reference within a SELECT
, INSERT
, UPDATE
, or DELETE
statement. CTEs help to break down complex queries into more readable parts and can be referenced multiple times within the same query.
Basic syntax of CTE
CTEs are typically defined using the WITH
keyword. The basic syntax is as follows:
WITH cte_name [(column1, column2, ...)] AS (
QUERY
)
SELECT ...
FROM cte_name;