Lesson 8 of 12 · 20 min
Common table expressions (WITH)
By the end of this lesson you can rewrite a derived table as a common table expression, define several CTEs in one WITH clause, reference a CTE more than once, and explain how PostgreSQL executes them.
Derived tables work, but they make you read inside out: the first thing you see is the final SELECT, and the real starting point is buried in the middle. A common table expression (CTE) moves that inner query to the top and gives it a name:
This lesson, its 4 graded exercises and every other Members lesson in SQL Quest come with a Formula Quest membership that includes SQL (Single Quest with SQL selected, Quest Trio including SQL, or Quest Unlimited), or with SQL access from your organization.
Free to everyone: all of SQL Foundations and Retrieving and Filtering Data, the first lesson of every track, the playground, the dataset library and the SQL reference. See free lessons