- Documentation
- Installation
- Data Import
- Client APIs
- SQL
- Introduction
- Statements
- Overview
- Select
- Insert
- Delete
- Update
- Create Schema
- Create Table
- Create View
- Create Sequence
- Create Macro
- Drop
- Alter Table
- Copy
- Export
- Query Syntax
- Data Types
- Expressions
- Functions
- Overview
- Numeric Functions
- Text Functions
- Pattern Matching
- Date Functions
- Timestamp Functions
- Time Functions
- Interval Functions
- Date Formats
- Date Parts
- Blob Functions
- Nested Functions
- Indexes
- Aggregates
- Window Functions
- Samples
- Pragmas
- Full Text Search
- Development
- Sitemap
- Why DuckDB
- FAQ
- Code of Conduct
- Live Demo
The WITH
clause allows you to specify common table expressions (CTEs). Regular (non-recursive) common-table-expressions are essentially views that are limited in scope to a particular query. CTEs can reference each-other.
Examples
-- create a CTE called "cte" and use it in the main query
WITH cte AS (SELECT 42)
SELECT * FROM cte;
-- create two CTEs, where the second CTE references the first CTE
WITH cte AS (SELECT 42 AS i),
cte2 AS (SELECT i*100 FROM cte)
SELECT * FROM cte2;
Common Table Expressions
Search Shortcut cmd + k | ctrl + k