Skip to main content

Common Table Expressions (CTE)

CTEs allow you to build reusable query fragments and simplify complex queries by breaking them into named subqueries.

Example Usage

const users = await User.query()
.with('normal', (cte) =>
cte.newCte('users_cte', (b) => b.select('name'))
)
.many();

CTEs can be used for:

  • Recursive queries
  • Simplifying multi-step data transformations
  • Improving query readability

See also:

Next: MongoDB Introduction