Jump to content

Common table expression

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Yobot (talk | contribs) at 15:57, 9 September 2010 (WP:CHECKWIKI error fixes +genfixes using AWB (7105)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A Common Table Expression, or CTE, (in SQL) is a temporary named result set, derived from a simple query and defined within the execution scope of a SELECT, INSERT, UPDATE, or DELETE statement.

CTEs can be thought of as alternatives to derived tables (subquery), views, and inline user-defined functions.

Common table expressions

Common table expressions are supported by DB2, Firebird [1], Microsoft SQL Server, Oracle and PostgreSQL. Syntax:

WITH [RECURSIVE] with_query [, ...]
SELECT...

with_query looks like

with_query_name [ (column_name [,...]) ] AS (SELECT ...)

References